Scala Interview
Scala Interview
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")
}
• Akka Framework
• Spark Framework
• Play Framework
• Scalding Framework
• Neo4j Framework, etc.
• 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
Java Scala
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.
Lazy evaluation and operator Lazy evaluation and operator overloading are
overloading are not supported. supported.
Grails, spring, and many other Scala supports frameworks such as Play and
frameworks are supported. Lift.
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.