01: Q1 – Q6 ♥ Scala interview questions & answers – FP

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 Bigdata. 70+ FAQ Bigdata & Hadoop interview questions & answers with diagrams & tutorials.

Q1. What is a function in Scala?
A1. A function is a group of statements 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”

In FP “no statements, only expressions

In functional languages, there are no statements, only expressions. The above 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

Print Friendly
The following two tabs change content below.
Arulkumaran Kumaraswamipillai
Mechanical Engineer to freelance Java developer within 3 years. Freelancing since 2003 for the major banks, telecoms, retail & government organizations. Attended 150+ Java job interviews, and most often got 3-6 job offers to choose from. Published Java/JEE books via Amazon.com in 2005, and sold 35K+ copies. Books are outdated and replaced with this online Java training. join my LinkedIn group. Hope these industrial strength Java resources will help you fast-track your Java career too by taking the road less traveled.
  •  
  •  
  •  
  •  
  •  
  •  
Arulkumaran Kumaraswamipillai

Mechanical Engineer to freelance Java developer within 3 years. Freelancing since 2003 for the major banks, telecoms, retail & government organizations. Attended 150+ Java job interviews, and most often got 3-6 job offers to choose from. Published Java/JEE books via Amazon.com in 2005, and sold 35K+ copies. Books are outdated and replaced with this online Java training. join my LinkedIn group. Hope these industrial strength Java resources will help you fast-track your Java career too by taking the road less traveled.

Posted in Scala Interview Q&As

800+ Q&As – ♥ Free | ♦ FAQs

open all | close all

Java FAQs 200+ Quick Prep

open all | close all

16 Java Key Areas to be a top-notch

open all | close all

100+ Java Tutorials – Step by step

open all | close all

100+ Java Coding Exercises

open all | close all

How good are your

open all | close all

List by categories