Month: October 2021

Scala Job Interview – Language Questions

This is the second installment of this series. The first post in the series gives you a bit more context. Anyway, this is my try to answer Scala Interview grade Questions I found in a blog post by Signify. You can find alternate answers here.

Language Questions

What is the difference between a var, a val and def?

All these keywords are for declaring something. var declares a variable, val declares a variable that cannot be re-assigned and def declares a function.

scala
var a = 3
val b = 3
def c = 3

In the above code, a is a variable as you can find in almost every language, you can increment it, re-assign it. Standard stuff.

b is a variable bound to value 3 forever? Attempting to change its value results in a compile-time error. c is a parameterless function that returns 3. Interestingly a, b and c evaluate to the integer 3. Also note that val just prevents reassignment, not value change. E.g. in the following code:

scala
class Foo {
  private var a: Int = 3
  def change( newA : Int ) : Unit = { a = newA }
}

val x = new Foo()
x.change(8)

There is no compile-time error because you are not changing the bound between x and the instance that references. You are just changing the instance.

Continue reading “Scala Job Interview – Language Questions”

Scala Job Interview Answers

Idling over Twitter I came across a post on Signify blog about a list of Scala Job Interview Questions. The post in fact referenced a GitHub repository. Now the post came with a pointer to ready-made answers, but I decided to take the exercise and try to reply myself. The intent was both to check my knowledge and to learn something new since I’m no Scala guru.

The post contained many questions, grouped by topic. So I prefer to split my answers along several posts to keep them short and manageable.

Let’s start with general questions.

Continue reading “Scala Job Interview Answers”