Apache and Scala Interview Questions

25+ TOP Apache and Scala Interview Questions

Apache and Scala Interview Questions
Apache and Scala Interview Questions

1. What is Scala?
Scala is a Java-based Hybrid programming language which is the fusion of both Functional and Object-Oriented Programming Language features. It can integrate itself with Java Virtual Machine and compile the code written.

2. What is a Scala Map?
Scala Map is a collection of key value pairs wherein the value in a map can be retrieved using the key. Values in a Scala Map are not unique but the keys are unique.

Scala supports two kinds of maps- mutable and immutable. By default, Scala supports immutable map and to make use of the mutable map, programmers have to import the scala.collection.mutable.Map class explicitly. When programmers want to use mutable and immutable map together in the same program then the mutable map can be accessed as mutable.map and the immutable map can just be accessed with the name of the map.

3. What are the advantages of Scala?
Among various other benefits of the language, here are a few:
It is highly scalable
It is highly testable
It is highly maintainable and productive
It facilitates concurrent programming
It is both object-oriented and functional
It has no boilerplate code
Singleton objects are a cleaner solution than static
Scala arrays use regular generics
Scala has native tuples and concise code.

4. Explain how Scala is both Functional and Object-oriented Programming Language?
Scala treats every single value as an Object which even includes Functions. Hence, Scala is the fusion of both Object-oriented and Functional programming features.

5. What is the advantage of using Scala over other functional programming languages?
As the name itself indicates Scala meaning Scalable Language, its high scalable, maintainability, productivity and testability features make it advantageous to use Scala.
Singleton and Companion Objects in Scala provide a cleaner solution unlike static in other JVM languages like Java.
It eliminates the need for having a ternary operator as if blocks’, ‘for-yield loops’, and ‘code’ in braces return a value in Scala.

6. Write a few Frameworks of Scala
Some of the Frameworks supported by Scala are as follows:
Akka Framework
Spark Framework
Play Framework
Scalding Framework
Neo4j Framework
Lift Framework
Bowler Framework.

7. Who designed Scala? Which is the latest version?
At the time of writing, Scala 2.12.6 is the latest version. The interviewer may ask you this to find out whether you keep yourself updated. Martin Odersky, a German computer scientist, began designing it in 2001 at EPFL, Switzerland.

8. What is a monad in Scala?
A monad is something to which we can pass functions and manipulate the underlying object’s data. We don’t need to manipulate the object directly. Hence, a monad is an object that wraps another.

9. Is a case class the same as a regular class in Scala?
No, these aren’t synonyms. Here are a few important characteristics of a Scala case class:

They support pattern-matching
To create an instance of a case class, you don’t need new
Scala automatically generates methods like equals(), hashcode(), and toString() for case classes
For all constructor arguments for a case class, Scala automatically generates accessor methods
For more on case classes, read up on Case Classes in Scala.

10. What is the advantage of companion objects in Scala?
Classes in Scala programming language do not have static methods or variables but rather they have what is known as a Singleton object or Companion object. The companion objects in turn are compiled to classes which have static methods.

A singleton object in Scala is declared using the keyword object as shown below –

object Main {

def sayHello () {

println (“Hello!”);

}

}

In the above code snippet, Main is a singleton object and the method sayHello can be invoked using the following line of code –

Main. SayHello ();

If a singleton object has the same name as that of the class then it is known as a Companion object and it should be defined in the same source file as that of the

class.

class Main {

def sayHelloWorld() {

println(“Hello World”);

}

}

object Main {

def sayHello() {

println(“Hello!”);

}

}

Advantages of Companion Objects in Scala
Companion objects are beneficial for encapsulating things and they act as a bridge for writing functional and object oriented programming code.
Using companion objects, the Scala programming code can be kept more concise as the static keyword need not be added to each and every attribute.
Companion objects provide a clear separation between static and non-static methods in a class because everything that is located inside a companion object is not a part of the class’s runtime objects but is available from a static context and vice versa.

11. Which Scala library is used for functional programming?
Scalaz library has purely functional data structures that complement the standard Scala library. It has pre-defined set of foundational type classes like Monad, Functor, etc.

12. What do you understand by “Unit” and “()” in Scala?
Unit is a subtype of scala.anyval and is nothing but Scala equivalent of Java void that provides the Scala with an abstraction of the java platform. Empty tuple i.e.

() in Scala is a term that represents unit value.

13. Is Scala compatible with Java? Explain.
We’ve seen that both Scala and Java work on the JVM on the backend. Well, Scala classes are Java classes and Java classes are Scala classes too. So you can call a Java method from a Scala method, and vice-versa. You can also extend classes from one language in another. However, features like traits in Scala have no equivalents in Java.

14. What is a lens in Scala?
A lens is an abstraction from functional programming. It makes updating complex immutable nested objects easier for us.
For lenses, we have three kinds of available implementations:
scalaz.Lens
Quicklens- Has more functionality than a Sauron
Sauron

15. Explain Streams in Scala.
In simple words, we define Stream as a Lazy list which evaluates the elements only when it needs to. This sort of lazy computation enhances the Performance of the program.

16. What is Recursion tail in Scala?
‘Recursion’ is a function that calls itself. For example, a function ‘A’ calls function ‘B’, which calls the function ‘C’. It is a technique used frequently in

Functional programming. In order for a Tail recursive, the call back to the function must be the last function to be performed.

17. Explain the use of Tuples in Scala?
Scala tuples combine a Finite number of items together so that the programmer can Pass a tuple around as a Whole. Unlike an Array or List, a tuple is Immutable and can hold objects with different Datatypes.

18. Why do we need App in Scala?
App is a helper class that holds the main method and its Members together. The App trait can be used to quickly turn Objects into Executable programs. We can have our classes extend App to render the executable code.

object Edureka extends App{
println(“Hello World”)
}

19. Explain the scope provided for variables in Scala.
There are three different scopes depending upon their use. Namely:

Fields:
Fields are variables declared inside an object and they can be accessed anywhere inside the program depending upon the access modifiers. Fields can be declared using var as well as val.

Method Parameters:
Method parameters are strictly Immutable. Method parameters are mainly used to Pass values to the methods. These are accessed inside a method, but it is possible to access them from outside the method provided by a Reference.

Local Variables:
Local variables are declared inside a method and they are accessible only inside the method. They can be accessed if you return them from the method.

20. Explain the Scala Anonymous Function.
In the Source code, Anonymous functions are called ‘Function literals’ and at run time, function literals are instantiated into objects called Function values. Scala provides a relatively easy Syntax for defining Anonymous functions.

//Syntax
(z:Int, y:Int)=> z*y
Or
(_:Int)*(_Int)

21. Differentiate between Val and var in Scala.
Val and var are the two keywords used to define variables in Scala. Var keyword is just similar to variable declaration in Java whereas Val is little different. Once a variable is declared using Val the reference cannot be changed to point to another reference. This functionality of Val keyword in Scala can be related to the functionality of java final keyword. To simplify it, Val refers to immutable declaration of a variable whereas var refers to mutable declaration of a variable in Scala.

22. What do you understand by a closure in Scala?
Closure is a function in Scala where the return value of the function depends on the value of one or more variables that have been declared outside the function.

23. What is Scala Future? How it differs from java.util.concurrent.Future?
Scala Future is a monadic collection, which starts a background task. It is an object which holds the potential value or future value, which would be available after the task is completed. It also provides various operations to further chain the operations or to extract the value. Future also provide various call-back functions like onComplete, OnFailure, onSuccess to name a few, which makes Future a complete concurrent task class. The main and foremost difference between Scala’s Future and Java’s Future class is that the later does not provide promises/callbacks operations. The only way to retrieve the result is Future.get () in Java.

24. What is a Scala Trait?
A trait is a special kind of Class that enables the use of multiple inheritance. Although a trait can extend only one class, but a class can have multiple traits.

However, unlike classes, traits cannot be instantiated.

25. What is tail-recursion in Scala?
There are several situations where programmers have to write functions that are recursive in nature. The main problem with recursive functions is that, it may eat up all the allocated stack space. To overcome this situation, Scala compiler provides a mechanism “tail recursion” to optimize these recursive functions so that it does not create new stack space, instead uses the current function stack space. To qualify for this, annotation “@annotation.tailrec” has to be used before defining the function and recursive call has to be the last statement, then only the function will compile otherwise, it will give an error.

Frequently Asked Scala Interview Questions