InterviewSolution
This section includes InterviewSolutions, each offering curated multiple-choice questions to sharpen your knowledge and support exam preparation. Choose a topic below to get started.
| 1. |
How Scala defines the getter-setter methods for a field in a class? |
|
Answer» Scala automatically GENERATES getter-setter METHODS for each field DEFINED in a class after compilation. If there is a field as, value:Int the getter-setter methods for this field would be, def value:Int = this.value // Getter def value_= (value:Int) = this.value = value // Setter
|
|
| 2. |
What is the linearization rule in Scala? |
|
Answer» When a class extends multiple traits, it might happen that they have a common method. Now when that method is executed, it is very important to know the trait on which the method is INVOKED first. The linearization rule says that the common method execution WOULD start in the traits from right to left as it is mentioned in the extends list. Though the class hierarchy is like a graph, the execution of the method stringValue FOLLOWS a particular order based on the linearization rule: |
|
| 3. |
What is the difference between a statement and expression? |
|
Answer» A statement is an instruction that performs some operation, whereas, an expression evaluates a value. In SCALA, the basic control structures such as if and for-loop are expressions, as they RETURN value and it can be ASSIGNED to a variable. For example, val isOdd = if (num%2 != 0) true else false |
|
| 4. |
What is a package object? |
|
Answer» In Scala, every package permits one package object to declare. It is a singleton object where the defined members can be imported in a class through the package import. It lets the developer define top-level FUNCTIONS. Please note that overloading is not ALLOWED in package objects. Let’s see an example: package object myconverters { DEF fromAtoB (a:A): B = ... //conversion logic def fromAToInt (a:A):Int = ... //conversion logic different other converters } |
|
| 5. |
How can you import all classes from a specific package excluding a few classes? |
|
Answer» Scala import provides a NUMBER of flexibilities. We can EXCLUDE certain CLASSES while importing the whole package: import com.xyz.abc.{Abc => _, _} // excluding the class AbcAlso, it’s POSSIBLE while importing that we can rename the existing classes. import com.xyz.abc.{Abc => Pqr, _} // renaming the class Abc |
|
| 6. |
How many access modifiers are there in Scala? |
|
Answer» Scala defines 3 types of access modifiers:
|
|
| 7. |
What is a by-name parameter in Scala? |
|
Answer» Scala allows passing a block of code in a function which is evaluated only when it is used. This kind of parameters are called a by-name parameter. By-name parameters are often used while DEFINING a new CONTROL structure. Let’s take an example. DEF executionTime(f: => Any) = { val start = System.currentTimeMillis() f System.currentTimeMillis() - start } val t = executionTime { gcdList(List(18, 24, 36)) } |
|
| 8. |
Why immutability is very important in software development? |
|
Answer» Immutability is a very important concept in software development. Immutable objects don’t change their state after construction. So they are very safe to USE as a shared object in a multi-threaded environment. They also reduce the parts of code which need explicit synchronization and LOCKS. This IMPROVES the overall performance of the system. Immutable objects HELP in writing pure functions or methods. There are no side effects in pure functions. That is why they make the whole application less bug-prone and easily TESTABLE. |
|
| 9. |
What is a Trait? |
|
Answer» A trait is like a Java interface declaring a SET of methods and fields. The difference is that the members of a trait may not be abstract. If a class extends a trait, it should implement the abstract methods and fields declared by the trait. On the other hand, it may or may not override those methods having bodies. For example, trait Comparable[T] { def compare(other: T): Int; def greaterThan (other: T) = compare(other) > 0 def lessThan (other: T) = compare(other) < 0 def isEqualTo (other: T) = compare(other) == 0 } trait Loggable { def LOG(out:PRINTWRITER) }
|
|
| 10. |
Does Scala support static methods? If not, then how can we write object-independent or class level methods in Scala? |
|
Answer» Scala doesn’t have a static keyword to DEFINE the OBJECT-independent METHODS. We can directly define them in a singleton object. Otherwise, we can use a companion object for a given class for holding those methods. For example, we can create a List in Scala in the following way, val l = List(1, 2, 3, 4)This is POSSIBLE as there is a method named apply defined in the object - List . |
|
| 11. |
How can you define an auxiliary constructor for a class? |
|
Answer» Scala PERMITS defining the auxiliary constructors of a class using this keyword. If we define one or more than one methods in the class body, they BECOME the auxiliary constructors. The first statement of an auxiliary CONSTRUCTOR must be either a call to the primary constructor or any other auxiliary constructor. For example, class Customer(val ID:Int, val name:STRING, val age:Int) { def this(id:Int, name:String, dob:LocalDate) = { this(id, name, Period.between(dob, LocalDate.now).getYears) } } |
|
| 12. |
What do you mean by class parameters in Scala? |
|
Answer» In Scala, while declaring a class, sometimes we SPECIFY some PARAMETERS which are mandatory to pass while instantiating an object of that type. These parameters are called the class parameters. |
|
| 13. |
What is the difference between a function and a method? |
|
Answer» Function and method both terms are used when a CODE block takes a SET of parameters and computes a value or performs some action based on the INPUTS. Such a block is called a function when it is not associated with any class. On the other hand, one such block is called a method when it is a member of a class. |
|
| 14. |
What is the difference between val and lazy val? |
|
Answer» In Scala, val and LAZY val both are used to declare a value. When we declare a val and ASSIGN some expression in it, the expression is evaluated and assigned in that place. Whereas, for lazy val, the expression is evaluated and the value is assigned at the time of its first use. Lazy VALS are useful when OBJECT creation or expression evaluation is costly in terms of CPU and memory and the value may or may not be used based on some condition. |
|
| 15. |
What is a local function? |
|
Answer» SCALA has a feature to define a function in the body of another function. The scope of these functions are the declaring function only. That is they are not accessible from anywhere else in the CODE. These functions are called local functions. Here, the recursive function gcd() inside gcdList() is a local function. |
|
| 16. |
What is a higher-order function? |
|
Answer» A function that TAKES another function as an argument or returns a function is called a higher-order function. For EXAMPLE, the sumApplied() function as defined below is a higher-order function. def sumApplied(x:Int, y:Int, F:Int => Int):Int = f(x) + f(y) |
|
| 17. |
What is a first-order function? |
|
Answer» In Scala, FUNCTIONS can be written as LITERALS. They can be assigned to any variable, can be passed to another function as an argument or can be returned from a function as a value. These functions are called first-order functions. For example, in the following code snippet, X => x*x is a first-order function which has been assigned to a value NAMED SQUARE. |
|
| 18. |
Why Scala is called a Scalable Language? |
|
Answer» Scala is a general-purpose language which runs on JVM. It has some unique features which make it very useful to develop programs of different shapes and sizes. Starting from a SMALL script to enterprise level multi-module applications, Scala GIVES the developers a lot of flexibility in design and implementation. Inferring types, implicit conversions and its conciseness help defining custom control structures and domain-specific languages. On one side, its object-oriented features help to design systems with complex ENTITIES. On the other hand, the functional programming constructs help to develop bug-free and reusable code. In short, the language FITS in serving all kinds of requirements. That’s why it is called a scalable language. |
|