0% found this document useful (0 votes)
13 views26 pages

Scala Interview

Uploaded by

SASIKUMAR S
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views26 pages

Scala Interview

Uploaded by

SASIKUMAR S
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

What is Scala

Scala, originally a general-purpose programming language, has risen to


prominence in today's Big Data industry. It is because of its high level of
scalability and ability to handle petabytes of data.

Object-oriented and functional programming are both possible with Scala, and
Scala is a language that can run on the Java Virtual Machine (JVM). This makes
Scala a great choice for developing websites. It has grown to be one of the most
beloved programming languages of developers because of its vast array of
libraries. Using Scala, Spark developers can write better, more performant code
through powerful features like macros, tuples, and functions. Although Scala is
difficult to master to some extent, the effort and money put into learning the
language are well worth it.
Example 1: Object-oriented Approach
object ScalaProgram{
def main(args:Array[String])
{
println("Welcome to InterviewBit")
}
}
Output:
Welcome to InterviewBit
A functional approach can also be used to write code in Scala, as shown below:
Example 2: Functional Approach
def scalaProgram
{
println("Welcome to InterviewBit")
}
Output:
Welcome to InterviewBit
Scala Basic Interview Questions
1. What is the importance of App in Scala?
A helper class, named App, is provided by Scala that provides the main method
and its members together. App trait allows Objects to be turned into executable
programs quickly. The App class in Scala can be extended instead of writing your
own main method. This way you can produce concise and scalable applications.
Example:
object InterviewBit extends App
{
println("Hello Scala")
}

2. Write some benefits of using Scala.


It is important for a language to offer attractive features if it hopes to challenge
Java's dominance. In this regard, Scala brings many positive attributes to the table
and its ability to compete with Java proves its prominence. The following are
some of these positive attributes:
• It is easier to learn because it is more concise, readable, and error-free,
especially for people with a background in Java or a similar language.
• Scala offers complex features such as macros, tuples, etc., making coding
easier and more performance-enhancing.
• Scala offers a number of advances, including functions, macros, and tuples.
• By using an expressive typing system, it ensures security and consistency
in statistical abstraction.
• With Scala, you can build fault-tolerant, highly concurrent systems.
• Apache Spark Ecosystem has good support for Scala, it's perfect for data
analytics.
• Scala provides support for concurrency, allowing parallel processing.
3. Name some of the frameworks that Scala supports.
Following are some of the Frameworks Scala supports:

• Akka Framework
• Spark Framework
• Play Framework
• Scalding Framework
• Neo4j Framework, etc.

4. What are case classes in Scala?


Scala case classes are like regular classes except for the fact that they are good
for modeling immutable data and serve as useful in pattern matching. Case classes
include public and immutable parameters by default. These classes support
pattern matching, which makes it easier to write logical code. The following are
some of the characteristics of a Scala case class:
• Instances of the class can be created without the new keyword.
• As part of the case classes, Scala automatically generates methods such as
equals(), hashcode(), and toString().
• Scala generates accessor methods for all constructor arguments for a case
class.
Syntax:
case class className(parameters)
Example:
case class Student(name:String, age:Int)
object MainObject
{
def main(args:Array[String])
{
var c = Student(“Aarav”, 23)
println("Student name:" + c.name);
println("Student age: " + c.age);
}
}
Output:
Student Name: Aarav
Student Age: 23
5. Explain the term stream in Scala.
Streams, a Scala feature, are essentially lazy lists in which elements are only
evaluated as needed. In this way, Scala allows for faster processing. Streams are
similar to lists in terms of performance.
Syntax:
val str = 1 #:: 2 #:: 3 #:: Stream.empty
Scala lets you construct Lists by using the :: operator, while you can build Streams
by using the #:: operator with Stream.empty at the end of the expression. A
stream's head in the above syntax is 1, while its tail is 2 and 3.
Example:
object MainObject
{
def main(args:Array[String]){
val stream = 20 #:: 30 #:: 45 #:: Stream.empty
println(stream)
} Observe the output and you will see a
} question mark instead of the second
element. Scala only evaluates lists if
Output: Stream(20, ?) they are needed.
6. What are tuples and what is their usage in Scala?
A tuple is a heterogeneous data structure, consisting of a fixed number of
elements, each with a different data type. A tuple, however, can contain objects
of different types, as well as being immutable. They are particularly useful when
returning multiple values from methods.
A tuple's type is determined by how many elements it contains as well as the type
of those elements. Scala uses Tuple2, Tuple3, etc., up to Tuple22, to represent
types of tuples. Scala currently limits the number of elements in a tuple to 22, and
if the number of elements in the tuple exceeds 22, an error will be generated.
Example: A tuple storing an integer, and a string.
val name = (23, "InterviewBit")
The inferred type of ingredient is (Integer, String), which is shorthand for
Tuple2[Int, String].
Note: Tuples can be used to overcome this limit. It is possible for a tuple to
contain other tuples.

7. What are different types of Scala variables?


It is well known that variables are merely reserved memory locations for storing
values. In Scala, variables are mainly of two types:

• Mutable Variables: These are variables whose value is capable of changing


and the var keyword is used to declare these variables.
Syntax:
var Variable_name: Data_type = "value";
Example:
var Company: String = "InterviewBit”;
• Immutable Variables: These are variables whose value is not capable of
changing and the val keyword is used to declare these variables.
Syntax:
var Variable_name: Data_type = "value";
Example:
val Company: String = "InterviewBit";

8. Explain Option and write its usage.


The Scala Option[T] is referred to as a carrier of one element or none for a
specified type. As the name suggests, this container holds either a Some or a None
object, which represents a missing value. It holds Some[T] if a value is stored,
otherwise none. In simple words, Scala options are wrappers for missing values.
Example:
object option
{
def main(args: Array[String])
{
val employee:
Map("InterviewBit" -> "Aarav", "Scaler" -> "Ankesh")
val a= employee.get("InterviewBit")
val b= employee.get("Informatica")
println(a);
println(b);
}
} Here, the key of the value InterviewBit
is found, therefore, Some is returned,
Output: however, the key of the value
Some(Aarav) Informatica is not found, therefore,
None is returned.
None
9. Explain what you mean by literals and write its types.
Literals, or constants, are any constant value that can be assigned to a variable. A
literal is a set of symbols used to describe a constant value in the code. They are
classified into the following types:
• Integer literals
• Floating point literals
• Boolean literals
• Symbol literals
• Character literals
• String literals
• Multi-Line strings

10. What are some main features of Scala?


Some languages bring unique benefits to a particular kind of project, making them
the best choice. The interoperability of Scala and its functional programming
paradigm propelled Scala's rapid growth. In order to address Java's critics, Scala
is designed to be concise. The following are some of Scala's unique features that
set it apart from other programming languages:

• Type Inference: Scala doesn't require you to mention the data type or return
type of functions explicitly. It will infer the type of data by itself and the
return type of function depends on the type of last expression present in the
function.
• Immutability: The default behavior of Scala variables is immutability,
which means that they can't be altered. Thus, concurrency control can be
managed easier. In addition, mutable variables can also be used.
• Lazy Evaluation: In lazy evaluation or call-by-need, expressions are not
evaluated until their first use, or until their demand. Computations are lazy
in Scala by default. To declare a lazy variable, use the lazy keyword.
• Case classes and Pattern matching: Case classes in Scala are immutable
classes that can be decomposed via pattern matching. Case classes include
public and immutable parameters by default. These classes support pattern
matching, which makes it easier to write logical code.
• String Interpolation: Scala 2.10.0 introduces String Interpolation, a new
method for generating strings from your data. Users can embed variable
references directly in processed string literals with string interpolation.
String interpolation in Scala can be achieved using the s, f, and raw
interpolation methods.
• Singleton object: Neither static variables nor static methods exist in Scala,
so its singleton object (a class with only one object in the source code) is
used as an entry point to your program execution. a class. When declaring
a singleton object, the keyword "object" is used instead of the class
keyword.
Additionally, it offers:
• Scala's compatibility and interoperability with Java allow developers to
keep their Java libraries and use the JVM.
• By supporting multi-paradigm programming, Scala enables more elegant,
compact, and type-safe programming.
• Scala integrates seamlessly with big data ecosystems, which are largely
based on Java. It works flawlessly with Java libraries, IDEs (like Eclipse
and IntelliJ), and frameworks (such as Spring and Hibernate).
11. What is the use of apply and unapply methods in Scala?
In Scala, an extractor defines a method unapply(), as well as an optional method,
apply(). For mapping and unmapping data between form and model data, both
apply and unapply methods are used.
• Apply() method: Assembling an object from its components is done with
this method. For example, by using the two components firstName and
lastName, we can create the Employee object with the apply method.
• Unapply() method: This method follows the reverse order of apply order
and decomposes an object into components. For example, you can break or
decompose that employee object down into its components i.e., firstName
and lastName.
12. Explain BitSet.
A set is a collection of unique items that cannot be repeated. In Scala, non-
negative integer sets are called Bitsets, and they are represented as variable-size
arrays of bits packed into 64-bit words. The largest number in a bitset represents
its memory footprint.
Syntax:
var BS: BitSet = BitSet(element1, element2, element3, ....)
Here, BS represents the name of the BitSet that was created.
scala.collection.immutable.BitSet and scala.collection.mutable.BitSet are the two
versions of BitSet provided in Scala. Although both are identical, mutable data
structures change the bits in place, thus making them less concurrency friendly
than immutable data structures.
Example:
import scala.collection.immutable.BitSet
object InterviewBit
{
def main(args:Array[String])
{
println("Initialize a BitSet")
val numbers: BitSet = BitSet(5, 6, 7, 8)
println(s"Elements of BitSet are = $bitSet")
println(s"Element 6 = ${bitSet(6)}")
println(s"Element 3 = ${bitSet(3)}")
println(s"Element 7 = ${bitSet(7)}")
}
}
Output:
Initialize a BitSet
Elements of BitSet are = BitSet(5,6,7,8)
Element 6 = true
Element 3 = false
Element 7 = true

13. What do you mean by ofDim()?


Scala provides us with a function called ofDim that declares multidimensional
arrays using the matrix format. Array.ofDim can be used to declare
multidimensional arrays. There's no limit to the number of dimensional arrays
you can create.
Syntax:
val arrayname = Array.ofDim[data_type](number of rows, number of columns)
or
var arrayname = Array(Array(elements), Array(elements))
Example:
object MultiArrayExample
{
def main(args: Array[String])
{ val multiArr= Array.ofDim[Int](2, 2)
multiArr(0)(0) = 5
multiArr(0)(1) = 10
multiArr(1)(0) = 15
multiArr(1)(1) = 20
for(i <- 0 to 1; j <- 0 to 1)
{
println("Element "+ i + j + " = " +
multiArr(i)(j))
}
}
}
Output:
Element 00 = 5
Element 01 = 10
Element 10 = 15
Element 11 = 20
14. Write different types of scope provided for variables in Scala.
According to their declarations, Scala variables are categorized into three scopes
as given below:
• Fields: Variables of this type can be accessed from any method within a
Web object, or from outside the object, depending on access modifiers used.
Depending on the var and val keywords, they can be mutable or immutable.
• Method Parameters: When a method is invoked, these variables are used
to pass values to the method. All method parameters use the val keyword
and are strictly immutable. Normally, these are accessed within a method,
but a Reference allows accessing them outside the method.
• Local Variables: These are the variables (mutable or immutable) declared
inside a method and accessible only within that method. By returning them
from the method, they can be accessed outside of the method.

15. Explain map() and flatmap().


Map() and its close cousin flatMap() are often used when we deal with data
structures in Scala and both are higher-order functions.
• map() method: It functions similarly to mapping operations in other
languages like JavaScript since it allows us to iterate over a collection to
translate it into another collection. Every element of the collection is
transformed by applying a function to it. Its uses are heavily overloaded,
making it useful for situations that aren't immediately obvious. Scala's
map() method is exceptionally powerful.
• flatpmap() method: In Scala, flatMap() is the same as map(), except that
flatMap removes the inner grouping of items and creates a sequence.
Essentially, it is a combination of the flatten method and the map method.
The map method followed by the flatten method produces the same result
as flatMap(), so we can say that flatMap runs the map method first, followed
by the flatten method. Compared to the map method, flatMap is much more
powerful.

Scala Interview Questions for Experienced


1. Explain Implicit Parameter.
The implicit parameter, as opposed to the regular parameter, can be passed
silently to a method without going through the regular parameter list. The implicit
keyword indicates these parameters and any parameters following the implicit
keyword are implicit. Essentially, when we don't pass anything to methods or
functions, the compiler will look for an implicit value and use that as a parameter.
Whenever implicit keywords appear in a function's parameter scope, this marks
all parameters as implicit. Each method can simply have a single implicit
keyword.
Syntax:
def func1(implicit a : Int) // a is implicit
def func2(implicit a : Int, b : Int) // a and b both are
implicit
def func3 (a : Int)(implicit b : Int) // only b is implicit
Example:
object InterviewBit
{
def main(args: Array[String])
{
val value = 20
implicit val addition = 5
def add(implicit to: Int) = value + to
val result = add
println(result)
}
}
Output: 25
2. What do you mean by Monads in Scala?
Despite being a simple topic, the monad belongs in the advanced section of the
Scala language. It is important to understand that a monad is not a class or a trait;
it is a concept. Thus, a monad represents an object that wraps another object in
Scala. Each step in Monads has an output that serves as input for subsequent steps.
Although Scala's maximum collections are Monads, yet not all Monads are
collections. Some Monads, for example, can be used as containers like Options.
To put it succinctly, the data types that implement map and flatMap() (such as
options, lists) in Scala are called monads.
3. What is Scala Anonymous Function?
Standard functions generally include a name, a list of parameters, a return type,
and a body. An anonymous function is one for which a name is not given. In
Scala, anonymous functions are defined by a lightweight syntax. In the source,
anonymous functions are referred to as function literals, and at run time, these
function literals become objects called function values. We can use it to create
inline functions.
Syntax:
(z:Int, y:Int)=> z*y
Or
(_:Int)*(_Int)

4. Explain why Scala prefers Immutability?


Scala takes the opposite approach; with functions, the arguments are val - i.e.,
immutable by default. This eliminates the need to type extra arguments. Scala is
known for its use of immutability in design and in many cases, this is the default
behavior. Immutability's benefits include addressing equality issues and
concurrency.

5. What are different types of Identifiers in Scala?


Identification is done with the help of identifiers in programming languages. The
identifiers in Scala are case-sensitive and can be class, method, variable, or object
name.
Example:
class InterviewBit
{
var x: Int = 45
}
object Scaler
{
def main(args: Array[String])
{
var y = new InterviewBit();
}
}
As you can see from the above example, we have six identifiers:
• InterviewBit: Class name
• x: Variable name
• Scaler: Object name
• main: Method name
• args: Variable name
• y: Object name
Types of Identifiers
• Alphanumeric Identifiers: These identifiers start with a letter
(capital/small) or underscore, followed by a digit, letter, or underscore.
Example: myVal , _Scaler , etc.
• Operator Identifiers: These identifiers contain one or more operator
characters (such as +, :, ?, ~, or #, etc.).
Example: +, ++ , etc.
• Mixed Identifiers: This type of identifier consists of an alphanumeric
identifier followed by an underscore and the operator identifier.
Example: scaler_+ , myVal_= , etc.
• Literal Identifiers: In these identifiers, an arbitrary string is enclosed
with backticks (`….`).
Example: `Scaler` , etc.
6. Explain the function Currying in Scala.
In Scala, currying refers to the technique of transforming a function. There are
multiple arguments passed into this function, which then forms a chain of
functions that take one argument each. This type of function is widely used in
multiple functional languages.
Syntax:
def curryfunction_name(argument1, argument2) = operation
Example:
object Currying
{
def add(a: Int, b: Int) = a + b;
def main(args: Array[String])
{
println(add(10, 5));
}
}
Output:
15
In this case, we've defined an add function that takes two arguments (a and b),
and it gives us the result of adding a and b, by calling it in the main function.
7. What do you mean by tail-recursion?
Scala supports tail recursion, which sets it apart from other languages. Recursion
involves breaking a problem down into smaller sub-problems and calling itself to
resolve each of them. Simply put, recursion can be thought of as a function calling
itself. Tail recursion refers to executing the last statement recursively. As a result,
these functions are more performant since they utilize tail call optimization. They
do not add another stack frame to memory, but rather keep calling functions. If
you are experiencing a stack overflow with your recursive functions, a tail-
recursive function is your remedy. In addition, tail recursion makes your code
faster and memory-constant.
Syntax:
@tailrecdef FuntionName(Parameter1, Parameter2, ...): type = …
Example:
import scala.annotation.tailrec
object InterviewBit
{
def SCALER(a: Int, b: Int): Int =
{
@tailrec def scaler(x:Int, y:Int): Int=
{
if (y == 0) x
else scaler(y, x % y)
}
scaler(a, b)
}
def main(args:Array[String])
{
println(SCALER(11, 7))
}
}
Output:
4
8. What are different types of constructors used in Scala?
The constructors are responsible for initializing the state of the object. In the same
way as methods, constructors also consist of statements/instructions that are
executed when an object is created. Constructors are used in Scala to create
instances of classes. Scala has two types of constructors:
• Primary Constructor: Scala programs often contain only one constructor,
known as the primary constructor. It is not necessary to create a constructor
explicitly since the primary constructor and the class shares the same body.
Syntax:
class class_name(Parameter_list)
{
// Statements...
}
• Auxiliary Constructor: Auxiliary constructors are the constructors in a
Scala program other than a primary constructor. A program may contain any
number of auxiliary constructors, but only one primary constructor.
Syntax:
def this(......)
9. Explain the difference between the terms Nil, Null, None and
Nothing.
Null, null, Nil, Nothing, None, and Unit are all used to represent empty values in
Scala.
• Null refers to the absence of data, and its type is Null with a capital N.
• Null is considered to be a list that contains zero items. Essentially, it
indicates the end of the list.
• Nothing is also a trait without instances.
• None represents a sensible return value.
• Unit is the return type for functions that return no value.
10. Can you explain the functionality of Yield?
Scala uses the yield keyword in conjunction with the for loop. Following the
completion of loop iterations, the yield keyword returns the result. For loops
internally store the iterated result in a buffer, and upon finishing all iterations, it
yields the final result from that buffer. Maps, FlatMaps, Filters, and Nomads are
all supported by Yield.
Syntax:
var result = for{condition} yield variable
11. State difference between Scala and Java.
Scala and Java are two of the world's most popular programming languages.
Comparing Scala to Java, Scala is still a relatively new language. So, let us
compare some of the differences between both of them.

Java Scala

Originally intended to be both an object-


It was originally designed to be an
oriented and functional language, Scala
object-oriented language, but it began
supports concurrency and immutability, as
supporting functional programming
well as many other features of functional
features in recent years.
programming.

It takes a short time to convert source It takes a long time to compile the source
code to byte code. code into byte code.

Scala is designed to be concise. One line of


In Java, even the most mundane and Scala code can easily replace twenty lines of
simple tasks require long-form code. “simple” Java code, even though it is a
slightly more complex language.

Lazy evaluation and operator Lazy evaluation and operator overloading are
overloading are not supported. supported.

Due to Java's backward compatibility,


The language does not provide backward
code written in the new version will
compatibility.
run without error in an older version.

Grails, spring, and many other Scala supports frameworks such as Play and
frameworks are supported. Lift.

Objects are treated as functions in In Scala, any method or function can be


Java. treated as a variable.

Java variables are mutable by default. Scala variables are immutable by default.

When compared to Scala, Java is Unlike Java, Scala includes nested coding,
easier to read. making it less readable.
Java Scala

Static keywords are used in Java. There are no static keywords in Scala.

The language supports multiple inheritances


Multiple inheritances are not supported
through classes, but not through abstract
by classes, but by interfaces.
classes.

12. What are different packages in Scala?


The default Scala packages are Java.lang, Java.io, and PreDef, each of which has
varying functionalities.
• Java.lang: Fundamentally, Java.lang is a package containing classes
compatible with the Java programming language.
• Java.io: You can import Scala classes for input-output resources with
Java.io.
• PreDef: The PreDef package implements type aliases for collections such
as Map, Set, and List that are specifically immutable.
13. Explain the different access modifiers that are available in Scala.
Using access modifiers, you can limit the scope to particular regions of code.
Scala has three access modifiers: public, private, and protected. If you wish to use
an access modifier, you must include its keywords private/protected/public in the
definition of a member of the package, class, or object. By default, the access
modifier is public if neither of these keywords is used.
• Private: A private member can only be accessed within a class that defines
it or through one of its objects.
• Protected: Protected members are accessible from sub-classes of the class
in which they are defined. This also applies to methods and variables.
• Public member: By default, the access modifier is public. In the absence
of the private or protected keyword, the compiler will treat the members as
public, and it is not necessary to specify "public".
14. Explain how you will explain a function in Scala.
Functions are first-class values in Scala and can be created using the def keyword.
When defining a function, the return type of the parameter must be specified. A
function's return type is optional. The default return type of a function is Unit if
it is not specified. Function declarations in Scala have the following form: −
def function_name ([list of parameters]) : [return
type] =
{
//Statement to be executed
}
When you don't use the equals sign and the method body, methods are implicitly
declared abstract.
Example:
object InterviewBit
{
def main(args: Array[String])
{
println("Sum:" + addFunction(10,5));
}
def addFunction(x:Int, y:Int) : Int =
{
var sum:Int = 0
sum = x + y
return sum
}
}
Output:
Sum: 15
15. Why “Static” keyword not used in Scala?
The Scala team has not decided to use a "static" keyword as part of their design.
This decision was taken primarily to make Scala a Pure Object-Oriented
Language. Using the "static" keyword, we can even access class members without
creating or using an object. This is totally against the OOP principles. For
instance, Java does not qualify as a pure OO language since it supports the "static"
keyword.
16. What are the types of inheritance supported by Scala?
There are various types of inheritance supported by Scala, including single,
multilevel, multiple, and hybrid. Single, multilevel, and hierarchy can all be
applied to your class. Due to the fact that Scala doesn't allow multiple
inheritances, Scala's trait comes into play to deal with the problem. Traits are
defined in a similar manner to classes, except that they use the keyword trait
rather than the class as shown below:
trait TraitName
{
//Methods
//Fields
}
17. What is the error in the following code:
var i = 0
while (j < args.length)
{
println(args(i))
j++
}
Scala does not support the expression "j++" and should either be replaced with
“j+=1” or “j=j+1”.
18. Explain how you will append data to a list.
A Scala list is a collection of data stored as a linked list. In Scala, a list is
immutable, meaning it cannot be changed. To remedy this, Scala offers List-
Buffer. There are multiple ways to update a list and add new elements such as
using the ":+" method: It adds new elements at the end of a list.
Syntax:
list_Name :+ element_Name
Example:
object Colours
{
def main(args: Array[String])
{
val myList = List ("Red" , "White")
println("List’s Content : " + myList)
println(" Appending/adding elements at the end
of list!")
val updatedList = myList :+ "Blue"
println("Updated List’s Content: " +
updatedList)
}
}
Output:
List’s Content : List(Red, White)
Appending/adding elements at the end of the list!
Updated List’s Content : List (Red, White, Blue)
19. How to run a Scala program in Eclipse?
A simple program in Scala using Eclipse IDE
• Step 1: Setup your Eclipse IDE for Scala development
• Step 2: In Eclipse, create a new Scala project
From the File menu, select New -> Scala project and provide the project
with a name.
• Step 3: Create a Scala object
Create a new Scala Object using File -> New -> Scala Object and provide a
name for your Scala application - > Click Finish to create the file.
• Step 4: Write your program code.
Example:
object InterviewBit
{ def main(args: Array[String])
{
println("Hello Scaler!!!")
}
}
• Step 5: Run the Scala program
Click on the Run button or select Run from the menu to run the Scala
application. If necessary, set the Run configuration.
Output:
Hello Scaler!!!
Conclusion
With Scala, you can both develop functional programs and Object-Oriented applications! Not
so difficult, was it? As a result, the need for Scala specialists is growing worldwide. To gain
access to the growing pool of job opportunities for Scala experts, you must first get a good
score on the Scala Interview. To help you out, we have compiled a list of 35+ of the most
commonly asked Scala interview questions and answers to provide some insight into what
Scala interviewers are really interested in. For the freshers and experienced developers, these
questions will be a great help in preparing for a software development job. For the pros, it
will be a helpful tool for implementing improvements in their everyday coding practices. It
should be noted that this list isn't exhaustive; Scala has several nuanced aspects that are better
mastered with practice.

20. Explain higher-order functions with an example.


Higher-order functions are functions in Scala that either take or return another
function as an argument or parameter. Another way to say it is a function that
works with a function. You can create higher-order functions, lambda functions,
or anonymous functions using a higher-order function.
Example: In this case, the apply function contains another function a that is
applied to b.
object InterviewBit
{
def main(args: Array[String])
{
println(apply(format, 45))
}
def apply(a: Double => String, b: Double) = a(b)
def format[S](x: S) = "{" + x.toString() + "}"
}
Output:
{45.0}
21. What do you mean by Closure in Scala?
Scala closures are functions whose return value is dependent on one or more free
variables declared outside the closure function. Neither of these free variables is
defined in the function nor is it used as a parameter, nor is it bound to a function
with valid values. Based on the values of the most recent free variables, the
closing function is evaluated.
Syntax:
var x = 20
def function_name(y:Int)
{
println(x+y)
}
Example:
object InterviewBit
{
def main(args: Array[String])
{
println( "Sum(2) value = " + sum(2))
println( "Sum(3) value = " + sum(3))
println( "Sum(4) value = " + sum(4))
}
var x = 20
val sum = (y:Int) => y + x
}
Output:
Sum(2) value = 22
Sum(3) value = 23
Sum(4) value = 24
In the above program, the sum is a closure function. var x = 20 is an impure
closure. As can be seen, x is the same, and y is different.
22. Explain Traits in Scala.
The concept of traits is similar to an interface in Java, but they are even more
powerful since they let you implement members. It is composed of both abstract
and non-abstract methods, and it features a wide range of fields as its members.
Traits can either contain all abstract methods or a mixture of abstract and non-
abstract methods. In computing, a trait is defined as a unit that encapsulates the
method and its variables or fields. Furthermore, Scala allows partial
implementation of traits, but no constructor parameters may be included in traits.
To create traits, use the trait keyword.
Syntax:
trait Trait_Name
{
// Fields...
// Methods...
}
Example:
trait MyCompany
{
def company
def position
}
class MyClass extends MyCompany
{
def company()
{
println("Company: InterviewBit")
}
def position()
{
println("Position: SoftwareDeveloper")
}
def employee() //Implementation
of class method
{
println("Employee: Aarav")
}
}
object Main
{
def main(args: Array[String])
{
val obj = new MyClass();
obj.company();
obj.position();
Obj.employee();
}
}
Output:
Company: InterviewBit
Position: SoftwareDeveloper
Employee: Aarav

You might also like