Home / Interview / Scala :: General Questions

Interview :: Scala

1)

What is Scala?

Scala is a general-purpose programming language. It supports object-oriented, functional and imperative programming approaches. It is a strong static type language. In Scala, everything is an object whether it is a function or a number. It was designed by Martin Odersky in 2004.

Scala Program Example

 

2) What are the features of Scala?

There are following features in Scala:

  • Type inference: In Scala, you don't require to mention data type and function return type explicitly.
  • Singleton object: Scala uses a singleton object, which is essentially class with only one object in the source file.
  • Immutability: Scala uses immutability concept. Immutable data helps to manage concurrency control which requires managing data.
  • Lazy computation: In Scala, computation is lazy by default. You can declare a lazy variable by using the lazy keyword. It is used to increase performance.
  • Case classes and Pattern matching: In Scala, case classes support pattern matching. So, you can write more logical code.
  • Concurrency control: Scala provides a standard library which includes the actor model. You can write concurrency code by using the actor.
  • String interpolation: In Scala, string interpolation allows users to embed variable references directly in processed string literals.
  • Higher order function: In Scala, higher order function allows you to create function composition, lambda function or anonymous function, etc.
  • Traits: A trait is like an interface with partial implementation. In Scala, the trait is a collection of abstract and non-abstract methods.
  • Rich set of collection: Scala provides a rich set of collection library. It contains classes and traits to collect data. These collections can be mutable or immutable.

3) What are the Data Types in Scala?

Data types in Scala are much similar to Java regarding their storage, length, except that in Scala there is no concept of primitive data types every type is an object and starts with capital letter. A table of data types is given below.

Data Types in Scala

Data Type Default Value Size
Boolean False True or false
Byte 0 8 bit signed value (-27 to 27-1)
Short 0 16 bit signed value(-215 to 215-1)
Char '\u0000' 16 bit unsigned Unicode character(0 to 216-1)
Int 0 32 bit signed value(-231 to 231-1)
Long 0L 64 bit signed value(-263 to 263-1)
Float 0.0F 32 bit IEEE 754 single-precision float
Double 0.0D 64 bit IEEE 754 double-precision float
String Null A sequence of characters

4) What is pattern matching?

Pattern matching is a feature of Scala. It works same as switch case in other languages. It matches the best case available in the pattern.

Example

5) What is for-comprehension in Scala?

In Scala, for loop is known as for-comprehensions. It can be used to iterate, filter and return an iterated collection. The for-comprehension looks a bit like a for-loop in imperative languages, except that it constructs a list of the results of all iterations.

Example

6) What is the breakable method in Scala?

In Scala, there is no break statement, but you can do it by using break method and importing Scala.util.control.Breaks._ package. It can break your code.

Example

7) How to declare a function in Scala?

In Scala, functions are first-class values. You can store function value, pass a function as an argument and return function as a value from other function. You can create a function by using the def keyword. You must mention return type of parameters while defining a function and return type of a function is optional. If you don't specify the return type of a function, default return type is Unit.

Scala Function Declaration Syntax

8) Why do we use =(equal) operator in Scala function?

You can create a function with or without = (equal) operator. If you use it, the function will return value. If you don't use it, your function will not return anything and will work like the subroutine.

Example

9) What is the Function parameter with a default value in Scala?

Scala provides a feature to assign default values to function parameters. It helps in the scenario when you don't pass value during function calls. It uses default values of parameters.

Example

10) What is a function named parameter in Scala?

In Scala function, you can specify the names of parameters while calling the function. You can pass named parameters in any order and can also pass values only.

Example