Edureka - Scala Interview Questions
Edureka - Scala Interview Questions
Web Clip
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: 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.
Q4. Mention the types of Variables in Scala? And What is the difference
between them?
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.
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.
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
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.
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.
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.
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
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.
2 println("Hello World")
3 }
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:
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.
upon the value of one or more variables declared outside the closure function.
Example:
Here the only variable used in the function body, i * 10 , is i, which is defined
as a parameter to the function
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 }
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
}
Ans: A few scenarios where Scala differs from Java are as follows:
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
1 //Syntax:
2 object {
5 }
6 }
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.
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
{
4 class InnerMost {
5 f() // OK
6 }
7 }
9
}
Protected:
A protected member is only Accessible from Subclasses of the class in
which the member is defined.
1 package p
2 class Super {
4 }
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:
1 class Outer {
2 class Inner {
4 class InnerMost {
5 f() // OK
6 }
7 }
9 }
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
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
3 Or
4 (_:Int)*(_Int)
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
Ans: The three important and default Packages in Scala are as follows:
Alphanumeric identifiers
Operator identifiers
Mixed identifiers
Literal identifiers
2 object Main
3 {
4 //Main method
6 {
7 //Valid Identifiers
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
}
1 object add {
3 var sum:Int = 0
4 sum = a + b
5 return sum
6 }
7 }
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
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.
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.
Example:
1 object Demo {
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
}
1 object Demo {
3 var a = 10;
5 while( true ){
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
}
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.
1 string1.concat(string2);
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.
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 {
6 try {
8 } catch {
10
println("Missing file exception")
11
}
12
case ex: IOException = {
13
println("IO Exception")
14
}
15
}
16
}
17
}
Ans:
Try the following example program, which shows how to match against an
integer value.
1 object Demo {
3 println(matchTest(3))
4 }
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
}
Ans:
4 result *= j
5 result
6 }
7 for (i - 1 to 10)
import scala.collection.mutable.Queue
1 import scala.collection.mutable.Queue
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.