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.

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.

We can partially applied function using wildcard operator _

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.

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.

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 10
What is Scala’s “for-comprehensions” ?

Scala’s “for comprehensions” are syntactic sugar for composition of multiple operations with map , flatMap , filter, withFilter .

This for comprehension is translated into following calls to map and flatMap.

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.

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.

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.

Some commonly seen monads: List, Option, Future.

Leave a Reply

Your email address will not be published. Required fields are marked *