Scala

Scala Important topics-Interview questions

Q1) CASE Classes:

A case class is a class that may be used with the match/case statement.
Case classes can be pattern matched
Case classes automatically define hashcode and equals
Case classes automatically define getter methods for the constructor arguments.
Case classes can be seen as plain and immutable data-holding objects that should exclusively depend on their constructor arguments.
Case classes contain a companion object which holds the apply method. This fact makes possible to instantiate a case class without the new keyword.

Q2) Pattern Matching

Scala has a built-in general pattern matching mechanism. It allows to match on any sort of data with a first-match policy

object MatchTest1 extends App {
def matchTest(x: Int): String = x match {
case 1 => “one”
case 2 => “two”
case _ => “many”  or case other => “many”
}
println(matchTest(3))
}

o/p: many

Q3)Curring

Methods may define multiple parameter lists. When a method is called with a fewer number of parameter lists, then this will yield a function taking the missing parameter lists as its arguments.
(Or)
Currying is a technique of making a function that takes multiple arguments into a series of functions that take a part of the arguments.

def add(a: Int)(b: Int) = a + b
val add2 = add(2)(_)

scala> add2(3)
res0: Int = 5

Currying is useful in many different contexts, but most often when you have to deal with Higher order functions.

Share This Post

Avatar
An Ambivert, music lover, enthusiast, artist, designer, coder, gamer, content writer. He is Professional Software Developer with hands-on experience in Spark, Kafka, Scala, Python, Hadoop, Hive, Sqoop, Pig, php, html,css. Know more about him at www.saikumar.me

Lost Password

Register