Scala Interview Questions
Scala Interview Questions
Scala Map is a collection of key value pairs wherein the value in a map can be retrieved using
the key. Values in a Scala Map are not unique but the keys are unique. Scala supports two
kinds of maps- mutable and immutable. By default, Scala supports immutable map and to make
use of the mutable map, programmers have to import the scala.collection.mutable.Map class
explicitly. When programmers want to use mutable and immutable map together in the same
program then the mutable map can be accessed as mutable.map and the immutable map can
just be accessed with the name of the map.
If you would like more information about Big Data careers, please click the orange "Request Info" button
on top of this page.
2) What is the advantage of using Scala over other functional programming
languages?
As the name itself indicates Scala meaning Scalable Language, its high scalable,
maintainability, productivity and testability features make it advantageous to use Scala.
Singleton and Companion Objects in Scala provide a cleaner solution unlike static in other JVM
languages like Java.
It eliminates the need for having a ternary operator as if blocks’, ‘for-yield loops’, and ‘code’ in
braces return a value in Scala.
3) What is the advantage of companion objects in Scala?
Classes in Scala programming language do not have static methods or variables but rather they
have what is known as a Singleton object or Companion object. The companion objects in turn
are compiled to classes which have static methods.
A singleton object in Scala is declared using the keyword object as shown below –
object Main {
def sayHello () {
println ("Hello!");
In the above code snippet, Main is a singleton object and the method sayHello can be invoked
using the following line of code –
If a singleton object has the same name as that of the class then it is known as a Companion
object and it should be defined in the same source file as that of the class.
class Main {
def sayHelloWorld() {
println("Hello World");
}
object Main {
def sayHello() {
println("Hello!");
One more example to describe functionality of Option type is to use it as a method return type,
which tells the caller that the method can return a string or it can return none.
12) What’s the difference ‘Nil’, ‘Null’, ‘None’ and ’Nothing’ in Scala?
Null – It’s a sub-type of AnyRef type in Scala Types hierarchy. As Scala runs on JVM, it uses
NULL to provide the compatibility with Java null keyword, or in Scala terms, to provide type for
null keyword, Null type exists. It represents the absence of type information for complex types
that are inherited from AnyRef.
Nothing – It’s a sub-type of all the types exists in Scala Types hierarchy. It helps in providing the
return type for the operations that can affect a normal program’s flow. It can only be used as a
type, as instantiation of nothing cannot be done. It incorporates all types under AnyRef and
AnyVal. Nothing is usually used as a return type for methods that have abnormal termination
and result in an exception.
Nil – It’s a handy way of initializing an empty list since, Nil, is an object, which extends List
[Nothing].
None – In programming, there are many circumstances, where we unexpectedly received null
for the methods we call. In java these are handled using try/catch or left unattended causing
errors in the program. Scala provides a very graceful way of handling those situations. In cases,
where you don’t know, if you would be able to return a value as expected, we can use Option
[T]. It is an abstract class, with just two sub-classes, Some [T] and none. With this, we can tell
users that, the method might return a T of type Some [T] or it might return none.
13) What is a Scala Trait?
A trait is a special kind of Class that enables the use of multiple inheritance. Although a trait can
extend only one class, but a class can have multiple traits. However, unlike classes, traits
cannot be instantiated.
15) What are the considerations you need to have when using Scala
streams?
Streams in Scala are a type of lazy collection, which are created using starting element and
then recursively generated using those elements. Streams are like a List, except that, elements
are added only when they are accessed, hence “lazy”. Since streams are lazy in terms of
adding elements, they can be unbounded also, and once the elements are added, they are
cached. Since Streams can be unbounded, and all the values are computed at the time of
access, programmers need to be careful on using methods which are not transformers, as it
may result in java.lang.OutOfMemoryErrors.
stream.max
stream.size
stream.sum
16) What do you understand by diamond problem and how does Scala
resolve this?
Multiple inheritance problem is referred to as the Deadly diamond problem or diamond problem.
The inability to decide on which implementation of the method to choose is referred to as the
Diamond Problem in Scala. Suppose say classes B and C both inherit from class A, while class
D inherits from both class B and C. Now while implementing multiple inheritance if B and C
override some method from class A, there is a confusion and dilemma always on which
implementation D should inherit. This is what is referred to as diamond problem. Scala resolves
diamond problem through the concept of Traits and class linearization rules.
Unapply method – Used to decompose an object from its components. It follows the reverse
process of apply method. So if you have an employee object, it can be decomposed into two
components- firstName and lastName.
24) Can a companion object in Scala access the private members of its
companion class in Scala?
According to the private access specifier, private members can be accessed only within that
class but Scala’s companion object and class provide special access to private members. A
companion object can access all the private members of a companion class. Similarly, a
companion class can access all the private members of companion objects.