scala interview questions

Scala Interview Questions

Scala stands for Scalable language. It’s a functional oriented programming language. Recently it’s spread it’s roots in all major languages like Apache Spark, Akka.

What is difference between abstract class and traits in Scala?

A class that extend another subClass called abstract class. Here abstract class creates a constructor and a class can extend only one class.
Traits are collections of fields and behaviors that you can extend or mix into your classes. It’s a component of a class, not a class by itself, so there is no constructor, trait is restricted in comparison to class to prevent multiple inheritance problems.scala interview questions

What is difference between Map and FlatMap?
Map is a method that apply a function on all elements.  it returns the associated value in a Some. Where as FlatMap also same, but it returns a sequence of for each element in the list. The results flattening into the original data list.
Eg: val l = List(“venu”, “Jyothi”, “Koti”, “Brahma”) //Returns VENU, JYOTHI, KOTI, BRAHMA
l.flatMap(_.toUpperCase) //Returns V,E,N,U,J,Y,O,T,H,I,K,O,T,I,B,R,A,H,M,A

What is the difference between for/yield combination without combination?

Yield is part of for comprehensions, which generates a value which will be temporary remembered/buffered internal values, but the initial collection is not change.

  • The combination of for/yield is returning a new collection.
  • But for loop without yield is return just operating on each element, it’s not create new collection

What is difference between for loop, for with Guard, for with Yield?

  • Simple for loop iterates over a collection, translated to foreach method.
  • for with guard translated to a sequence of withFilter method followed by foreach method.
  • for with yield translated to a withFilter followed by map method.

What indexOf(“”) does?

It searches for specified value and return Int.

res0: Int = 2

Why one billion plus two billion is not equal to three billion?

Int is 32 bits. It means it support maximum 2 power 32 (2147483647) value.
2 power 31 is 2147483647. If you add 2^32+1= -2147483648 (reached maximum 32 bit number)
It means it’s decreasing value from the last number.
If you add 2^31 + 2^31 answer -2.
However If you add 2^31 + 2^31  + 4 answer is 2. Now it coming from 0 and counts left to right (positive) manner.
Similarly if you add more than 2147483647 (two billion) it shows negative values, not 3 billions. If you add more than that value use long data type (Long is 64  bit) to get accuracy value.

How to print Un-icodes?
You can get different unicodes from this website.

What is difference between val and var variables?
To change the reference variables used “var”. Where as in “Val” the reference variable doesn’t change.

How can you get this output quickly?
res1: String = (venu, 30, Bangalore)

var n = “venu”
var a = 30
var l = “Bangalore”
s”($n, $a, $l)”

What is Case Class importance?

Depends on constructor Arguments, case class generate an immutable data-holding objects. Case classes provides that  can be reused in new applications without change the original application. Spark uses case classes to infer schema and write code in a single line.

What are case classes limitations?

Case Classes cannot take more than 22 fileds,  you don’t know schema beforehand.

If more than 22 fields in case class then How Spark process the data?
If Case class doesn’t work properly, data loaded as an RDD or the row objects. Schema created separately using StructType (table) and StructFiled (field) objects. Schema is applied to the row RDD to create a DataFrame.

What is string interpolation?
String interpolation allows users to embed variable references directly in processed string literals.
Let examples var names = “Venu”
>”Hello $name please welcome”

There are different interpolations such as: String interpolations, formatted string, raw interpolation,

What is BigInt datatype limitations?
Most often to define a number datatype used Int or Long, but to calculate huge numbers used BigInt. It’s not recommended it can slowdown the system performance.

What is the Difference between Array and List?

Array is mutable, it’s possible to change value, but List not immutable. Arrays are non-variant and lists are co-variant.

Can you explain few collections in Scala?

  • Array:List same type element, it’s mutable.
  • list: List same type elements, its immutable.
  • sets: have no duplicates
  • tuple: A tuple groups together simple logical collections of items without using a class.
  • map: Evaluates a function over each element in the list, returning with the same number of elements.
  • foreach: It’s like map but returns nothing. foreach is intended for side-effects only.
  • filter: Based on condition, removes any element, returns list of elements.
  • zip: aggregates the contents of two lists into a single list of pairs.
  • partition: splits a list based on where it falls with respect to a predicate function.
  • dind: based on matches it returns the first element.
  • drop: Drops the first specified element.
  • dropWhile: Drops the number of specified elements based on condition.
  • flatten: It collapse one level of nested structure.
  • flatMap: It similar to map, but return multiple lists.

8 thoughts on “Scala Interview Questions”

  1. very Nice collection, helped me lot.
    One correction is there in 2nd last question. Update “List is immutable” in place of “not immutable”

  2. Very nice post. Very helpful.
    There is correction in 2nd last question. “Write List is immutable” in place of “List not immutable”.

Leave a Reply

Your email address will not be published. Required fields are marked *