Categories
Scala Interview Questions and Answers
Here are Scala interview questions and answers for freshers as well as experienced candidates to get their dream job.
1. What is Scala?
Scala is an object functional programming and scripting language for general software applications designed to express solutions in a concise manner.
2. What is a ‘Scala map’?
Scala map is a collection of key or value pairs. Based on its key any value can be retrieved. Values are not unique but keys are unique in the Map.
3. What is a ‘Scala set’? What are methods through which operation sets are expressed?
Scala set is a collection of pairwise elements of the same type. Scala set does not contain any duplicate elements. There are two kinds of sets, mutable and immutable.
4. In what ways Scala is better than other programming language?
1) The arrays uses regular generics, while in other language, generics are bolted on as an afterthought and are completely separate but have overlapping behaviours with arrays.
2) Scala has immutable “val” as a first class language feature. The “val” of scala is similar to Java final variables. Contents may mutate but top reference is immutable.
3) Scala lets ‘if blocks’, ‘for-yield loops’, and ‘code’ in braces to return a value. It is more preferable, and eliminates the need for a separate ternary operator.
4) Singleton has singleton objects rather than C++/Java/ C# classic static. It is a cleaner solution
5) Persistent immutable collections are the default and built into the standard library.
6) It has native tuples and a concise code
7) It has no boiler plate code
2) Scala has immutable “val” as a first class language feature. The “val” of scala is similar to Java final variables. Contents may mutate but top reference is immutable.
3) Scala lets ‘if blocks’, ‘for-yield loops’, and ‘code’ in braces to return a value. It is more preferable, and eliminates the need for a separate ternary operator.
4) Singleton has singleton objects rather than C++/Java/ C# classic static. It is a cleaner solution
5) Persistent immutable collections are the default and built into the standard library.
6) It has native tuples and a concise code
7) It has no boiler plate code
5. What is the advantage of Scala?
1) Less error prone functional style
2) High maintainability and productivity
3) High scalability
4) High testability
5) Provides features of concurrent programming
2) High maintainability and productivity
3) High scalability
4) High testability
5) Provides features of concurrent programming
6. When can you use traits?
1) If the behaviour will not be reused, then make it a concrete class. Anyhow it is not a reusable behaviour.
2) In order to inherit from it in Java code, an abstract class can be used.
3) If efficiency is a priority then lean towards using a class
4) Make it a trait if it might be reused in multiple and unrelated classes. In different parts of the class hierarchy only traits can be mixed into different parts.
5) You can use abstract class, if you want to distribute it in compiled form and expects outside groups to write classes inheriting from it.
2) In order to inherit from it in Java code, an abstract class can be used.
3) If efficiency is a priority then lean towards using a class
4) Make it a trait if it might be reused in multiple and unrelated classes. In different parts of the class hierarchy only traits can be mixed into different parts.
5) You can use abstract class, if you want to distribute it in compiled form and expects outside groups to write classes inheriting from it.
7. What is ‘scala trait’ in scala?
‘Traits’ are used to define object types specified by the signature of the supported methods. Scala allows to be partially implemented but traits may not have constructor parameters. A trait consists of method and field definition, by mixing them into classes it can be reused.
8. What is recursion tail in scala?
‘Recursion’ is a function that calls itself. 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.
9. Mention the difference between an object and a class ?
A class is a definition for a description. It defines a type in terms of methods and composition of other types. A class is a blueprint of the object. While, an object is a singleton, an instance of a class which is unique. An anonymous class is created for every object in the code, it inherits from whatever classes you declared object to implement.
10. What are the Scala variables?
Values and variables are two shapes that come in Scala. A value variable is constant and cannot be changed once assigned. It is immutable, while a regular variable, on the other hand, is mutable, and you can change the value.
The two types of variables are
var myVar : Int=0;
val myVal: Int=1;
The two types of variables are
var myVar : Int=0;
val myVal: Int=1;
11. What is a closure in Scala?
A closure is a function whose return value depends on the value of the variables declared outside the function.
12. What are implicit parameters in Scala?
Implicit parameter is the way that allows parameters of a method to be “found”. It is similar to default parameters, but it has a different mechanism for finding the “default” value. The implicit parameter is a parameter to method or constructor that is marked as implicit. This means if a parameter value is not mentioned then the compiler will search for an “implicit” value defined within a scope.
13. What is function currying in Scala?
Currying is the technique of transforming a function that takes multiple arguments into a function that takes a single argument Many of the same techniques as language like Haskell and LISP are supported by Scala. Function currying is one of the least used and misunderstood one.
14. What is the use of tuples in scala?
Scala tuples combine a fixed number of items together so that they can be passed around as whole. A tuple is immutable and can hold objects with different types, unlike an array or list.
15. What is Case Classes?
Case classes provides a recursive decomposition mechanism via pattern matching, it is a regular classes which export their constructor parameter. The constructor parameters of case classes can be accessed directly and are treated as public values.
16. What are option, some and none in scala?
‘Option’ is a Scala generic type that can either be ‘some’ generic value or none. ‘Queue’ often uses it to represent primitives that may be null.
17. What is the difference between var and value?
In scala, you can define a variable using either a, val or var keywords. The difference between val and var is, var is much like java declaration, but val is little different. We cannot change the reference to point to another reference, once the variable is declared using val. The variable defined using var keywords are mutable and can be changed any number of times.
18. Explain ‘Scala higher order’ functions?
Scala allows the definition of higher order functions. These are functions that take other functions as parameters, or whose result is a function. In the following example, apply () function takes another function ‘f’ and a value ‘v’ and applies function to v.
19. What is Scala anonymous function?
In a 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.
20. What is Monad in Scala?
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.
21. How do I append to the list?
var myList = List.empty[String]
myList :+= "a"
myList :+= "b"
myList :+= "c"
use++ for appending a list
var myList = List.empty[String]
myList ++= List("a", "b", "c")
22. How can you format a string?
To format a string, use the .format () method, in scala you can use
Val formatted= “%s %i”.format (mystring.myInt)
Val formatted= “%s %i”.format (mystring.myInt)
23. What are the different types of Scala literals?
1) Integer literals
2) Floating point literals
3) Boolean literals
4) Symbol literals
5) Character literals
6) String literals
7) Multi-Line strings
2) Floating point literals
3) Boolean literals
4) Symbol literals
5) Character literals
6) String literals
7) Multi-Line strings
24. What are the four types of scala identifiers ?
1) Alpha numeric identifiers
2) Operator identifiers
3) Mixed identifiers
4) Literal identifiers
2) Operator identifiers
3) Mixed identifiers
4) Literal identifiers
25. Why scala prefers immutability?
Scala prefers immutability in design and in many cases uses it as default. Immutability can help when dealing with equality issues or concurrent programs.