Friday 19 August 2016

Scala Intermediate and Advanced Interview Questions and Answers

Introduction

Before reading this post, Please go through my previous Scala Interview Questions and Answers posts:
In this post, we are going to discuss all remaining Scala Interview Questions and Answers which are related to Intermediate and Advanced Category.

Scala Intermediate and Advanced Interview Questions

  1. How to develop Immutable Classes in Scala?
  2. What are the major Advantages of Scala’s Case Classes?
  3. What is String Interpolation in Scala? How do we use String Interpolation in Scala?
  4. What is SBT? Explain about some basic commands of SBT tool?
  5. What is the main use of Option and Either in Scala? How do we handle Errors or Exceptions in Functional Style(FP style) in Scala?
  6. Explain the difference between Scala’s protected and Java’s protected Access Modifier?
  7. Like Java’s Package level Access Modifier, is there anything same concept in Scala? How to provide Package level Access Modifier in Scala?

  8. Scala Intermediate and Advanced Interview Questions and Answers

    In this section, we will pickup each and every question from above list and discuss in-detail with suitable examples(if required). If you want to understand these concepts in-depth with examples, please go through my previous posts in Scala Tutorials section.

    IN PROGRESS

    Please check this post with full content Soon.
    As this post is already covered enough questions, I'm going to answer rest of the questions in the following posts:
            • Scala Advanced Interview Questions and Answers (Part-2)
            • Scala Advanced Interview Questions and Answers (Part-3)

              How to develop Immutable Classes in Scala?

              As we know, we need to follow a set of rules to develop an Immutable Class in Java. However, it is not that much tough to develop Immutable Classes in Scala.
              By Default, Scala’s Case Class parameters are ‘val’ that means we cannot change it’s parameters once created. So by nature, Scala’s Case classes are Immutable. We don’t need to do any extra logic.

              What are the major Advantages of Scala’s Case Classes?

              Scala’s Case Classes have the following advantages:
              • By default, they are Immutable.
              • They avoid lot of boilerplate code.
              • They ease the development process and improve Productivity.
              • They are very easy to use in Pattern Matching.

              What is String Interpolation in Scala? How do we use String Interpolation in Scala?

              In Scala, String Interpolation means replacement of defined variables or expressions in the String with values. We should use s String Interpolator to define processed String Literal as shown below:

              Example:-
              1
              2
              3
              4
              5
              6
              7
              8
              9
              10
              11
              scala> val title1 = "Play"
              title1: String = Play
              scala>val title2 = "Scala"
              title2: String = Scala
              scala>val book1 = "Book($title1 2 for $title2)"
              book1: String = Book($title1 2 for $title2)
              scala>val book2 = s"Book($title1 2 for $title2)"
              book2: String = Book(Play 2 for Scala)
              If we observe both book1 and book2 results, we can observe that in book2 we have used String Interpolation concept that’s why that String is evaluated to correct expected result.

              What is SBT? Explain about some basic commands of SBT tool?

              SBT stands for Scala Build Tool Or Build Tool for Scala.
              Like Ant, Maven and Gradle, it’s a build tool for Scala/Play/Akka based applications to automate the compile, build and deployment process.
              Some useful SBT Commands here:
              • sbt compile
              • To compile all project files.
              • sbt test
              • To run and test all unit tests.
              • sbt run
              • To compile all files, run and test all unit tests and deploy application with single step.

              What is the main use of Option and Either in Scala? How do we handle Errors or Exceptions in Functional Style(FP style) in Scala?

              In Scala, the main purpose of Option and Either constructs is to develop code in such a way that we don’t need to deal with ‘null’ checks, No need to handle NullPointerException and other exceptions.
              In Scala, we can handle Errors or Exceptions in Functional Style(Functional Programming (FP) style) by using Option and Either constructs.
              NOTE:-See my “Scala Option/Some/None In-Depth” and “Scala Either/Left/Right In-Depth” posts for more details and practical examples.

              Explain the difference between Scala’s protected and Java’s protected Access Modifier?

              In Java, we can use ‘protected’ keyword to access that component in the same package (within all sub-classes or any other classes which are available in the same package) or any subclass available in any package.
              In Scala, ‘protected’ access modifier has different meaning. We can use it in two different ways:
              • protected
              • protected[T]
              Here “T” parameter can be a Scaa class/trait, package or object.
              If we use Non-Parameterized protected that is “protected”, then that is visible only from it’s subclasses. It not visible from the same package or outside.
              If we use Parameterized protected that is “protected[T]”, then that is visible from that component only.
              Examples:-
              1. If we use something like protected[Account] where “Account” is a Scala class, then that is visible within that class only. It is not visisble outside that class.
              2. If we use something like protected[retailbank] where “retailbank” is a Scala package, then that is visible within that package (and sub-packages) only. It is not visible outside that package.
              NOTE:-See my Scala Access Modifiers In-Depth post for more details and practical examples.

              Like Java’s Package level Access Modifier, is there anything same concept in Scala? How to provide Package level Access Modifier in Scala?

              As we know in Java, we can use default access modifier (that is without using any access modifier) to provide package level access. But this is different in Scala.
              In Scala,if we don’t specify any access modifier, that means we are using ‘public’ access modifier. There is no ‘public’ keyword in Scala to do this.
              In Scala, to provide package level access we can use protected[T] syntax where T is the package name.
              That’s it all about “Scala Interview Questions and Answers”. We will discuss some more Scala Interview Questions and Answers in my coming posts.
              Please drop me a comment if you like my post or have any issues/suggestions.

            No comments:

            Post a Comment