Intermediate Scala Interview Questions and Answers

Question 1
What is the use of lazy val ?

In Scala we use lazy keyword to define a value as being lazy, meaning that it is evaluated only once, when it’s accessed.

1

lazyvalmyval="Hello"

Question 2
What is a partially applied function ?

In Scala we do not have to provide all expected arguments to function, if we only pass few of them we call it partially applied function.

1

defadd(a:Int,b:Int)=a+b

We can partially applied function using wildcard operator _

1

2

scala>valpartialAdd=add(10,_:Int)

partialAdd:Int=>Int=

partialAdd is now function of type Int => Int

Question 3
What are by-name parameters ?

Mechanism that enables to define parameter which will be evaluated only when it’s used, and each time it’s accessed it will be recomputed.
We using => to declare by name parameter.

1

deff(param:=&gt;Int):(Int,Int)=(param,param)

1

2

scala>f(Random.nextInt())

res2:(Int,Int)=(-806238482,954063161)

Question 4
What is a partial function ?

Function that is only defined for certain values. Partial function extends trait PartialFunction which provides method isDefinedAt that can be used to check if function is defined for given argument.

Question 5
Difference between a method and a function ?

Scala function is an object that extends Function trait.
Scala methods are a members of a class, trait or singleton object, and defined using def keyword.

Question 6
What is tail recursion ?

Optimization technique that can be apply to recursive functions performed by compiler which eliminates function call so that current stack space can be reused. In order for the function to be optimized for tail recursion the last statement must be recursive call.

Question 7
What is the eta expansion ?

Mechanism which enables to automatically convert methods into functions.
We can convert method to function by writing method name followed by underscore.

1

2

3

4

5

scala>deff(s:String)=s

f:(s:String)String

scala>f_

res0:String=>String=<function1>

Question 8
Explain concept of implicits in Scala

Scala’s implicit comes in 2 flavours:

Implicit conversions:

It’s functionality which enables to convert one type to another using method with implicit modifier at the beginning.

Implicit parameters:

The parameters on a method can be marked implicit if not provided they will be taken from context.

Question 9
What is parametric polymorphism ?

Parametric polymorphism allows for methods, classes, traits to take type parameters.
We define method f where A is a type parameter.

Question 11
What is a Future ?

Future[T] is a container for a value of type T, which may or may not be available at some point in time.

1

2

3

Future{

10

}

Question 12
What is a TypeTag ?

Because of type erasure, generic types only exist during compilation time, during runtime, there is no information about the type a generic class was parametrized with. TypeTag enables to preserve type information so that it’s available during runtime.

Question 13
What is a covariance ?

Let’s assume that Box is a generic class, if S is a subtype of T then
Box[S] is a subtype of Box[T]. We denote covariance by using + in front of type parameters.

1

classBox[+A]

Question 14
What is a monad in Scala?

A monad is a parameterized type with two operations, map and flatMap.
It provides a standard interface for composing and sequencing operations.