Home / Interview / Scala :: General Questions

Interview :: Scala

11) What is a higher order function in Scala?

Higher order function is a function that either takes a function as an argument or returns a function. In other words, we can say a function which works with function is called a higher-order function.

Example

12) What is function composition in Scala?

In Scala, functions can be composed from other functions. It is a process of composing in which a function represents the application of two composed functions.

Example

13) What is Anonymous (lambda) Function in Scala?

An anonymous function is a function that has no name but works as a function. It is good to create an anonymous function when you don't want to reuse it later. You can create anonymous function either by using ⇒ (rocket) or _ (underscore) wildcard in Scala.

Example

14) What is a multiline expression in Scala?

Expressions those are written in multiple lines are called multiline expression. In Scala, be careful while using multiline expressions.

Example

The above program does not evaluate the complete expression and return b here.

15) What is function currying in Scala?

In Scala, the method may have multiple parameter lists. When a method is called with a fewer number of parameter lists, this will yield a function taking the missing parameter lists as its arguments.

Example

16) What is a nexted function in Scala?

In Scala, you can define the function of variable length parameters. It allows you to pass any number of arguments at the time of calling the function.

Example

17) What is an object in Scala?

The object is a real-world entity. It contains state and behavior. Laptop, car, cell phone are the real world objects. An object typically has two characteristics:

1) State: data values of an object are known as its state.

2) Behavior: functionality that an object performs is known as its behavior.

The object in Scala is an instance of a class. It is also known as runtime entity.

18) What is a class in Scala?

The class is a template or a blueprint. It is also known as a collection of objects of similar type.

In Scala, a class can contain:

  1. Data member
  2. Member method
  3. Constructor
  4. Block
  5. Nested class
  6. Superclass information, etc.

Example

19) What is an anonymous object in Scala?

In Scala, you can create an anonymous object. An object which has no reference name is called an anonymous object. It is good to create an anonymous object when you don't want to reuse it further.

Example

20) What is a constructor in Scala?

In Scala, the constructor is not a special method. Scala provides primary and any number of auxiliary constructors. It is also known as default constructor.

In Scala, if you don't specify a primary constructor, the compiler creates a default primary constructor. All the statements of the class body treated as part of the constructor.

Scala Primary Constructor Example