While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.
Welcome to Scala interview questions and answers. Now-a-days, most of the Finance/Banking, Government, Telecom, Social Networking etc. companies are using Scala, Play and Akka Framework to develop their projects because these frameworks support both OOPs and FP features and also provide many advantages.
I am going to share my Interview’s Knowledge and Scala Ecosystem or Fullstack Scala (Scala/Play/Akka) Development experience with you through couple of posts. I’m going to share you 3 separate posts. Some questions are just one question and some have some sub-questions. Totally am going to discuss around 200 questions. I bet if you are familiarise with all these questions, you will definitely clear all Scala Developer interviews. However, if you want to continue as a Fullstack Scala developer, you should learn all these Fullstack Scala technologies thoroughly.
I’m going to deliver Scala/Play/Akka Interview Questions and Answers in three parts:
We will also discuss about some difference between Scala and Java constructs so that Users (who are moving from Java to Scala) can get benefits from these posts. In this post, we are going discuss about some basic Scala Interview Questions. Read my coming posts for rest of the two sections.
In this section, we are going to list out all Scala Basic Interview Questions and in next section we will discuss them in detail.
Please refer my previous post to learn more about Functional Programming (Click this link to open my previous post).
In this section, we will pickup each and every question from above list and discuss in-detail with suitable examples(if required). If you want to understand these concepts in-depth with examples, please go through my previous posts in Scala Tutorials section.
Scala stands for SCAlable LAnguage. Martin Odersky is the father of Scala. Scala is a Multi-Paradigm Programming Language, which supports both Object-Oriented and Functional Programming concepts. It is designed and developed by Martin Odersky. Scala is a Type-Safe Object-Functional Programming JVM Language. Scala runs on JVM(Java Virtual Machine). Scala is a Hybrid Functional (Object-Oriented and Functional) Programming JVM Language. Scala has a Strong and Statically Type System. In Scala, all types are checked at compile-time.
Yes, Scala is a Statically-Typed Language. Statically-Typed Language means that Type checking is done at compile-time by compiler, not at run-time. The main Advantage of these kinds of Languages is: As a Developer, we should care about writing right code to avoid all compile-time errors. As Compiler checks many of the errors at compile-time, we don’t get much issues or bugs at run-time. Examples:- Java, Scala, C, C++, Haskell etc. Dynamically-Typed Language means that Type checking is done at run-time, not at compile-time by compiler. As a compiler won’t check any type checking at compile-time, We can expect more run-time issues or bugs. Example:- Groovy, JavaScript, Ruby, Python, Smalltalk etc.
Pure Object-Oriented Programming Language means that everything should be an Object. Java is not a Pure Object-Oriented Programming (OOP) Language because it supports the following two Non-OOP concepts:
Yes, Scala is a Pure Object-Oriented Programming Language because in Scala, everything is an Object and everything is a value. Functions are values and values are Objects. Scala does not have primitive data types and also does not have static members.
Yes, Scala supports all Functional Programming (FP) concepts. Java 8 has introduced some Functional Programming constructs, but it does NOT support all Functional Programming concepts. For instance, Java 8 does not support Pattern Matching, Function Currying, Implicits etc.
If we use Scala Language to develop our applications, we can get the following benefits or advantages and drawbacks: Advantages of Scala Language:-
Drawbacks of Scala Language:-
NOTE:- We can write Scala Code either more readable or less readable way.
Apart from many benefits of Scala, it has one major Drawback: Backward Compatibility Issue. If we want to upgrade to latest version of Scala, then we need to take care of changing some package names, class names, method or function names etc. For instance, If you are using old Scala version and your project is using BeanProperty annotation. It was available in “scala.reflect” like “scala.reflect.BeanProperty” in old versions. If we want to upgrade to new Scala versions, then we need to change this package from “scala.reflect” to “scala.beans”.
Like Java’s Motto “Write Once Run Anywhere”, Scala has “Do More With Less” or “Do More With Less Code” Motto. “Do More With Less” means that we can develop more complex program or logic with less code.
Java, Scala, Groovy and Closure are most popular JVM (Java Virtual Machine) languages. Scala, Groovy and Closure JVM languages supports both Object-Oriented Programming Features and Functional Programming Features. Java SE 8 supports all Object-Oriented Programming Features. However, it supports very few Functional Programming Features like Lambda Expressions, Functions, Type Inference, Higher-Order Functions.
As we know in Java, the super class of all classes (Java API Classes or User Defined Classes) is java.lang.Object. In the same way in Scala, the super class of all classes or traits is “Any” class. Any class is defined in scala package like “scala.Any”.
In Scala, if we don’t mention any access modifier to a method, function, trait, object or class, the default access modifier is “public”. Even for Fields also, “public” is the default access modifier. Because of this default feature, Scala does not have “public” keyword.
Types can be inferred by the Scala Compiler at compile-time. It is known as “Type Inference”. Types means Data type or Result type. We use Types at many places in Scala programs like Variable types, Object types, Method/Function Parameter types, Method/Function return types etc. In simple words, determining the type of a variable or expression or object etc at compile-time by compiler is known as “Type Inference”.
Similarities between Scala’s Int and Java’s java.lang.Integer:
Differences between Scala’s Int and Java’s java.lang.Integer:
Java’s Integer is something similar to Scala’s Int and RichInt. RichInt is a final class defined in scala.runtime package like “scala.runtime.RichInt”. In Scala, the Relationship between Int and RichInt is that when we use Int in a Scala program, it will automatically convert into RichInt to utilize all methods available in that Class. We can say that RichInt is an Implicit class of Int. (We will discuss "What is Implicit and the advantages of Implicits in my next post).
In Scala, Nothing is a Type (final class). It is defined at the bottom of the Scala Type System that means it is a subtype of anything in Scala. There are no instances of Nothing. Use Cases of Nothing In Scala:- If Nothing does not have any instances, then when do we use this one in Scala Applications?
object None extends Option[Nothing]
Nil is an object, which is used to represent an empty list. It is defined in “scala.collection.immutable” package as shown below:
object Nil extends List[Nothing]
Example:-
scala> Nil
res5: scala.collection.immutable.Nil.type = List()
scala> Nil.length
res6: Int = 0
Null is a Type (final class) in Scala. Null type is available in “scala” package as “scala.Null”. It has one and only one instance that is null. In Scala, “null” is an instance of type scala.Null type. Example:-
scala> val myNullRef : Null = null
myNullRef: Null = null
We cannot assign other values to Null type references. It accepts only ‘null’ value. Null is a subtype of all Reference types. Null is at the bottom of the Scala Type System. As it is NOT a subtype of Value types, we can assign “null” to any variable of Value type. Example:-
scala> val myInt : Int = null
:10: error: an expression of type Null is ineligible for implicit conversion
val myInt : Int = null
^ Here type mismatch error. found : Null(null) but required: Int. The implicit conversions between Null and Int are not applicable because they are ambiguous.
In Scala, Unit is used to represent “No value” or “No Useful value”. Unit is a final class defined in “scala” package that is “scala.Unit”. Unit is something similar to Java’s void. But they have few differences.
Both are used to represent a method or function is not returning anything.
In Scala, both val and var are used to define variables. However, they have some significant differences.
REPL stands for Read-Evaluate-Print Loop. We can pronounce it as ‘ripple’. In Scala, REPL is acts as an Interpreter to execute Scala code from command prompt. That’s why REPL is also known as Scala CLI(Command Line Interface) or Scala command-line shell. The main purpose of REPL is that to develop and test small snippets of Scala code for practice purpose. It is very useful for Scala Beginners to practice basic programs. We can access REPL by using “scala” command. When we type “scala” command at CMD Prompt, we will get REPL shell where we can type and execute scala code.
D:\> scala
scala>
Scala Language supports the following features:
We know how to implement loops in Object-Oriented style: Using Mutable Temporary variables, update the variable value and use Loop constructs. It is very tedious and unsafe approach. It is not Thread-Safe. Object-Oriented style uses the following constructs to implement Loops:
We can implement same Loops differently in Functional way. It is Thread-Safe. We can use the following two techniques to implement the loops in functional style:
Scala Application: In Scala, App is a trait defined in scala package like “scala.App”. It defines main method. If an Object or a Class extends this trait, then they will become as Scala Executable programs automatically because they will inherit main method from Application. The main advantage of using App is that we don’t need to write main method. The main drawback of using App is that we should use same name “args” to refer command line argument because scala.App’s main() method uses this name. Example:- Without Scala App:
object MyApp {
def main( args: Array[String]){
println("Hello World!")
}
}
With Scala App:
object MyApp extends App{
println("Hello World!")
}
If we observe above two examples, in second example we have not defined main method because we have inherited from Scala App(Application). Before Scala 2.9, we have scala.Application trait. But it is deprecated by scala.App since Scala 2.9 version.
Java does not support Operator Overloading. Scala supports Operator Overloading. The reason is that Java does not want to support some misleading method names like “+*/”. Scala has given this flexibility to Developer to decide which methods/functions name should use. When we call 2 + 3 that means ‘+’ is not an operator, it is a method available in Int class (or it’s implicit type). Internally, this call is converted into “2.+(3)”.
Expression: Expression is a value that means it will evaluate to a Value. As an Expression returns a value, We can assign it to a variable. Example:- Scala’s If condition, Java’s Ternary operator. Statement: Statement defines one or more actions or operations. That means Statement performs actions. As it does not return a value, we cannot assign it to a Variable. Example:- Java’s If condition.
Java’s “If…Else”: In Java, “If…Else” is a statement, not an expression. It does not return a value and cannot assign it to a variable. Example:-
int year;
if( count == 0)
year = 2014;
else
year = 2015;
Scala’s “If…Else”: In Scala, “If…Else” is an expression. It evaluates a value i.e. returns a value. We can assign it to a variable.
val year = if( count == 0) 2014 else 2015
**NOTE:-**Scala’s “If…Else” works like Java’s Ternary Operator. We can use Scala’s “If…Else” like Java’s “If…Else” statement as shown below:
val year = 0
if( count == 0)
year = 2014
else
year = 2015
In Scala, everything is a value. All Expressions or Statements evaluates to a Value. We can assign Expression, Function, Closure, Object etc. to a Variable. So Scala is an Expression-Oriented Language. In Java, Statements are not Expressions or Values. We cannot assign them to a Variable. So Java is not an Expression-Oriented Language. It is a Statement-Based Language.
**NOTE:-**This list goes beyond one page. However, these are some important points to remember about differences in Scala and Java features to face Scala Interviews.
Scala supports both functions and methods. We use same syntax to define functions and methods, there is no syntax difference. However, they have one minor difference:
NOTE:- We will discuss about Class, Trait,Package, Object etc in my coming posts.
In Java, we can define at-most one public class/interface in a Source file. Unlike Java, Scala supports multiple public classes in the same source file. We can define any number of public classes/interfaces/traits in a Scala Source file.
We know, java.lang is the default package imported into all Java Programs by JVM automatically. We don’t need to import this package explicitly. In the same way, the following are the default imports available in all Scala Programs:
Unlike Java and like C++, Scala supports Operator Overloading. Scala has one and only operator that is “=” (equalto) operator. Other than this all are methods only. For instance 2 + 3, here “+” is not an Operator in Scala. “+” is method available in Int class. Scala Compiler observes 2 and 3 are Integers and tries to find that “+” method in Int class. So Scala Compiler converts “2 + 3” expression into “2.+(3)” and make a call to “+” method on integer object “2” and pass integer object “3” as parameter to “+” method. Both “2 + 3” and “2.+(3)” are equal. It’s just Scala’s syntactic sugar to write programs in Functional style.
Java uses the following keywords extensively:
Scala does not required these two keywords. Scala does not have ‘public’ and ‘static’ keywords.
h3>What is PreDef in Scala? What is the main purpose of PreDef in Scala? In Scala, PreDef is an object defined in scala package as “scala.PreDef”. It is an utility object. It defines many utility methods as shown below:
For instance, print, println, readLine, readInt, require etc methods are defined in PreDef object. In Scala, PreDef is available to use its methods without importing in all Scala Programs because Scala Compiler imports this object into all compilation units like Class, Object, Trait etc. automatically. That’s it all about “Scala Basic Interview Questions and Answers”. We will discuss some Intermediate, Advanced and Real-time Scala Interview Questions and Answers in my coming posts. Please drop me a comment if you like my post or have any issues/suggestions.
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
Sign up for Infrastructure as a Newsletter.
Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Some very well written and succinct answers, thank you.
- Samir Kahvedzic
Really Good One to Understand the SCALA Concepts in Depth . Thank you for for your valuable time for it.
- krishnaiah Chappidi
Thank you. post Scala interview question and ans. post practical example for interview in both spark and scala
- Ajay Jayswal
Thank you. Very nice post.
- Satya
Thank you for this post. There is a small mistake in the third example in 23rd question - reassignment to val. It won’t compile.
- Nick
Scala tutorial is much-needed demand.
- Gaurav Kawatra
Hi Nice blog for scala interview questions so far I found. Sir, your 16th question. In Val and Var diff, you just wrote in reverse way. var is Mutable val is Immutable
- Raviteja
here we found all the interview question which is asked in the interviews. thanks.
- Anurag Singh
Thank you so much.Keep it up.Very Expressive and knowledgeable post.I gained losts of knowledge.
- Kundan Kumar
Thank you Ram, to providing such a useful information likewise also send SPARK related information.Its very helpful for all … Thanks in Advance NarendraReddy.M
- Narendra.M