0% found this document useful (0 votes)
142 views

Edureka - Scala Interview Questions

This document contains an article on Scala interview questions and answers. It begins with introductory questions about what Scala is and its key features. It then covers intermediate questions about concepts like higher-order functions, closures, traits, and more. The questions progress from beginner to intermediate level.

Uploaded by

Rahul Jain
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
142 views

Edureka - Scala Interview Questions

This document contains an article on Scala interview questions and answers. It begins with introductory questions about what Scala is and its key features. It then covers intermediate questions about concepts like higher-order functions, closures, traits, and more. The questions progress from beginner to intermediate level.

Uploaded by

Rahul Jain
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

10/20/2019 My Notebook - Evernote Web

My Notebook Only you Share

Last edited on Oct 20, 2019 Insert

Top 45 Scala Interview Questions And Answers |


Edureka

Web Clip

Most Important Scala Interview


Questions to Prepare in 2019

Published on Jun 03,2019 4.3K Views


Ravi Kiran Tech Enthusiast working as a Research Analyst at Edureka. Curious
about learning... Tech Enthusiast working as a Research Analyst at Edureka.
Curious about learning more about Data Science and Big-Data Hadoop.

myMock Interview Service for Real Tech


Jobs
Scala, the Unrivalled Programming Language with its phenomenal capabilities
in handling Petabytes of Big-data with ease. Scala is dominating the well-
enrooted languages like Java and Python. This Scala Interview Questions
article will cover the crucial questions that can help you bag a job. I have lined
up the questions as below.

Scala Interview Questions: Beginner


Level

Q1 What is Scala?
https://www.evernote.com/u/0/client/web#?b=9b8e3b22-552e-4b1e-a2d0-6c7c17d392e4&n=88ced9e4-ae29-482d-ba9e-554004a00084&s=s635& 1/22
10/20/2019 My Notebook - Evernote Web
Q1. What is Scala?

Ans: Scalais a Java-based Hybrid programming language which is the fusion


of both Functional and Object-Oriented Programming Language features. It
can integrate itself with Java Virtual Machine and compile the code written.

Q2. Explain how Scala is both Functional and Object-oriented


Programming Language?

Ans: Scala treats every single value as an Object which even includes
Functions. Hence, Scala is the fusion of both Object-oriented and Functional
programming features.

Q3.Write a few Frameworks of Scala

Ans: Some of the Frameworks supported by Scala are as follows:


AkkaFramework
Spark Framework
PlayFramework
Scalding Framework
Neo4j Framework
Lift Framework
Bowler Framework

Q4. Mention the types of Variables in Scala? And What is the difference
between them?

Ans: The Variables in Scala are mainly of two types:

Mutable Variables
We Declare Mutable Variables by using the var keyword.
The values in the Mutable Variables support Changes

Immutable Variables
https://www.evernote.com/u/0/client/web#?b=9b8e3b22-552e-4b1e-a2d0-6c7c17d392e4&n=88ced9e4-ae29-482d-ba9e-554004a00084&s=s635& 2/22
10/20/2019 My Notebook - Evernote Web
Immutable Variables
We declare Immutable Variables using the val keyword.
The values in Immutable Variables do not support changes.

Q5. Explain Streams in Scala.

Ans: In simple words, we define Stream as a Lazy list which evaluates the
elements only when it needs to. This sort of lazy computation enhances the
Performance of the program.

Q6. Mention the Advantages of Scala

Ans: Some of the major Advantages of Scala are as follows:


It is highly Scalable
It is highly Testable
It is highly Maintainable and Productive
It facilitates Concurrent programming
It is both Object-Oriented and Functional
It has no Boilerplate code
Singleton objects are a cleaner solution than Static
Scala Arrays use regular Generics
Scala has Native Tuples and Concise code

Q7. Explain the Operators in Scala

Ans: The following are the Operators in Scala:

Arithmetic Operators
Relational Operators
Logical Operators
Bitwise Operators
Assignment Operators

https://www.evernote.com/u/0/client/web#?b=9b8e3b22-552e-4b1e-a2d0-6c7c17d392e4&n=88ced9e4-ae29-482d-ba9e-554004a00084&s=s635& 3/22
10/20/2019 My Notebook - Evernote Web

Q8. What is Recursion tail in Scala?

Ans: ‘Recursion’ is a function that calls itself. For example, a function ‘A’ calls
function ‘B’, which calls the function ‘C’. It is a technique used frequently in
Functional programming. In order for a Tail recursive, the call back to the
function must be the last function to be performed.

Q9. Explain the use of Tuples in Scala?

Ans: Scala tuples combine a Finite number of items together so that the
programmer can Pass a tuple around as a Whole. Unlike an Array or List, a
tuple is Immutable and can hold objects with different Datatypes.

Q10. How is a Class different from an Object?

Ans: Class combines the data and its methods whereas an Object is one
particular Instance in a class.

So, with this, we finished some questions on the Beginner Level. Now, Let us
move to the next level of interview questions which happen to be the Scala
Intermediate Level Interview Questions.

Scala Interview Questions:


Intermediate Level

https://www.evernote.com/u/0/client/web#?b=9b8e3b22-552e-4b1e-a2d0-6c7c17d392e4&n=88ced9e4-ae29-482d-ba9e-554004a00084&s=s635& 4/22
10/20/2019 My Notebook - Evernote Web

Q11. Why do we need App in Scala?

Ans: App is a helper class that holds the main method and its Members
together. The App trait can be used to quickly turn Objects into Executable
programs. We can have our classes extend App to render the executable code.

1 object Edureka extends App{

2 println("Hello World")

3 }

Q12. What are Higher-order functions?

Ans: A Higher-order function is a function that does at least one of the


following: takes one or more Functions as Arguments, returns a Function as
its result.

Q13. Explain the scope provided for variables in Scala.

Ans: There are three different scopes depending upon their use. Namely:

Fields:

Fields are variables declared inside an object and they can be accessed
anywhere inside the program depending upon the access modifiers.
Fields can be declared using var as well as val.

Method Parameters:

Method parameters are strictly Immutable. Method parameters are


mainly used to Pass values to the methods. These are accessed inside a
method, but it is possible to access them from outside the method
provided by a Reference.

Local Variables:
Local variables are declared inside a method and they are accessible
only inside the method. They can be accessed if you return them from
the method.

Q14. What is a Closure?

Ans: Closure is considered as a Function whose return value is Dependent


https://www.evernote.com/u/0/client/web#?b=9b8e3b22-552e-4b1e-a2d0-6c7c17d392e4&n=88ced9e4-ae29-482d-ba9e-554004a00084&s=s635& 5/22
10/20/2019 My Notebook - Evernote Web

upon the value of one or more variables declared outside the closure function.

Apache Spark and Scala Certification


Training
Instructor-led Sessions
Real-life Case Studies
Assessments
Lifetime Access

Example:

1 val multiplier = (i:Int) => i * 10

Here the only variable used in the function body, i * 10 , is i, which is defined
as a parameter to the function

Q15. Explain Traits in Scala.

Ans: A Trait can be defined as a unit which Encapsulates the method and its
variables or fields. The following example will help us understand in a better
way.

1 trait Printable{

2 def print()

3 }

4 class A4 extends Printable{

5 def print(){

6 println("Hello")

https://www.evernote.com/u/0/client/web#?b=9b8e3b22-552e-4b1e-a2d0-6c7c17d392e4&n=88ced9e4-ae29-482d-ba9e-554004a00084&s=s635& 6/22
10/20/2019 My Notebook - Evernote Web

7 }

8 }

9 object MainObject{

10
def main(args:Array[String]){

11
var a = new A4()

12
a.print()

13
}

14
}

Q16. Mention how Scala is different from Java

Ans: A few scenarios where Scala differs from Java are as follows:

All values are treated as Objects.


Scala supports Closures
Scala Supports Concurrency.
It has Type-Inference.
Scala can support Nested functions.
It has DSL support [Domain Specific Language]
Traits

Q17. Explain extend Keyword

Ans: You can extend a base Scala class and you can design an Inherited class
in the same way you do it in Java by using extends keyword, but there are two
restrictions: method Overriding requires the override keyword, and only the
Primary constructor can pass parameters to the base Constructor. Let us
understand by the following example

1 println("How to extend abstract class Parent and define a sub-class of Parent called
Child")
2
class Child=(name:String)extends Parent(name){
3
override def printName:Unit= println(name)
4
https://www.evernote.com/u/0/client/web#?b=9b8e3b22-552e-4b1e-a2d0-6c7c17d392e4&n=88ced9e4-ae29-482d-ba9e-554004a00084&s=s635& 7/22
10/20/2019 My Notebook - Evernote Web

}
5
object Child {
6
def apply(name:String):Parent={
7
new Child(name)
8
}
9

Q18. Explain implicit classes with syntax


Ans: Implicit classes allow Implicit conversations with the class’s Primary
constructor when the class is in scope. Implicit class is a class marked with the
“implicit” keyword. This feature was introduced in with Scala 2.10 version.

1 //Syntax:

2 object {

3 implicit class Data type) {

4 def Unit = xyz

5 }

6 }

Q19. Explain the access Modifiers available in Scala

Ans: There are mainly three access Modifiers available in Scala. Namely,

Private:
The Accessibility of a private member is restricted to the Class or the
Object in which it declared.

The following program will explain this in detail.

1 class Outer {

2 class Inner {
https://www.evernote.com/u/0/client/web#?b=9b8e3b22-552e-4b1e-a2d0-6c7c17d392e4&n=88ced9e4-ae29-482d-ba9e-554004a00084&s=s635& 8/22
10/20/2019 My Notebook - Evernote Web
{

3 private def f() { println("f") }

4 class InnerMost {

5 f() // OK

6 }

7 }

8 (new Inner).f() // Error: f is not accessible

9
}

Protected:
A protected member is only Accessible from Subclasses of the class in
which the member is defined.

The following program will explain this in detail.

1 package p

2 class Super {

3 protected def f() { println("f") }

4 }

5 class Sub extends Super {

6 f()

7 }

8 class Other {

https://www.evernote.com/u/0/client/web#?b=9b8e3b22-552e-4b1e-a2d0-6c7c17d392e4&n=88ced9e4-ae29-482d-ba9e-554004a00084&s=s635& 9/22
10/20/2019 My Notebook - Evernote Web

9
(new Super).f() // Error: f is not accessible

10
}

11
}

Public:

Unlike Private and Protected members, it is not required to specify


Public keyword for Public members. There is no explicit modifier for
public members. Such members can be accessed from Anywhere.

Following is the example code snippet to explain Public member

1 class Outer {

2 class Inner {

3 def f() { println("f") }

4 class InnerMost {

5 f() // OK

6 }

7 }

8 (new Inner).f() // OK because now f() is public

9 }

Q20. What is a Monad in Scala?

Ans: A Monad is an object that wraps another object. You pass the Monad
mini-programs, i.e functions, to perform the data manipulation of the
underlying object, instead of manipulating the object directly. Monad
chooses how to apply the program to the underlying object.

https://www.evernote.com/u/0/client/web#?b=9b8e3b22-552e-4b1e-a2d0-6c7c17d392e4&n=88ced9e4-ae29-482d-ba9e-554004a00084&s=s6… 10/22
10/20/2019 My Notebook - Evernote Web

Q21. Explain the Scala Anonymous Function.

Ans: In the Source code, Anonymous functions are called ‘Function literals’
and at run time, function literals are instantiated into objects called Function
values. Scala provides a relatively easy Syntax for defining Anonymous
functions.

1 //Syntax

2 (z:Int, y:Int)=> z*y

3 Or

4 (_:Int)*(_Int)

Q22. How do I Append data in a list?

Ans: In Scala to Append into a List, We have the following methods:

1 use “:+” single value

2 var myList = List.empty[String]

3 myList :+= "a"

Q23. Why Scala prefers Immutability?

Ans: Scala prefers Immutability in design and in many cases uses it as


default. Immutability can help when dealing with Equality issues or
Concurrent programs.

https://www.evernote.com/u/0/client/web#?b=9b8e3b22-552e-4b1e-a2d0-6c7c17d392e4&n=88ced9e4-ae29-482d-ba9e-554004a00084&s=s63… 11/22
10/20/2019 My Notebook - Evernote Web

Q24. Give some examples of Packages in Scala

Ans: The three important and default Packages in Scala are as follows:

Java.lang._ : Java.lang._ package in Java. Provides classes that are


fundamental to the design of the Java programming language.
Java.io._ : Java.io._ Package used to import every class in Scala for input-
output resources.
PreDef: Predef provides type aliases for types which are commonly used,
such as the immutable collection types Map, Set, and the List constructors

Q25. Why is an Option used in Scala?

Ans: Option in Scala is used to Wrap the Missing value.

Q26. Mention the Identifiers in Scala.

Ans: There are four types of Scala Identifiers:

Alphanumeric identifiers
Operator identifiers
Mixed identifiers
Literal identifiers

1 //Scala program to demonstrate Identifiers in Scala.

2 object Main

3 {

4 //Main method

5 def main(args: Array[String])

6 {

7 //Valid Identifiers

8 var 'name = "Hari"'

9 var age = 20;

10
var Branch = "Computer Science"

11
https://www.evernote.com/u/0/client/web#?b=9b8e3b22-552e-4b1e-a2d0-6c7c17d392e4&n=88ced9e4-ae29-482d-ba9e-554004a00084&s=s6… 12/22
10/20/2019 My Notebook - Evernote Web
11
println()

12
println()

13
println()

14
}

15
}

Q27. How do you define a function in Scala?

Ans: def keyword is used to define the Function in Scala.

1 object add {

2 def addInt( a:Int, b:Int ) : Int = {

3 var sum:Int = 0

4 sum = a + b

5 return sum

6 }

7 }

Q28. How is the Scala code compiled?

Ans: Code is written in Scala IDE or a Scala REPL, Later, the code is
converted into a Byte code and transferred to the JVM or Java Virtual
Machine for compilation.

https://www.evernote.com/u/0/client/web#?b=9b8e3b22-552e-4b1e-a2d0-6c7c17d392e4&n=88ced9e4-ae29-482d-ba9e-554004a00084&s=s6… 13/22
10/20/2019 My Notebook - Evernote Web

Q29. Explain the functionality of Yield.

Ans: Yield is used with a loop, Yield produces a value for each iteration.
Another way to do is to use map/flatMap and filter with nomads.

1 for (i <- 1 to 5) yield i

Q30. Differentiate between Null, Nil, None and Nothing

Ans: They appear similar but different in their behaviour:

Null Nil None Nothing

Null represents the Nil denotes None is the value of Nothing is lowest
absence of a the end a an Option with no type in type
value. List value. System.

Q31. Explain If-Else-If terminology

Ans: If-Else-If statement executes one of the three statements.

Example:

1 object Demo {

2 def main(args: Array[String]) {

3 var x = 30;

4 if( x == 10 ){

5 println("Value of X is 10")

6 } else if( x == 20 ){

7 println("Value of X is 20");

8 } else if( x == 30 ){

9 println("Value of X is 30");

10
} else{

https://www.evernote.com/u/0/client/web#?b=9b8e3b22-552e-4b1e-a2d0-6c7c17d392e4&n=88ced9e4-ae29-482d-ba9e-554004a00084&s=s6… 14/22
10/20/2019 My Notebook - Evernote Web

11
println("This is else statement");

12
}

13
}

14
}

Q32. Describe Loops in Scala.

Ans: There are mainly three types of loops in Scala.

While Loop: Repeats a statement or group of statements while a given


condition is true. It tests the condition before executing the loop body.
Do-While: Like a while statement, except that it tests the condition at
the end of the loop body.
For: Executes a sequence of statements multiple times and abbreviated
the code that manages the loop variable.
Break: Break is a loop control statement which Terminates the loop
statement and transfers execution to the statement immediately
following the loop.

Q33. Generate an Infinite loop

Ans: A loop becomes an Infinite loop if a condition never becomes false. If


you are using Scala, the while loop is the best way to implement an infinite
loop.

The following program implements an infinite loop.

1 object Demo {

2 def main(args: Array[String]) {

3 var a = 10;

4 //An infinite loop.

5 while( true ){

6 println( "Value of a: " + a );

7 }

https://www.evernote.com/u/0/client/web#?b=9b8e3b22-552e-4b1e-a2d0-6c7c17d392e4&n=88ced9e4-ae29-482d-ba9e-554004a00084&s=s6… 15/22
10/20/2019 My Notebook - Evernote Web

8 }

9
}

Q34. What is the Syntax for function declaration in Scala?

Ans: The Syntax for function declaration is as follows:

1 def functionName ([list of parameters]) : [return type] = {

2 function body

3 return [expression]

4 }

Here, the return type is any valid Scala data type and we separate the list of
parameters by comma and list of parameters and return type are optional.
Very similar to Java, we use a return statement along with an expression in
case function returns a value.

Q35. How do I Concatenate two Strings?

Ans: There are three methods to perform string concatenation in Scala

1 string1.concat(string2);

2 "My name is ".concat("Zara");

3 "Hello," + " world" + "!"

Q36. Explain any five string methods.

Ans: Following are few String Methods in Scala.

String trim(): Returns a copy of the string, with leading and trailing
whitespace omitted.
String toUpperCase: Converts all of the characters in this String to
https://www.evernote.com/u/0/client/web#?b=9b8e3b22-552e-4b1e-a2d0-6c7c17d392e4&n=88ced9e4-ae29-482d-ba9e-554004a00084&s=s6… 16/22
10/20/2019 My Notebook - Evernote Web
String toUpperCase: Converts all of the characters in this String to
upper case using the rules of the given Locale.
Char[] to CharArray(): Converts this string to a new character
array.
String[] split(String regex): Splits this string around matches of the
given regular expression.
Int length(): returns the length of this string.

Q37. Explain how to create Arrays

Ans: We use the following methods to create arrays in Scala:

We use ofDim to declare multidimensional arrays.

1 var myMatrix = ofDim[Int](3,3)

We use Range() method to generate an array containing a sequence of


increasing integers in a given range.
def range( start: Int, end: Int, step: Int ): Array[Int]

1 range (10, 20, 2)

Q38. What is Map in Scala?

Ans: Map is a collection of key/value pairs. Scala retrieves a Value based on


its Key.

1 val colors = Map("red" -> "#FF0000", "azure" -> "#F0FFFF")

Q39. Explain Exception Handling in Scala

Ans: Throw Exception: Throwing an exception looks the same as in Java. You
create an exception object and then you throw it with the throw keyword as
follows.
Throw new IllegalArgumentException
https://www.evernote.com/u/0/client/web#?b=9b8e3b22-552e-4b1e-a2d0-6c7c17d392e4&n=88ced9e4-ae29-482d-ba9e-554004a00084&s=s6… 17/22
10/20/2019 My Notebook - Evernote Web

Catching an Exception:

Scala allows you to try/catch any exception in a single block and then perform
pattern matching against it using case blocks. Try the following example
program to handle the exception.

Example:

1 import java.io.FileReader

2 import java.io.FileNotFoundException

3 import java.io.IOException

4 object Demo {

5 def main(args: Array[String]) {

6 try {

7 val f = new FileReader("input.txt")

8 } catch {

9 case ex: FileNotFoundException ={

10
println("Missing file exception")

11
}

12
case ex: IOException = {

13
println("IO Exception")

14
}

15
}

16
}

17
}

So, with this, we finished some questions on the Intermediate Level.


https://www.evernote.com/u/0/client/web#?b=9b8e3b22-552e-4b1e-a2d0-6c7c17d392e4&n=88ced9e4-ae29-482d-ba9e-554004a00084&s=s6… 18/22
10/20/2019 My Notebook - Evernote Web
So, with this, we finished some questions on the Intermediate Level.
Now, Let us move to the next level of interview questions which happen to be
the Advanced Level Interview Questions.

Scala Interview Questions: Advanced


Level

Q40. Explain Pattern Matching in Scala through an example

Ans:

A Pattern match includes a sequence of alternatives, each starting with the


Keyword case. Each alternative includes a Pattern and one or more
Expressions, Scala evaluates whenever a pattern matches. An arrow symbol
=> separates the pattern from the expressions.

Try the following example program, which shows how to match against an
integer value.

1 object Demo {

2 def main(args: Array[String]) {

3 println(matchTest(3))

4 }

5 def matchTest(x: Int): String = x match {

https://www.evernote.com/u/0/client/web#?b=9b8e3b22-552e-4b1e-a2d0-6c7c17d392e4&n=88ced9e4-ae29-482d-ba9e-554004a00084&s=s6… 19/22
10/20/2019 My Notebook - Evernote Web

6 case 1 = "one"

7 case 2 = "two"

8 case _ = "other"

9
}

10
}

Q41. Explain Extractors in Scala

Apache Spark and Scala Certification


Training
Weekday / Weekend Batches
Ans: An Extractor in Scala is an object that has a method called unapply as
one of its members. The purpose of that unapply method is to match the value
and take it apart.

Q42. What is the result of x+y*z and why?

Ans: Similar to any other programming language, Scala also follows


Presidency and Priority tables. According to the tables, Scala Performs the
operations as follows.
Scala evaluates y*z first.
Then adds (y*z) with x

Q43. What is an Auxiliary constructor

Ans: We use Auxiliary constructor in Scala for Constructor Overloading. The


Auxiliary Constructor must call either previously defined auxiliary
constructors or primary constructor in the first line of its body.

Q44. Explain recursion through a program


https://www.evernote.com/u/0/client/web#?b=9b8e3b22-552e-4b1e-a2d0-6c7c17d392e4&n=88ced9e4-ae29-482d-ba9e-554004a00084&s=s6… 20/22
10/20/2019 My Notebook - Evernote Web
Q . pa ecu s o t oug a p og a

Ans:

1 def factorial_loop(i: BigInt): BigInt = {

2 var result = BigInt(1)

3 for (j- 2 to i.intValue)

4 result *= j

5 result

6 }

7 for (i - 1 to 10)

8 format("%s: %sn", i, factorial_loop(i))

Q45. Explain Que with example

Ans: Queue is a Data Structure similar to Stack except, it follows First In


First Out procedure for data processing. In Scala, to work with Queues, you
need to import a library called,

import scala.collection.mutable.Queue

1 import scala.collection.mutable.Queue

2 val empty = new Queue[Int]

So, with this, we come to an end of this Scala Interview Questions article. I
hope we sparked a little light upon your knowledge about Scala, Its features
and the various types of operations that can be performed using Scala.

This article based on Apache Spark and Scala Certification Training is


d i dt f th Cl d H d dS kD l
https://www.evernote.com/u/0/client/web#?b=9b8e3b22-552e-4b1e-a2d0-6c7c17d392e4&n=88ced9e4-ae29-482d-ba9e-554004a00084&s=s6… 21/22

You might also like