Explore topic-wise InterviewSolutions in .

This section includes InterviewSolutions, each offering curated multiple-choice questions to sharpen your knowledge and support exam preparation. Choose a topic below to get started.

1.

If I Want To Become A Fullstack Scala Developer, Which Technology Stack I Should Learn?

Answer»

If you want to become a Fullstack Scala Developer, you should learn the following TECHNOLOGY stack:

  • Scala 2.11.7
  • Play 2.4.6 Framework
  • Akka 2.3 Framework
  • One Build Tool: SBT/Maven
  • One JS Framework: CoffeeScript/JavaScript
  • One IDE: IntelliJ IDEA 15/ Eclipse IDE 4.x
  • One TDD & BDD Framework: ScalaTest,Spec2,ScalaCheck,Mockito
  • Micro Services with Play and Scala
  • SCoverage
  • Scalastyle
  • FUNCTIONAL Programming Design Patterns
  • Machine Learning with Scala

If you want to become a Fullstack Scala Developer, you should learn the following technology stack:

2.

What Is The Difference Between :: And #:: In Scala? What Is The Difference Between ::: And #::: In Scala?

Answer»

In Scala Collection API,

  • :: and ::: are methods available in List class.
  • #:: and #::: are methods available in STREAM class
  • In List class, :: method is used to append an element to the beginning of the list.

    scala> VAR list1 = List(1,2,3,4)
    list1: List[Int] = List(1, 2, 3, 4)
     
    scala> list1 = 0 :: list1
    list1: List[Int] = List(0, 1, 2, 3, 4)

  • In List class, ::: method is used to concatenate the elements of a given list in FRONT of this list.

    scala> var list1 = List(3,4,5)
    list1: List[Int] = List(3, 4, 5)
     
    scala> val list2 = List(1,2) ::: list1
    list2: List[Int] = List(1, 2, 0, 1, 2, 3, 4)

  • In Stream class, #:: method is used to append a given element at beginning of the stream. Only this newly added element is evaluated and followed by lazily evaluated stream elements.

    scala> var s1 = Stream(1,2,3,4)
    s1: scala.collection.immutable.Stream[Int] = Stream(1, ?)
     
    scala> s1 = 0 #:: s1
    s1: scala.collection.immutable.Stream[Int] = Stream(0, ?)

  • In Stream class, #::: method is used to concatenate a given stream at beginning of the stream. Only this newly added element is evaluated and followed by lazily evaluated stream elements.

    scala> var s1 = Stream(1,2,3,4)
    s1: scala.collection.immutable.Stream[Int] = Stream(1, ?)
     
    scala> val s2 = Stream(-1,0) #::: s1
    s2: scala.collection.immutable.Stream[Int] = Stream(-1, ?)

  • :: method works as a cons OPERATOR for List class and #:: method words as a cons operator for Stream class. Here ‘cons’ stands for construct.
  • ::: method works as a concatenation operator for List class and #::: method words as a concatenation operator for Stream class.

In Scala Collection API,

3.

Explain The Main Difference Between List And Stream In Scala Collection Api? How Do We Prove That Difference? When Do We Choose Stream?

Answer»

In Scala, both List and Stream are from Collection API and works almost similar. Both are Immutable COLLECTIONS.

However, there is one main difference between List and Stream in Scala Collection API: That is List elements are evaluated Eagerly and Stream elements are evaluated Lazily that means when we access them.

scala> VAR list1 = List(1,2,3,4)
list1: List[Int] = List(1, 2, 3, 4)

Here we can observe that all List elements evaluated at the time of creating List object. However, if we do same thing on Stream, we cannot SEE all elements. We can see only first evaluated element and remaining elements are evaluated lazily as shown below:

scala> var s1 = Stream(1,2,3,4)
s1: scala.collection.immutable.Stream[Int] = Stream(1, ?)

When we want LAZY collection to evaluate elements only when we access them then it’s better to use Stream.

In Scala, both List and Stream are from Collection API and works almost similar. Both are Immutable collections.

However, there is one main difference between List and Stream in Scala Collection API: That is List elements are evaluated Eagerly and Stream elements are evaluated Lazily that means when we access them.

scala> var list1 = List(1,2,3,4)
list1: List[Int] = List(1, 2, 3, 4)

Here we can observe that all List elements evaluated at the time of creating List object. However, if we do same thing on Stream, we cannot see all elements. We can see only first evaluated element and remaining elements are evaluated lazily as shown below:

scala> var s1 = Stream(1,2,3,4)
s1: scala.collection.immutable.Stream[Int] = Stream(1, ?)

When we want Lazy collection to evaluate elements only when we access them then it’s better to use Stream.

4.

What Is The Use Of ‘???’ In Scala-based Applications?

Answer»

This ‘???’ three question marks is not an operator, a method in Scala. It is used to MARK a method which is ‘In PROGRESS’ that means Developer should PROVIDE implementation for that one.

This method is define in scala.PreDef class as shown below:

def ??? : Nothing = throw new NotImplementedError

If we RUN that method without providing implementation, then it throws ‘NotImplementedError’ error as shown below:

scala> def add(a:Int, b:Int) : Int = ???
add: (a: Int, b: Int)Int
 
scala> add(10,20)

scala.NotImplementedError: an implementation is MISSING

This ‘???’ three question marks is not an operator, a method in Scala. It is used to mark a method which is ‘In Progress’ that means Developer should provide implementation for that one.

This method is define in scala.PreDef class as shown below:

def ??? : Nothing = throw new NotImplementedError

If we run that method without providing implementation, then it throws ‘NotImplementedError’ error as shown below:

scala> def add(a:Int, b:Int) : Int = ???
add: (a: Int, b: Int)Int
 
scala> add(10,20)

scala.NotImplementedError: an implementation is missing

5.

What Is Extractor In Scala? What Is The Difference Between Constructor And Extractor In Scala? What Is The Use Of Extractor In Scala?

Answer»

Not only in Java and Scala, in almost all OOP languages Constructor is used to create (or assemble) an object or an instance of a Class using it’s parameters (or components). Extractor is quite OPPOSITE to Constructor.

In Scala, Extractor is used to decompose or disassemble an object into it’s parameters (or components).

In Scala, apply method is a Constructor. Internally, Extractor uses unapply method to decompose an OBJECTS into it’s parts (or parameters). In Scala, Extractor is mainly used in PATTERN MATCHING CONCEPT. We will discuss Pattern Matching concept soon.

Not only in Java and Scala, in almost all OOP languages Constructor is used to create (or assemble) an object or an instance of a Class using it’s parameters (or components). Extractor is quite opposite to Constructor.

In Scala, Extractor is used to decompose or disassemble an object into it’s parameters (or components).

In Scala, apply method is a Constructor. Internally, Extractor uses unapply method to decompose an objects into it’s parts (or parameters). In Scala, Extractor is mainly used in Pattern Matching concept. We will discuss Pattern Matching concept soon.

6.

What Are The Popular Mvc Frameworks For Scala Language To Develop Web Applications?

Answer»

The following are the most popular MVC frameworks available for Scala Language to DEVELOP Web Applications:

The following are the most popular MVC frameworks available for Scala Language to develop Web Applications:

7.

What Is Call-by-name? Does Scala And Java Support Call-by-name? What Is The Difference Between Call-by-value And Call-by-name Function Parameters?

Answer»

Call-by-name means evaluates method/function parameters only when we need them or we access them. If we don’t use them, then it does not EVALUATE them.

Scala supports both call-by-value and call-by-name function parameters. However, Java supports only call-by-value, but not call-by-name.

Difference between call-by-value and call-by-name:

The major difference between these two are described below:

  • In Call-by-name, the function parameters are evaluated only whenever they are needed but not when the function is CALLED.
  • In Call-by-value, the function parameters are evaluated when the function is called.
  • In Call-by-value, the parameters are evaluated before EXECUTING function and they are evaluated only once IRRESPECTIVE of how many times we used them in that function.
  • In Call-by-name, the parameters are evaluated whenever we access them and they are evaluated each time we use them in that function.
  • Scala SYNTAX Differences

Call-by-value:

1 def myFunction(a: Int, b: Int) { }

Here both a and b are Call-by-value parameters to myFunction.

Call-by-name:

1 def myFunction(a: Int, b: => Int) { }

Here both a is a Call-by-value parameter and b is Call-by-name to myFunction.

Call-by-name means evaluates method/function parameters only when we need them or we access them. If we don’t use them, then it does not evaluate them.

Scala supports both call-by-value and call-by-name function parameters. However, Java supports only call-by-value, but not call-by-name.

Difference between call-by-value and call-by-name:

The major difference between these two are described below:

Call-by-value:

1 def myFunction(a: Int, b: Int) { }

Here both a and b are Call-by-value parameters to myFunction.

Call-by-name:

1 def myFunction(a: Int, b: => Int) { }

Here both a is a Call-by-value parameter and b is Call-by-name to myFunction.

8.

What Are The Java’s Oop Constructs Not Supported By Scala? What Are The Scala’s Oop Constructs Not Supported By Java? What Are The New Oops Constructs Introduced By Scala, But Not Supported By Java?

Answer»

JAVA’s OOP constructs, which are not SUPPORTED by Scala:

  • There is no INTERFACE concept in Scala
  • There is no Enum concept in Scala

Scala’s OOP constructs, which are not supported by Java:

OR

The new OOPs constructs introduced by Scala, but not supported by Java:

Java’s OOP constructs, which are not supported by Scala:

Scala’s OOP constructs, which are not supported by Java:

OR

The new OOPs constructs introduced by Scala, but not supported by Java:

9.

What Are The Advantages Of Play/scala Stack To Develop Web Applications?

Answer»

The following are the major advantages of Play/Scala stack to develop web applications:

  • Open Source: Play is an Open-source free-software framework to develop web applications.
  • Better Productivity: Play framework’s Auto-reload feature improves Developer Productivity. No need to build, deploy and test our changes. Just do our changes and refresh the page to see our changes.
  • STATELESS and Easy to Develop REST API: Play is HTTP based stateless model to serve web requests so it is very easy to develop REST API or RESTful Web Services.
  • Better Error-Handling: If we develop our web application using Play framework,it informs all errors in the browser in very useful format. It shows error message, the file location, line number where error occurred, highlighting the code-snippet to understand the error very easily.
  • High Performance and Better Scalability With REACTIVEPlay framework is developed by following Reactive design patterns and it is built on top of Netty sever to utilize Non-blocking IO Feature. Because of this feature, we can develop very highly Scalable and performance applications very easily.
  • Easy to Extend: Play is very FLEXIBLE framework and supports DEVELOPING plug-ins very easy to extend it’s features and functionality.
  • Highly Concurrency and Better Parallelism: As both Scala and Play supports Functional Programming, it is very easy to develop Highly Concurrency and Better Parallelism applications very easily because FP supports Immutability, Pure Functions (Functions without side-effects), Pattern Matching, Actor Model etc.
  • Better Reusability, Easy to Test and More Modular: As both Scala and Play supports Functional Programming, we can develop more modular and reusable applications. It is ALSO very easy to test more modular applications.

The following are the major advantages of Play/Scala stack to develop web applications:

10.

What Are The Differences Between Case Class And Normal Class?

Answer»

Case class is also a class, however when we compare it with normal class, it gives the following extra features or benefits:

  • By default, Case-class constructor parameters are ‘val’. We don’t need to declare parameters with ‘val’.
  • By default, Case-class constructor parameters become class fields.
  • These methods are added AUTOMATICALLY: TOSTRING, equals, hashCode, copy. APPLY and unapply.
  • It automatically gets COMPANION object.
  • No need to use ‘new’ keyword to create instance of Case Class.
  • EASY to use in Pattern Matching.

All these features are added by Scala Compiler at compile-time. It is not possible with normal class.

Case class is also a class, however when we compare it with normal class, it gives the following extra features or benefits:

All these features are added by Scala Compiler at compile-time. It is not possible with normal class.

11.

What Is An Higher-order Function (hof)?

Answer»

Higher Order Function (HOF) is ALSO a function but which performs one, two or both of the FOLLOWING THINGS:

  • TAKE other functions as ARGUMENTS
  • Return functions as their results

Higher Order Function (HOF) is also a function but which performs one, two or both of the following things:

12.

What Is An Anonymous Function In Scala? What Is A Function Literal In Scala? What Are The Advantages Of A Anonymous Function/function Literal In Scala?

Answer»

Anonymous Function is ALSO a Function but it does not have any function NAME. It is also KNOWN as a Function Literal.

The advantages of a Anonymous Function/Function Literal in Scala:

  • We can ASSIGN a Function Literal to variable
  • We can pass a Function Literal to another function/method
  • We can return a Function Literal as another function/method result/return value.

Anonymous Function is also a Function but it does not have any function name. It is also known as a Function Literal.

The advantages of a Anonymous Function/Function Literal in Scala:

13.

Why Scala Is Better Than Java? What Are The Advantages Of Scala Over Java (java 8)? Compare To Java What Are The Major Advantages Or Benefits Of Scala?

Answer»

Because Scala supports the following extra features, it is better than Java 8:

  • Full FP Features
  • More Expression Language
  • Pattern Matching
  • Better SUPPORT for Akka Actor Model
  • Automatic resolution for INHERITANCE Diamond PROBLEM with TRAITS
  • Asynchronous and Non-blocking IO programming using Akka Framework
  • Fully Reactive Streaming API

Because Scala supports the following extra features, it is better than Java 8:

14.

What Is The Default Unit And Functional Testing Framework For Play? What Is The Default Build Tool For Play? What Is The Default Template Engine For Play? What Is The Built-in Web Server Available In Play Framework?

Answer»

Play Framework’s DEFAULT UNIT and Functional TESTING Framework is Spec2. It is very easy to test Play/Scala based APPLICATIONS using Spec2 Framework.

Play Framework’s Default built-in template is “Twirl”. It was developed in Scala. By using these templates, we can DEVELOP Play/Scala based applications very easily.

The Built-in or Default Web Server available for Play Framework is Netty Server.

Play Framework’s default Unit and Functional Testing Framework is Spec2. It is very easy to test Play/Scala based applications using Spec2 Framework.

Play Framework’s Default built-in template is “Twirl”. It was developed in Scala. By using these templates, we can develop Play/Scala based applications very easily.

The Built-in or Default Web Server available for Play Framework is Netty Server.

15.

Which Ides Support Play And Scala-based Applications Development And How?

Answer»

The following two popular IDES SUPPORT Play and SCALA-Based Applications Development:

  • IntelliJ IDEA
  • Eclipse IDE

They support by using Scala Plugins like Eclipse IDE has a Scala IDE for Eclipse to support Play and Scala-Based Applications Development.

IntelliJ IDEA has a plug-in like “Scala Plugin for IntelliJ IDEA” to support “Scala, SBT and Play 2 Framework” based applications.

The following two popular IDEs support Play and Scala-Based Applications Development:

They support by using Scala Plugins like Eclipse IDE has a Scala IDE for Eclipse to support Play and Scala-Based Applications Development.

IntelliJ IDEA has a plug-in like “Scala Plugin for IntelliJ IDEA” to support “Scala, SBT and Play 2 Framework” based applications.

16.

What Is The Best Scala Style Checker Tool Available For Play And Scala Based Applications?

Answer»

LIKE Checkstyle for Java-Based APPLICATIONS, Scalastyle is best Scala style checker tool available for Play and Scala based applications.

Scalastyle observes our Scala source code and indicates potential PROBLEMS with it. It has three separate plug-ins to SUPPORTS the following build tools:

It has two separate plug-ins to supports the following two IDEs:

  • IntelliJ IDEA
  • Eclipse IDE

Like Checkstyle for Java-Based Applications, Scalastyle is best Scala style checker tool available for Play and Scala based applications.

Scalastyle observes our Scala source code and indicates potential problems with it. It has three separate plug-ins to supports the following build tools:

It has two separate plug-ins to supports the following two IDEs:

17.

What Is The Best Code-coverage Tool Available For Play And Scala Based Applications?

Answer»

SCoverage is the Code-coverage tool for Play and Scala based APPLICATIONS.

SCoverage stands for Scala Code-coverage tool. It has THREE separate plug-ins to SUPPORTS the FOLLOWING build tools:

  • SBT
  • Maven
  • Gradle

SCoverage is the Code-coverage tool for Play and Scala based applications.

SCoverage stands for Scala Code-coverage tool. It has three separate plug-ins to supports the following build tools:

18.

What Are The Available Unit Testing, Functional Testing And/or Bdd Frameworks For Play And Scala Based Applications?

Answer»

The following are most POPULAR available UNIT Testing, FUNCTIONAL Testing and/or BDD FRAMEWORKS for Play/Scala Based APPLICATIONS:

  • Spec2
  • ScalaTest
  • ScalaCheck
  • Mokito

The following are most popular available Unit Testing, Functional Testing and/or BDD Frameworks for Play/Scala Based applications:

19.

What Is Sbt? What Is The Best Build Tool To Develop Play And Scala Applications?

Answer»

SBT stands for Scala BUILD Tool. Its a Simple Build Tool to develop Scala-based APPLICATIONS.

Most of the PEOPLE USES SBT Build tool for Play and Scala Applications. For example, IntelliJ IDEA Scala Plugin by DEFAULT uses SBT as Build tool for this purpose.

SBT stands for Scala Build Tool. Its a Simple Build Tool to develop Scala-based applications.

Most of the people uses SBT Build tool for Play and Scala Applications. For example, IntelliJ IDEA Scala Plugin by default uses SBT as Build tool for this purpose.

20.

What Are The Available Build Tools To Develop Play And Scala Based Applications?

Answer»

The following three are most popular available BUILD Tools to DEVELOP PLAY and Scala Applications:

The following three are most popular available Build Tools to develop Play and Scala Applications:

21.

How Scala Supports Both Highly Scalable And Highly Performance Applications?

Answer»

As Scala supports Multi-Paradigm Programming(Both OOP and FP) and uses Actor CONCURRENCY Model, we can DEVELOP very highly SCALABLE and high-performance APPLICATIONS very EASILY.

As Scala supports Multi-Paradigm Programming(Both OOP and FP) and uses Actor Concurrency Model, we can develop very highly Scalable and high-performance applications very easily.

22.

What Is The Best Language To Use With Play Framework: Scala Or Java?

Answer»

Play 2 is COMPLETELY written in Scala. If we use Java with Play framework, we need to face MANY issues because Java does not support full FP features.

Scala is the best option to use with Play framework to develop Highly Scalable, BETTER Performance with Concurrency/Parallelism and Low latency applications, because:

  • Play 2 is completely written in Scala.
  • It supports full FP features.
  • It is more expression language than Java.
  • It supports Akka Actor model very EASILY
  • It supports some new OOP feature like Traits.
  • Play’s built-in templates are developed in Scala

Play 2 is completely written in Scala. If we use Java with Play framework, we need to face many issues because Java does not support full FP features.

Scala is the best option to use with Play framework to develop Highly Scalable, Better Performance with Concurrency/Parallelism and Low latency applications, because:

23.

Popular Clients Who Are Using Play And Scala To Develop Their Applications?

Answer»

Thousands of CLIENTS are USING PLAY and Scala in Production. The following LIST is the more popular clients who are using Play and Scala ACTIVELY.

  • LinkedIn
  • The Guardian
  • Ocado
  • LuchidChart
  • GOV.UK

Thousands of clients are using Play and Scala in Production. The following list is the more popular clients who are using Play and Scala actively.

24.

What Is The Best Tool To Develop Play/scala Applications To Persist Data In Mongodb Nosql Data Store?

Answer»

ReactiveMongo is the BEST Scala Driver to DEVELOP Play/Scala applications to persist data in MongoDB NoSQL data STORE. It SUPPORTS fully non-blocking and asynchronous I/O OPERATIONS.

ReactiveMongo is the best Scala Driver to develop Play/Scala applications to persist data in MongoDB NoSQL data store. It supports fully non-blocking and asynchronous I/O operations.

25.

Like Hibernate For Java-based Applications, What Are The Popular Orm Frameworks Available To Use In Play/scala Based Applications?

Answer»

Like JPA, HIBERNATE and Toplink ETC ORM Frameworks for Java-based applications, There are MANY ORM frameworks to USE in Play/Scala based applications.

Popular ORM frameworks for Play/Scala based applications:

  • SLICK
  • Anorm
  • SORM(Scala ORM)
  • Squeryl

Like JPA, Hibernate and Toplink etc ORM Frameworks for Java-based applications, There are many ORM frameworks to use in Play/Scala based applications.

Popular ORM frameworks for Play/Scala based applications:

26.

What Is The Best Framework To Generate Rest Api Documentation For Scala-based Applications?

Answer»

Swagger is is the best TOOL for this purpose. It is very SIMPLE and open-source tool for GENERATING REST APIs documentation with JSON for Scala-based applications.

  • If we use Play with Scala to develop your REST API, then use play-swagger module for REST API documentation.
  • If we use Spray with Scala to develop your REST API, then use spray-swagger module for REST API documentation.

Swagger is is the best tool for this purpose. It is very simple and open-source tool for generating REST APIs documentation with JSON for Scala-based applications.

27.

What Are The Popular Scala-based Frameworks To Develop Restful Web Services Or Rest Api?

Answer»

There are many Scala-Based Framework to develop RESTFUL Web Services. Most popular frameworks are:

  • Play Framework: n Play, we call REST API URLs as routes. We place all routes at once place in Play framework. It is a stateless web framework to develop REST API EASILY.
  • Scalatra Framework: It is very simple and easy Scala-based web framework to develop REST API
  • SPRAY Framework: It is very concise and built on top of AKKA framework so it’s better to develop REST API using Actor Model.
  • Lift Framework: It allows routing using PATTERN Matching concept.

There are many Scala-Based Framework to develop RESTful Web Services. Most popular frameworks are:

28.

What Are The Advantages Of Functional Programming (fp) Or Advantages Of Pure Functions?

Answer»

The following are the Advantages of FUNCTIONAL Programming (FP) or Advantages of Pure Functions:

  • More Modular
  • EASIER to understand Or Easier reason about
  • Easier to test
  • Less PRONE to bugs
  • Easier to REUSE
  • Easier to Parallelism and generalize

The following are the Advantages of Functional Programming (FP) or Advantages of Pure Functions:

29.

What Is The Equivalent Construct Of Scala’s Option In Java Se 8? What Is The Use Of Option In Scala?

Answer»

Scala’s Option is similar to Java SE 8’s OPTIONAL. Java SE 8 has introduced a new utility class Optional to REPRESENT existing or non-existing of some value. Optional is AVAILABLE in java.util package.

Both Scala’s Option and Java SE 8’s Optional are used to represent optional values. Both are used to AVOID unwanted null checks and NullPointerException.

Scala’s Option is similar to Java SE 8’s Optional. Java SE 8 has introduced a new utility class Optional to represent existing or non-existing of some value. Optional is available in java.util package.

Both Scala’s Option and Java SE 8’s Optional are used to represent optional values. Both are used to avoid unwanted null checks and NullPointerException.

30.

What Is Either In Scala? What Are Left And Right In Scala? Explain Either/left/right Design Pattern In Scala?

Answer»

In Scala, Either is an abstract CLASS. It is used to REPRESENT one value of two possible types. It takes two type parameters: Either[A,B].

It EXACTLY have two SUBTYPES: Left and Right. If Either[A,B] represents an INSTANCE A that means it is Left. If it represents an instance B that means it is Right.

This is known as Either/Left/Right Design Pattern in Scala.

In Scala, Either is an abstract class. It is used to represent one value of two possible types. It takes two type parameters: Either[A,B].

It exactly have two subtypes: Left and Right. If Either[A,B] represents an instance A that means it is Left. If it represents an instance B that means it is Right.

This is known as Either/Left/Right Design Pattern in Scala.

31.

What Is Option In Scala? What Are Some And None? What Is Option/some/none Design Pattern In Scala?

Answer»

In Scala, Option is used to represent optional values that is either exist or not exist.

Option is an abstract class. Option has two subclasses: Some and None. All three (Option, Some and None) are defined in “scala” package like “scala.Option”.

Option is a bounded collection in Scala, which CONTAINS either zero or one element. If Option contains zero elements that is None. If Option contains one element, that is Some.

Some is used to represent existing value. None is used to represent non-existent value.

Example:-

def get(val index: Int): Option[String]

LET us assume that this method is from LIST. This method has a return type of Option[String]. If List contains elements, this get method returns “Some[String]” element AVAILABLE in that index position. OTHERWISE, it returns “None” (that is no elements)

Some is a case class and None is an Object. As both are case class/object, we can use them in Pattern Matching very well.

The combination of all these three definitions is known as Option/Some/None Design Pattern in Scala.

In Scala, Option is used to represent optional values that is either exist or not exist.

Option is an abstract class. Option has two subclasses: Some and None. All three (Option, Some and None) are defined in “scala” package like “scala.Option”.

Option is a bounded collection in Scala, which contains either zero or one element. If Option contains zero elements that is None. If Option contains one element, that is Some.

Some is used to represent existing value. None is used to represent non-existent value.

Example:-

def get(val index: Int): Option[String]

Let us assume that this method is from List. This method has a return type of Option[String]. If List contains elements, this get method returns “Some[String]” element available in that index position. Otherwise, it returns “None” (that is no elements)

Some is a case class and None is an Object. As both are case class/object, we can use them in Pattern Matching very well.

The combination of all these three definitions is known as Option/Some/None Design Pattern in Scala.

32.

What Is The Current Latest Version Of Scala? What Is The Major Change Or Update In Scala 2.12?

Answer»

Current SCALA’s stable is 2.11.7. It supports Java SE 7.

The major change or update in Scala 2.12 version is that it supports Java SE 8 or LATER versions only. Scala 2.12 is not a BINARY COMPATIBLE with the 2.11.x series. It’s still in Mile Stone Builds only.

Current Scala’s stable is 2.11.7. It supports Java SE 7.

The major change or update in Scala 2.12 version is that it supports Java SE 8 or later versions only. Scala 2.12 is not a binary compatible with the 2.11.x series. It’s still in Mile Stone Builds only.

33.

In Scala, Pattern Matching Follows Which Design Pattern? In Java, ‘isinstanceof’ Operator Follows Which Design Pattern?

Answer»

In SCALA, Pattern Matching follows VISITOR DESIGN Pattern. In the same way, JAVA’s ‘isinstanceof’ OPERATOR also follows Visitor Design Pattern.

In Scala, Pattern Matching follows Visitor Design Pattern. In the same way, Java’s ‘isinstanceof’ operator also follows Visitor Design Pattern.

34.

How Scala Solves Inheritance Diamond Problem Automatically And Easily Than Java 8?

Answer»

If we use Java 8’s Interface with Default METHODS, we will get Inheritance Diamond Problem. DEVELOPER has to solve it manually in Java 8. It does not PROVIDE default or automatic RESOLUTION for this problem.

In Scala, we will get same problem with Traits but Scala is very clever and solves Inheritance Diamond Problem automatically using Class LINEARIZATION concept.

If we use Java 8’s Interface with Default methods, we will get Inheritance Diamond Problem. Developer has to solve it manually in Java 8. It does not provide default or automatic resolution for this problem.

In Scala, we will get same problem with Traits but Scala is very clever and solves Inheritance Diamond Problem automatically using Class Linearization concept.

35.

What Is Guard In Scala’s For-comprehension Construct?

Answer»

In Scala, for-comprehension construct has an if clause which is used to write a condition to filter some elements and generate NEW collection. This if clause is also known as “Guard”.

If that guard is true, then add that ELEMENT to new collection. OTHERWISE, it does not add that element to original collection.

Example:- For-comprehension Guard to generate only Even numbers into new collection.

scala> val list = List(1,2,3,4,5,6,7,8,9,10)
list: List[Int] = List(1, 2, 3, 4, 5 , 6 , 7 , 8 , 9 , 10)

scala> for(l <- list if l % 2 =0 ) yield l
res0: List[Int] = List(2, 4, 6, 8, 10)

In Scala, for-comprehension construct has an if clause which is used to write a condition to filter some elements and generate new collection. This if clause is also known as “Guard”.

If that guard is true, then add that element to new collection. Otherwise, it does not add that element to original collection.

Example:- For-comprehension Guard to generate only Even numbers into new collection.

scala> val list = List(1,2,3,4,5,6,7,8,9,10)
list: List[Int] = List(1, 2, 3, 4, 5 , 6 , 7 , 8 , 9 , 10)

scala> for(l <- list if l % 2 =0 ) yield l
res0: List[Int] = List(2, 4, 6, 8, 10)

36.

What Is The Use Of ‘yield’ Keyword In Scala’s For-comprehension Construct?

Answer»

We can use ‘yield’ keyword in Scala’s for-comprehension construct. ‘for/yield’ is used to iterate a collection of elements and generates new collection of same type. It does not change the original collection. It generates new collection of same type as original collection type.

For EXAMPLE, if we use ‘for/yield’ construct to iterate a List then it generates a new List only.

scala> val list = List(1,2,3,4,5)
list: List[INT] = List(1, 2, 3, 4, 5)

scala> for(l &LT;- list) yield l*2
res0: List[Int] = List(2, 4, 6, 8, 10)

We can use ‘yield’ keyword in Scala’s for-comprehension construct. ‘for/yield’ is used to iterate a collection of elements and generates new collection of same type. It does not change the original collection. It generates new collection of same type as original collection type.

For example, if we use ‘for/yield’ construct to iterate a List then it generates a new List only.

scala> val list = List(1,2,3,4,5)
list: List[Int] = List(1, 2, 3, 4, 5)

scala> for(l <- list) yield l*2
res0: List[Int] = List(2, 4, 6, 8, 10)

37.

What Are The Major Differences Between Scala’s Auxiliary Constructors And Java’s Constructors?

Answer»

Scala’s Auxiliary constructor is almost similar to Java’s constructor with few differences.

Compared to Java’s CONSTRUCTORS, Auxiliary constructors have the FOLLOWING few differences:

  • The auxiliary constructors are called using “this” keyword.
  • All auxiliary constructor are defined with the same name that is “this”. In Java, we USE class name to define constructors.
  • Each auxiliary constructor must start with a call to a previously defined auxiliary constructor or the primary constructor.
  • We use ‘DEF’ keyword to define auxiliary constructors like method/function definition. In Java, constructor definition and Method definition is different.

Scala’s Auxiliary constructor is almost similar to Java’s constructor with few differences.

Compared to Java’s constructors, Auxiliary constructors have the following few differences:

38.

In Fp, What Is The Difference Between A Function And A Procedure?

Answer»

Both are USED to PERFORM computation, however they have one major DIFFERENCE in Functional Programming world.

A function is a computation unit without side-effect where as a Procedure is ALSO a computation unit with side-effects.

Both are used to perform computation, however they have one major difference in Functional Programming world.

A function is a computation unit without side-effect where as a Procedure is also a computation unit with side-effects.

39.

What Is A Pure Function?

Answer»

A PURE function is a function without any observable side-effects. That means it returns always same results irrespective how many times we call it with same inputs.

A pure function always gives same output for the same inputs.

For EXAMPLE:-

scala&GT; 10 + 20
res0: Int = 30
scala>
scala> 10 + 20
res0: Int = 30

Here “+” a pure function available in Int class. It gives same result 30 for same inputs 10 and 30, irrespective how many times we call it.

A pure function is a function without any observable side-effects. That means it returns always same results irrespective how many times we call it with same inputs.

A pure function always gives same output for the same inputs.

For Example:-

scala> 10 + 20
res0: Int = 30
scala>
scala> 10 + 20
res0: Int = 30

Here “+” a pure function available in Int class. It gives same result 30 for same inputs 10 and 30, irrespective how many times we call it.

40.

How Many Values Of Type Unit Have In Scala?

Answer»

In Scala, Unit is something SIMILAR to JAVA’s void keyword. It is used to represent “No VALUE exists”. It has ONE and only one value that is ().

In Scala, Unit is something similar to Java’s void keyword. It is used to represent “No value exists”. It has one and only one value that is ().

41.

How Many Values Of Type Nothing Have In Scala?

Answer»

In Scala, Nothing type have no values that is ZERO. It does not have any values. It is a subtype of all VALUE CLASSES and REFERENCE classes.

In Scala, Nothing type have no values that is zero. It does not have any values. It is a subtype of all Value classes and Reference classes.

42.

What Is Range In Scala? How To Create A Range In Scala?

Answer»

Range is a Lazy Collection in SCALA. Range is a class available in ‘scala’ package LIKE ‘scala.Range’. It is used to represent a sequence of integer values. It is an ordered sequence of integers.

Example:-

scala> 1 to 10

res0: scala.collection.immutable.Range.Inclusive = Range(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)

scala> 1 until 10

res1: scala.collection.immutable.Range = Range(1, 2, 3, 4, 5, 6, 7, 8, 9)

Range is a Lazy Collection in Scala. Range is a class available in ‘scala’ package like ‘scala.Range’. It is used to represent a sequence of integer values. It is an ordered sequence of integers.

Example:-

scala> 1 to 10

res0: scala.collection.immutable.Range.Inclusive = Range(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)

scala> 1 until 10

res1: scala.collection.immutable.Range = Range(1, 2, 3, 4, 5, 6, 7, 8, 9)

43.

How To Implement Interfaces In Scala?

Answer»

As we know from Java BACKGROUND, we use interface to define contact.

HOWEVER, there is no interface concept in Scala. EVEN, Scala doesn’t have interface keyword. Scala has a more powerful and flexible concept i.e. TRAIT for this purpose.

As we know from Java background, we use interface to define contact.

However, there is no interface concept in Scala. Even, Scala doesn’t have interface keyword. Scala has a more powerful and flexible concept i.e. trait for this purpose.

44.

What Is A Companion Object In Scala? What Is A Companion Class In Scala? What Is The Use Of Companion Object In Scala?

Answer»

In simple words, if a Scala class and object shares the same name and defined in the same source file, then that class is known as “Companion Class” and that object is known as “Companion Object”.

When we CREATE a Class by using Scala “class” keyword and Object by using Scala “object” keyword with same name and within the same source file, then that class is known as “Companion Class” and that object is known as “Companion Object”.

Example:-

Employee.scala
class Employee{ }
object Employee{ }

In Scala, The MAIN PURPOSE of Companion Object is to define apply methods and avoid using new keyword in CREATING an instance of that Companion class object.

In simple words, if a Scala class and object shares the same name and defined in the same source file, then that class is known as “Companion Class” and that object is known as “Companion Object”.

When we create a Class by using Scala “class” keyword and Object by using Scala “object” keyword with same name and within the same source file, then that class is known as “Companion Class” and that object is known as “Companion Object”.

Example:-

Employee.scala
class Employee{ }
object Employee{ }

In Scala, The main purpose of Companion Object is to define apply methods and avoid using new keyword in creating an instance of that Companion class object.

45.

What Is Object In Scala? Is It A Singleton Object Or Instance Of A Class?

Answer»

Unlike Java, Scala has two MEANINGS about ‘object’. Don’t get confuse about this, I will explain it clearly. In Java, we have only one MEANING for object that is “An instance of a class”.

Like Java, the first meaning of object is “An instance of a class”.

VAL p1 = new Person("Scala","Java")
or 
val p1 = Person("Scala","Java")

Second meaning is that object is a keyword in Scala. It is USED to define Scala Executable programs, Companion Objects, Singleton Objects etc.

Unlike Java, Scala has two meanings about ‘object’. Don’t get confuse about this, I will explain it clearly. In Java, we have only one meaning for object that is “An instance of a class”.

Like Java, the first meaning of object is “An instance of a class”.

val p1 = new Person("Scala","Java")
or 
val p1 = Person("Scala","Java")

Second meaning is that object is a keyword in Scala. It is used to define Scala Executable programs, Companion Objects, Singleton Objects etc.

46.

What Is The Main Design Decision About Two Separate Keywords: Class And Object In Scala? How Do We Define Instance Members And Static Members In Scala?

Answer»

In Scala, we USE class keyword to define instance members and object keyword to define static members. Scala does not have static keyword, but still we can define them by USING object keyword.

The MAIN design decision about this is that the clear separation between instance and static members. Loosely COUPLING between them. And other major reason is to avoid static keyword so that Scala will become a Pure-OOP Language.

In Scala, we use class keyword to define instance members and object keyword to define static members. Scala does not have static keyword, but still we can define them by using object keyword.

The main design decision about this is that the clear separation between instance and static members. Loosely coupling between them. And other major reason is to avoid static keyword so that Scala will become a Pure-OOP Language.

47.

Does A Companion Object Access Private Members Of It’s Companion Class In Scala?

Answer»

Generally, PRIVATE MEMBERS means ACCESSIBLE only WITHIN that class. However Scala’s Companion class and Companion Object has provided another feature.

In Scala, a Companion object can ACCESS private members of it’s Companion class and Companion class can access it’s Companion object’s private members.

Generally, private members means accessible only within that class. However Scala’s Companion class and Companion Object has provided another feature.

In Scala, a Companion object can access private members of it’s Companion class and Companion class can access it’s Companion object’s private members.

48.

How Do We Declare A Private Primary Constructor In Scala? How Do We Make A Call To A Private Primary Constructor In Scala?

Answer»

In Scala, we can declare a private Primary Constructor very easily. Just define a Primary Constructor as it is and ADD ‘private’ just after class NAME and before parameter LIST as shown below:

class Person private (name: String)
object Person{
 def apply(name: String) = new Person(name)
}

As it’s a private constructor, we cannot call it from outside. We should PROVIDE a factory method (that is apply method) as shown above and use that constructor INDIRECTLY.

In Scala, we can declare a private Primary Constructor very easily. Just define a Primary Constructor as it is and add ‘private’ just after class name and before parameter list as shown below:

class Person private (name: String)
object Person{
 def apply(name: String) = new Person(name)
}

As it’s a private constructor, we cannot call it from outside. We should provide a factory method (that is apply method) as shown above and use that constructor indirectly.

49.

How Does It Work Under-the-hood, When We Create An Instance Of A Class Without Using ‘new’ Keyword In Scala? When Do We Go For This Approach? How To Declare Private Constructors In Scala?

Answer»

In Scala, when we CREATE an instance of a Class without using ‘new’ KEYWORD, internally it make a call to appropriate apply method available in Companion OBJECT. Here appropriate apply method means that MATCHED with parameters.

When do we choose this option: When we need to provide private private constructor and we need to avoid using ‘new’ keyword, we can implement only apply method with same SET of parameters and allow our class users to create it without new keyword.

In Scala, when we create an instance of a Class without using ‘new’ keyword, internally it make a call to appropriate apply method available in Companion object. Here appropriate apply method means that matched with parameters.

When do we choose this option: When we need to provide private private constructor and we need to avoid using ‘new’ keyword, we can implement only apply method with same set of parameters and allow our class users to create it without new keyword.

50.

What Is Apply Method In Scala? What Is Unapply Method In Scala? What Is The Difference Between Apply And Unapply Methods In Scala?

Answer»

In Scala, apply and unapply methods play very important ROLE. They are also very useful in Play Framework in mapping and unmapping data between Form data and MODEL data.

In simple words,

apply method: To compose or assemble an object from it’s components.

unapply method: To decompose or dis-assemble an object into it’s components.

Scala’s apply method: It is used to compose an object by using its components. Suppose if we WANT to create a Person object, then use firstName and laststName two components and compose Person object as shown below.

class Person(val firstName: String, val lastName: String)
object Person{
DEF apply(firstName: String, lastName: String) 
= new Person(firstName, lastName)
}

Scala’s unapply method:

It is used to decompose an object into its components. It FOLLOWS reverse process of apply method. Suppose if we have a Person object, then we can decompose this object into it’s two components: firstName and laststName as shown below.

class Person(val firstName: String, val lastName: String)
object Person{
def apply(firstName: String, lastName: String) 
= new Person(firstName, lastName)
 
def unapply(p: Person): (String,String) 
= (p.firstName, p.lastName)
}

In Scala, apply and unapply methods play very important role. They are also very useful in Play Framework in mapping and unmapping data between Form data and Model data.

In simple words,

apply method: To compose or assemble an object from it’s components.

unapply method: To decompose or dis-assemble an object into it’s components.

Scala’s apply method: It is used to compose an object by using its components. Suppose if we want to create a Person object, then use firstName and laststName two components and compose Person object as shown below.

class Person(val firstName: String, val lastName: String)
object Person{
def apply(firstName: String, lastName: String) 
= new Person(firstName, lastName)
}

Scala’s unapply method:

It is used to decompose an object into its components. It follows reverse process of apply method. Suppose if we have a Person object, then we can decompose this object into it’s two components: firstName and laststName as shown below.

class Person(val firstName: String, val lastName: String)
object Person{
def apply(firstName: String, lastName: String) 
= new Person(firstName, lastName)
 
def unapply(p: Person): (String,String) 
= (p.firstName, p.lastName)
}