03: Q1 – Q6 Scala interview Q&As on Functional Programming

100+ FAQ Scala interview questions answered with code. Learn more about FP – 6 tips to transforming your thinking from OOP/imperative programming to functional programming (i.e. FP). Scala is very popular in Apache Spark programming to process Big Data. 70+ FAQ Bigdata & Hadoop interview questions & answers with diagrams & tutorials.

Q1. What is a function in Scala?
A1. A function is a group of expressions that perform a task. A Scala function declaration is of the form:

Scala simple function

“addOne” is a function that takes an “Int” and returns a result of type “Int”

Q. What is an “object” in Scala?
A. In Scala class defines a Class, object defines a Singleton object. It can be used to hold static members that are not associated with instances of some class.

This is same as above named function addOne(…), but as an anonymous class, which is an instance of ‘Function1’

T1 is input argument of type T1, which can be Int, String, Person, etc, and R is a return type of R, which can be of type Int, String, Person, etc. Similarly, trait Function2, where T1 and T2 are inputs of types T1 & T2, whereas R is a return type of R.

Used as shown below:

“addOne(5)” is same as “addOne.apply(5)” OR as “addOne apply 5 ”

So, all below are same in Scala:

OR

OR

Similarly,

1) Function1[Int, Int] is same as Int => Int i.e. A function that takes one arg of type Int and returns a result of type Int.

2) Function2[Int, Int, Int] is same as (Int , Int) => Int i.e. A function that takes two arguments of type Int and returns a result of type Int.

….

3) Function5[Int, Int, Int, Int, Int, Int] is same as (Int , Int, Int, Int, Int) => Int i.e. A function that takes 5 arguments of type Int and returns a result of type Int.

3) Function3[Int, String, Person, Person] is same as (Int , String, Person) => Person i.e. A function that takes 3 arguments of types Int, String & Person respectively and returns a result of type Person.

The above example implicitly infers the return type for the variables addOne & result values. The below code shows them explicitly after the “:

“abstract def apply(v1: T1): R” meaning Apply the body of this function to the argument. This is why you don’t have to define a list with a “new” keyword in Scala as most Scala objects have this “apply” function. You can just do

which is same as writing:

the apply method is like a constructor which takes arguments and creates an object. The unapply takes an object and tries to give back the arguments. This is useful in Scala pattern matching.

apply vs unapply

Name the above file as “UserId.scala“. Bring up the Scala REPL with:

Compile & Run:

Bring up a REPL:

Use “:paste” to paste multi-line and “ctrl-D” to finish

Outputs:

In FP “no statements, only expressions

Q. What is the difference between a statement & an expression?
A. An expression evaluates to a value. A statement does something. Statements represent an action or command e.g print statements, assignment statements. Expression is a combination of variables, operations and values that yields a result value.

Statement
Expression

In Scala, you write it as

In functional languages, there are no statements, only expressions. The addOne function definition code can be rewritten as following using “Int => Int” lambda expression. Takes an “Int” as arg and the body (i.e. x+1) returns an “Int ” as a result.

Anonymous(i.e. NO NAME) lambda expression can be assigned to a variable “addOne”. Expression can also be written as:

OR as

So, defining your functions without giving them a name, and sprinkling your code with these types of “function literals” is similar to writing integer literals like 32.

In FP, functions can not only be assigned to variables, but also can be passed around like objects.

Functions are Objects, and Objects are Functions

Q2. Can you explain the statement that “a function” is an object in Scala?
A2. In Scala, everything is an Object. No primitives, no wrapper types, no auto boxing & unboxing, etc. Since, Scala is an Object Oriented Programming (OOP) & a functional programming (FP) language, “Functions are Objects“, and also “Objects are Functions” as every value is an Object in Scala.

Output:

The function “applyAndAddOne” takes a function as an argument. Since a function is an object, it can be passed around as an argument to other functions. It takes two arguments:

1) A function of type Functio1[Int, Int], which takes an Int as arg and returns an Int type.

2) A “number” of type “Int”.

We are passing the “addOne” function to “applyAndAddOne” function along with a value of 5. So, it invokes the “addOne” function first to add 1 to 5, and then adds another one to return a result of 7.

Functions that take other functions as parameters, or whose result is a function is known as higher-order functions.

Q3. In Scala, why do you have traits like Function1, Function2, Function3, etc up to Function22?
A3. A “trait” in Scala is like an interface in Java. A “trait” has more ways to be applied than an interface in Java. In the above example we used “Function1[Int, Int]” to take a single argument of type “Int” and return a type of Int.

Q. How will you declare a function that takes 3 arguments of type Int, and returns a result of type Boolean?
A. Function3[Int, Int, Int, Boolean]

When you declare something like the following,

The Scala compiler implicitly creates an implementation of the right function trait. E.g. Function3[Int, Int, Int, Boolean]

Q. How will you declare a function that takes 2 arguments of type Int, 2 arguments of type String and returns a result of type Boolean?
A. Function4[Int, Int, String, String, Boolean].

Q4. Can you explain the following code, and what will be the output?

A4. The output will be

1) “x => x % 2 != 0” is a function of the form “Int => Boolean”. The Scala compiler will implicitly create an object implementation of the trait “Function1[Int, Boolean]“. This is known as an “Anonymous Function “.

2)applyFilter” is a function that takes three input parameters 1) a function, 2) a list, 3) a string and returns nothing.

3) “val result = input.filter(f);” filters the list based on the predicate supplied as a function. If the function returns “true”, that element is added to the result. “x % 2 != 0” returns true for the odd numbers, hence odd numbers are added to the list and so on for the even numbers, and multiples of 5.

Q5. Is there anything not quite right with the following Scala code?

A5. Lacks code reuse. The condition as to what numbers to be added is known as a “predicate“. This predicate can be added as a function and passed to single “addNumbers” function as shown below.

Q6. In the “AddGivenNumberExample.scala” below

How can you modify the code in “AddGivenNumber” so that in “AddGivenNumberExample” object instead of “three.add(5)”, you can just do “three(5)“.

A6. Change the name of “add(….)” function in the class “AddGivenNumber” to “apply(….)” as shown below. In mathematics and computer science, Apply is a function that applies functions to arguments. “apply” is a method which get’s called on Function application

So, you can use it in a Scala object as “three.apply(5)” or as three(5) as shown below.

So, this is same as instead of doing something like the following in other languages

In Scala, you can do

as the List object has an “apply” function as shown below. “A*” means type “A“, which can be an Int, String, Boolean, Any, etc and “*” repeated anynumber of times. E.g. Int, Int, Int as in (3, 5, 7).

This enables us to create a list in Scala as shown below, and the “apply(…)” method is implicitly invoked.

Note: Always have the Scala API doco handy. E.g. http://www.scala-lang.org/api. If you look at the Scala APIs, you will see “apply” methods. E.g. Future object, Promise object, List object, and the list goes on.

More Scala Interview Q&As

1. Q6 – Q12 Scala basics interview Q&As on functions

2. Q59 – Q64 Higher Order Functions in Scala Interview Q&As


Categories Menu - Q&As, FAQs & Tutorials

Top