Top 100+ Scala Interview Questions And Answers
Question 1. Explain What Is Scala?
Answer :
Scala is an item practical programming and scripting language for preferred software programs designed to explicit answers in a concise manner.
Question 2. What Is A ‘scala Set’? What Are Methods Through Which Operation Sets Are Expressed?
Answer :
Scala set is a collection of pairwise factors of the equal kind. Scala set does no longer include any replica elements. There are varieties of units, mutable and immutable.
Python Interview Questions
Question 3. What Is A ‘scala Map’?
Answer :
Scala map is a collection of key or cost pairs. Based on its key any fee may be retrieved. Values aren't specific however keys are specific within the Map.
Question four. What Is The Advantage Of Scala?
Answer :
Less mistakes susceptible practical fashion
High maintainability and productivity
High scalability
High testability
Provides features of concurrent programming
Python Tutorial
Question five. In What Ways Scala Is Better Than Other Programming Language?
Answer :
The arrays makes use of normal generics, at the same time as in other language, generics are bolted on as an afterthought and are completely separate however have overlapping behaviours with arrays.
Scala has immutable “val” as a first elegance language feature. The “val” of scala is much like Java final variables.  Contents can also mutate however top  reference is immutable.
Scala we could ‘if blocks’, ‘for-yield loops’, and ‘code’ in braces to go back a fee. It is more top-rated, and removes the need for a separate ternary operator.
Singleton has singleton objects instead of C++/Java/ C# classic static.  It is a cleaner answer
Persistent immutable collections are the default and built into the standard library.
It has local tuples and a concise code
It has no boiler plate code
Adv Java Interview Questions
Question 6. What Are The Scala Variables?
Answer :
Values and variables are shapes that are available Scala. A price variable is constant and can't be modified as soon as assigned. It is immutable, at the same time as a ordinary variable, alternatively, is mutable, and you may alternate the price.
The sorts of variables are
var  myVar : Int=0;
val   myVal: Int=1;
Question 7. Mention The Difference Between An Object And A Class ?
Answer :
A magnificence is a definition for an outline. It defines a kind in terms of methods and composition of different types. A class is a blueprint of the item. While, an object is a singleton, an example of a category which is unique. An nameless elegance is created for each object within the code, it inherits from something instructions you declared object to enforce.
Adv Java Tutorial Hadoop Interview Questions
Question 8. What Is Recursion Tail In Scala?
Answer :
‘Recursion’ is a characteristic that calls itself. A feature that calls itself, as an instance, a feature ‘A’ calls characteristic ‘B’, which calls the characteristic ‘C’. It is a technique used frequently in useful programming. In order for a tail recursive, the decision back to the function ought to be the closing function to be carried out.
Question 9. What Is ‘scala Trait’ In Scala?
Answer :
‘Traits’ are used to outline object types designated with the aid of the signature of the supported strategies. Scala lets in to be partially applied but tendencies might not have constructor parameters. A trait consists of technique and area definition, by using mixing them into training it is able to be reused.
Ruby on Rails Interview Questions
Question 10. When Can You Use Traits?
Answer :
There isn't any particular rule while you may use tendencies, but there is a tenet which you can consider.
If the behaviour will no longer be reused, then make it a concrete elegance. Anyhow it isn't always a reusable behaviour.
In order to inherit from it in Java code, an summary magnificence can be used.
If efficiency is a priority then lean in the direction of the usage of a category
Make it a trait if it is probably reused in a couple of and unrelated lessons. In extraordinary components of the elegance hierarchy best traits may be combined into different parts.
You can use abstract class, in case you want to distribute it in compiled form and expects outdoor companies to jot down instructions inheriting from it.
Hadoop Tutorial
Question eleven. What Is Case Classes?
Answer :
Case training presents a recursive decomposition mechanism through sample matching, it is a regular classes which export their constructor parameter. The constructor parameters of case classes may be accessed immediately and are handled as public values.
Apache Cassandra Interview Questions
Question 12. What Is The Use Of Tuples In Scala?
Answer :
Scala tuples integrate a hard and fast wide variety of gadgets collectively in order that they may be surpassed around as complete. A tuple is immutable and may hold items with differing types, not like an array or list.
Python Interview Questions
Question thirteen. What Is Function Currying In Scala?
Answer :
Currying is the approach of reworking a function that takes multiple arguments into a function that takes a unmarried argument Many of the equal techniques as language like Haskell and LISP are supported by using Scala. Function currying is one of the least used and misunderstood one.
Ruby on Rails Tutorial
Question 14. What Are Implicit Parameters In Scala?
Answer :
Implicit parameter is the way that allows parameters of a technique to be “determined”. It is much like default parameters, however it has a special mechanism for locating the “default” price. The implicit parameter is a parameter to technique or constructor this is marked as implicit. This manner if a parameter value is not stated then the compiler will look for an “implicit” price defined inside a scope.
Question 15. What Is A Closure In Scala?
Answer :
A closure is a feature whose go back value relies upon on the fee of the variables declared outside the function.
Prolog Interview Questions
Question 16. What Is Monad In Scala?
Answer :
A monad is an item that wraps any other object. You skip the Monad mini-applications, i.E functions, to perform the statistics manipulation of the underlying item, in place of manipulating the object at once. Monad chooses a way to follow the program to the underlying item.
Apache Cassandra Tutorial
Question 17. What Is Scala Anonymous Function?
Answer :
In a supply code, anonymous capabilities are called ‘feature literals’ and at run time, function literals are instantiated into gadgets called function values. Scala provides a exceedingly easy syntax for defining nameless capabilities.
Apache Spark Interview Questions
Question 18. Explain ‘scala Higher Order’ Functions?
Answer :
Scala permits the definition of better order features. These are features that take different functions as parameters, or whose result is a feature. In the following example, practice () characteristic takes some other function ‘f’ and a cost ‘v’ and applies function to v.
Example:
item Test 
 def main(args: Array[String]) 
 println( observe( format, 10) )
 
 def practice(f: Int => String, v: Int) = f(v)
 def layout[A](x: A) = "[" + x.ToString() + "]"
When the above code is compiled and performed, it produces following end result.
         C:/>scalac Test.Scala
          C:/>scala Test
          [10]
          C:/>
Adv Java Interview Questions
Question 19. What Is The Difference Between Var And Value?
Answer :
In scala, you could define a variable the usage of either a, val or var key phrases. The difference between val and var is, var is much like java assertion, however val is little one-of-a-kind. We cannot exchange the connection with factor to every other reference, once the variable is asserted the usage of val. The variable described using var key phrases are mutable and may be changed any quantity of times.
Apache Solr Tutorial
Question 20. What Are Option, Some And None In Scala?
Answer :
‘Option’ is a Scala generic type that could either be ‘some’ accepted price or none. ‘Queue’ frequently makes use of it to symbolize primitives that can be null.
Apache Solr Interview Questions
Question 21. How Do I Append To The List?
Answer :
In scala to append right into a list, use “:+” single price
       var myList = List.Empty[String]
       myList :+= "a"
       myList :+= "b"
       myList :+= "c"
       use++ for appending a listing
       var myList = List.Empty[String]
       myList ++= List("a", "b", "c")
Question 22. How Can You Format A String?
Answer :
To layout a string, use the .Format () approach, in scala you may use
Val formatted= “%s %i”.Layout (mystring.MyInt)
Spark SQL Programming Tutorial
Question 23. Why Scala Prefers Immutability?
Answer :
Scala prefers immutability in layout and in lots of instances makes use of it as default. Immutability can assist whilst dealing with equality issues or concurrent applications.
NoSQL Interview Questions
Question 24. What Are The Four Types Of Scala Identifiers ?
Answer :
The 4 forms of identifiers are
Alpha numeric identifiers
Operator identifiers
Mixed identifiers
Literal identifiers
Hadoop Interview Questions
Question 25. What Are The Different Types Of Scala Literals?
Answer :
The exclusive varieties of literals in scala are
Integer literals
Floating point literals
Boolean literals
Symbol literals
Character literals
String literals
Multi-Line strings
MongoDB Tutorial
Question 26. What Is Primary Constructor? What Is Secondary Or Auxiliary Constructor In Scala? What Is The Purpose Of Auxiliary Constructor In Scala? Is It Possible To Overload Constructors In Scala?
Answer :
Scala has two sorts of constructors:
Primary Constructor
Auxiliary Constructor
Primary Constructor: In Scala, Primary Constructor is a constructor that is defined with elegance definition itself. Each magnificence ought to have one Primary Constructor: Either Parameter constructor or Parameterless constructor.
Example:-
elegance Person
Above Person magnificence has one Zero-parameter or No-Parameter or Parameterless Primary constructor to create instances of this elegance.
Magnificence Person (firstName: String, lastName: String)
Above Person elegance has a two Parameters Primary constructor to create instances of this elegance.
Auxiliary Constructor: Auxiliary Constructor is likewise called Secondary Constructor. We can declare a Secondary Constructor using ‘def’ and ‘this’ keywords as proven beneath:
elegance Person (firstName: String, middleName:String, lastName: String)
  def this(firstName: String, lastName: String)
      this(firstName, "", lastName)
  
Spark SQL Programming Interview Questions
Question 27. What Is The Use Of Auxiliary Constructors In Scala?Please Explain The Rules To Follow In Defining Auxiliary Constructors In Scala?
Answer :
In Scala, The most important reason of Auxiliary Constructors is to overload constructors. Like Java, We can offer numerous styles of constructors in order that use can pick the proper one primarily based on his requirement.
Auxiliary Constructor Rules:
They are like strategies only. Like methods, we must use ‘def’ key-word to outline them.
We must use equal name ‘this’ for all Auxiliary Constructors.
Each Auxiliary Constructor need to begin with a name to previous defined some other Auxiliary Constructor or Primary Constructor. Otherwise bring together-time mistakes.
Each Auxiliary Constructor should range with their parameters listing: may be through variety or types.
Auxiliary Constructors can not call a amazing magnificence constructors. They must name them via Primary Constructor simplest.
All Auxiliary Constructors name their Primary Constructor either directly or not directly via other Auxiliary Constructors.
NOTE:- If you need to study Scala’s Constructors, please refer my Scala posts at: Primary Constructor and Auxiliary Constructor.
Ruby on Rails Interview Questions
Question 28. What Are The Differences Between Array And Arraybuffer In Scala?
Answer :
Differences among Array and ArrayBuffer in Scala:
Array is fixed length array. We cannot alternate its size as soon as its created.
ArrayBuffer is variable length array. It can increase or lower it’s size dynamically.
Array is something just like Java’s primitive arrays.
ArrayBuffer is something similar to Java’s ArrayList.
Lucene Tutorial
Question 29. What Is Case Class? What Is Case Object? What Are The Advantages Of Case Class?
Answer :
Case class is a class that is defined with “case magnificence” key phrases. Case item is an object that's defined with “case object” keywords. Because of this “case” keyword, we will get some benefits to avoid boilerplate code.
We can create case magnificence items with out the usage of “new” key-word. By default, Scala compiler prefixes “val” for all constructor parameters. That’s why with out the usage of val or var, Case class’s constructor parameters becomes magnificence participants, it isn't possible for ordinary lessons.
Advantages of case magnificence:
By default, Scala Compiler provides toString, hashCode and equals methods. We can avoid writing this boilerplate code.
By default, Scala Compiler adds companion item with observe and unapply techniques that’s why we don’t want new keyword to create instances of a case class.
By default, Scala Compiler adds reproduction technique too.
We can use case lessons in Pattern Matching.
By default, Case elegance and Case Objects are Serializable.
MongoDB Interview Questions
Question 30. When Compare To Normal Class, What Are The Major Advantages Or Benefits Of A Case-class?
Answer :
The following are the fundamental benefits or blessings of a Case class over Normal Classes:
Avoids plenty of boiler-plate code by way of including a few useful strategies automatically.
By default, helps Immutability as it’s parameters are ‘val’
Easy to apply in Pattern Matching.
No need to use ‘new’ keyword to create example of Case Class.
By default, helps Serialization and Deserialization.
Question 31. What Is The Usage Of Isinstanceof And Asinstanceof Methods In Scala? Is There Anything Similar Concept Available In Java?
Answer :
Both isInstanceOf and asInstanceOf methods are defined in Any class. So no want import to get these methods into any class or object.
“isInstanceOf” method is used to check whether or not the item is of a given kind or no longer. If so, it returns real. Otherwise returns fake.
Scala> val str = "Hello"
scala>str.IsInstanceOf[String]
res0: Boolean = false  
“asInstanceOf” approach is used to solid the item to the given a type. If the given object and type are of identical kind, then it forged to given kind. Otherwise, it throws java.Lang.ClassCastException.
Scala> val str = "Hello".AsInstanceOf[String]
str: String = Hello
In Java, ‘instanceof’ keyword is just like Scala’s ‘isInstanceOf’ approach. In Java, the following type of guide kind casting is just like Scala’s ‘asInstanceOf’ approach.
AccountService carrier = (AccountService)
 context.GetBean("accountService");
Question 32. How Do You Prove That By Default, Case Object Is Serializable And Normal Object Is Not?
Answer :
Yes, By Default, Case Object is Serializable. But normal object isn't. We can prove this by using the use of isInstanaceOf method as proven underneath:
scala> object MyNormalObject
described item MyNormalObject
scala> MyNormalObject.IsInstanceOf[Serializable]
res0: Boolean = fake
scala> case object MyCaseObject
defined item MyCaseObject
scala> MyCaseObject.IsInstanceOf[Serializable]
res1: Boolean = authentic
Question 33. Difference Between Array And List In Scala?
Answer :
Arrays are always Mutable in which as List is continually Immutable.
Once created, We can trade Array values where as we cannot change List Object.
Arrays are constant-length records systems where as List is variable-sized facts structures. List’s length is mechanically extended or reduced primarily based on it’s operations we carry out on it.
Arrays are Invariants wherein as Lists are Covariants.
Apache Cassandra Interview Questions
Question 34. What Is The Difference Between “val” And “lazy Val” In Scala? What Is Eager Evaluation? What Is Lazy Evaluation?
Answer :
As we mentioned in my Basic Scala Interview Questions, “val” means value or consistent that's used to outline Immutable variables.
There are sorts of program evaluations:
Eager Evaluation
Lazy Evaluation
Eager Evaluation method evaluating application at assemble-time or program deployment-time irrespective of clients are the use of that software or now not.
Lazy Evaluation method comparing program at run-time on-call for that means whilst clients get entry to the program then best its evaluated.
The difference among “val” and “lazy val” is that “val” is used to outline variables which are evaluated eagerly and “lazy val” is also used to define variables however they may be evaluated lazily.
Question 35. What Is The Relationship Between Equals Method And == In Scala? Differentiate Scala’s == And Java’s == Operator?
Answer :
compare instances with ==, Scala calls that object’s equals() approach robotically.
Java’s == operator is used to test References Equality that is whether or not references are pointing to the same item or now not. Scala’s == is used to check Instances Equality that is whether or not two times are same or now not.
Question 36. Difference Between Scala’s Inner Class And Java’s Inner Class?
Answer :
In Java, Inner magnificence is related to Outer elegance this is Inner elegance a member of the Outer magnificence.
Unlike Java, Scala treats the relationship among Outer elegance and Inner magnificence in a different way. Scala’s Inner magnificence is related to Outer class item.
Prolog Interview Questions
Question 37. What Is Diamond Problem? How Scala Solves Diamond Problem?
Answer :
A Diamond Problem is a Multiple Inheritance problem. Some humans calls this trouble as Deadly Diamond Problem.
In Scala, it takes place while a Class extends more than one Traits that have equal approach definition.
Unlike Java 8, Scala solves this diamond hassle routinely via following some rules defined in Language. Those regulations are known as “Class Linearization”.
Example:-
trait A   
  def show() println("From A.Show")  
trait B extends A 
  override def show()  println("From B.Show") 
trait C extends A 
  override def display()  println("From C.Display") 
elegance D extends B with C 
 
object ScalaDiamonProblemTest extends App 
    val d = new D
    d show
Here output is “From C.Display” form trait C. Scala Compiler reads “extends B with C” from proper to left and takes “show” method definition from lest maximum trait that is C.
Question 38. Why Scala Does Not Have “static” Keyword? What Is The Main Reason For This Decision?
Answer :
As we recognize, Scala does NOT have “static” key-word in any respect. This is the layout selection performed by means of Scala Team.
The foremost reason to take this choice is to make Scala as a Pure Object-Oriented Language. “static” keyword method that we will get admission to that class contributors without creating an item or without the use of an object. This is completely against with OOP standards.
If a Language helps “static” keyword, then that Language isn't a Pure Object-Oriented Language. For instance, as Java helps “static” keyword, it's miles NOT a Pure Object-Oriented Language. But Scala is a Pure Object-Oriented Language.
Question 39. What Is The Use Of “item” Keyword In Scala? How To Create Singleton Objects In Scala?
Answer :
In Scala, item keyword is used the subsequent purposes:
It is used to create singleton item in Scala.
Object MySingletonObject
Here, MySingletonObject will become singleton item automatically.
Object keyword is used to outline Scala Applications that is executable Scala packages.
Item MyScalaExecutableProgram   
   def primary(args: Array[String])
       println("Hello World")
   
When we outline primary method in item as proven above (its equal as most important() technique in Java), it becomes routinely as a executable Scala software.
It is used to outline static members like static variables and static techniques with out using ‘static’ key-word.
Item MyScalaStaticMembers 
  val PI: Double = three.1414 
  def upload(a: Int, b: Int) = a + b
By def PI variable and add strategies becomes as static contributors. That means we will name them without developing a separate object like MyScalaStaticMembers.Add(10,20).
Question forty. How To Define Factory Methods Using Object Keyword In Scala? What Is The Use Of Defining Factory Methods In Object?
Answer :
In Scala, we use ‘object’ key-word to define Factory methods. The foremost reason of these Factory techniques in Scala is to avoid the usage of ‘new’ keyword. Without the usage of ‘new’ key-word we can create items.
To define Factory strategies:
We can use follow method to outline Factory strategies in Scala. If we've Primary Constructor and Multiple Auxiliary constructors, then we need to outline multiple observe methods as shown below.
Class Person(val firstName: String, val middleName: String, val lastName: String)
  def this(firstName: String, lastName: String)
    this(firstName,"",lastName)
  
item Person
  def practice(val firstName: String, val middleName: String, val lastName: String) 
        = new Person(firstName,middleName,lastName)
 
  def observe(val firstName: String, val lastName: String) 
        = new Person(firstName, lastName)
Now we are able to create Person items with out the usage of new key-word or with new key-word upto your want.
Val p1 = new Person("Scala","Java")
or 
val p1 = Person("Scala","Java")
Apache Spark Interview Questions
Question forty one. What Is Apply Method In Scala? What Is Unapply Method In Scala? What Is The Difference Between Apply And Unapply Methods In Scala?
Answer :
In Scala, apply and unapply techniques play very critical position. They also are very beneficial in Play Framework in mapping and unmapping records between Form facts and Model records.
In simple words,
follow technique: To compose or collect an object from it’s components.
Unapply technique: To decompose or dis-collect an item into it’s additives.
Scala’s apply method: It is used to compose an item with the aid of the usage of its additives. Suppose if we want to create a Person object, then use firstName and laststName additives and compose Person object as proven beneath.
Elegance Person(val firstName: String, val lastName: String)
object Person
  def follow(firstName: String, lastName: String) 
        = new Person(firstName, lastName)
Scala’s unapply approach:
It is used to decompose an object into its components. It follows opposite process of practice method. Suppose if we've a Person object, then we can decompose this item into it’s two components: firstName and laststName as shown below.
Class Person(val firstName: String, val lastName: String)
item Person
  def observe(firstName: String, lastName: String) 
        = new Person(firstName, lastName)
 
    def unapply(p: Person): (String,String) 
        = (p.FirstName, p.LastName)
Question forty two. How Does It Work Under-the-hood, When We Create An Instance Of A Class Without Using ‘new’ Keyword In Scala? When Do We Go For This Approach? How To Declare Private Constructors In Scala?
Answer :
In Scala, when we create an example of a Class with out the use of ‘new’ keyword, internally it make a call to appropriate follow approach to be had in Companion item. Here appropriate apply technique approach that matched with parameters.
When will we select this selection: When we want to provide private non-public constructor and we want to keep away from the usage of ‘new’ key-word, we are able to put into effect only follow approach with same set of parameters and permit our magnificence customers to create it without new key-word.
Apache Solr Interview Questions
Question forty three. How Do We Declare A Private Primary Constructor In Scala? How Do We Make A Call To A Private Primary Constructor In Scala?
Answer :
In Scala, we will declare a private Primary Constructor very effortlessly. Just define a Primary Constructor as it's far and add ‘personal’ just after class name and before parameter list as proven underneath:
magnificence Person non-public (name: String)
item Person
 def observe(call: String) = new Person(name)
As it’s a non-public constructor, we cannot call it from outdoor. We should offer a factory approach (that is follow approach) as proven above and use that constructor indirectly.
Question forty four. Does A Companion Object Access Private Members Of It’s Companion Class In Scala?
Answer :
Generally, non-public individuals manner on hand best within that class. However Scala’s Companion elegance and Companion Object has supplied some other feature.
In Scala, a Companion object can get entry to private participants of it’s Companion elegance and Companion elegance can get entry to it’s Companion item’s private participants.
Question 45. What Is The Main Design Decision About Two Separate Keywords: Class And Object In Scala? How Do We Define Instance Members And Static Members In Scala?
Answer :
In Scala, we use elegance key-word to outline example contributors and object key-word to define static members. Scala does not have static key-word, however still we are able to define them by the usage of object keyword.
The essential layout selection approximately that is that the clear separation among example and static members. Loosely coupling among them. And other fundamental purpose is to avoid static key-word in order that Scala becomes a Pure-OOP Language.
Question 46. What Is Object In Scala? Is It A Singleton Object Or Instance Of A Class?
Answer :
Unlike Java, Scala has two meanings approximately ‘object’. Don’t get confuse approximately this, I will give an explanation for it truly. In Java, we've most effective one meaning for object this is “An instance of a class”.
Like Java, the primary which means of object is “An example of a category”.
Val p1 = new Person("Scala","Java")
or 
val p1 = Person("Scala","Java")
Second meaning is that object is a key-word in Scala. It is used to outline Scala Executable packages, Companion Objects, Singleton Objects and so on.
Question forty seven. What Is A Companion Object In Scala? What Is A Companion Class In Scala? What Is The Use Of Companion Object In Scala?
Answer :
In easy phrases, if a Scala elegance and object shares the identical call and described within the equal supply document, then that class is known as “Companion Class” and that item is known as “Companion Object”.
When we create a Class by way of the use of Scala “elegance” key-word and Object by using the usage of Scala “object” key-word with equal call and inside the equal supply file, then that class is known as “Companion Class” and that item is called “Companion Object”.
Example:-
Employee.Scala
magnificence Employee 
object Employee 
In Scala, The main cause of Companion Object is to outline apply methods and avoid the usage of new key-word in developing an instance of that Companion class item.
Question forty eight. How To Implement Interfaces In Scala?
Answer :
As we understand from Java historical past, we use interface to outline contact.
However, there may be no interface concept in Scala. Even, Scala doesn’t have interface keyword. Scala has a greater powerful and flexible idea i.E. Trait for this cause.
Question 49. What Is Range In Scala? How To Create A Range In Scala?
Answer :
Range is a Lazy Collection in Scala. Range is a category available in ‘scala’ package deal like ‘scala.Range’. It is used to represent a sequence of integer values. It is an ordered sequence of integers.
Example:-
scala> 1 to 10
res0: scala.Collection.Immutable.Range.Inclusive = Range(1, 2, three, four, five, 6, 7, eight, 9, 10)
scala> 1 till 10
res1: scala.Series.Immutable.Range = Range(1, 2, three, 4, 5, 6, 7, 8, 9)
Question 50. How Many Values Of Type Nothing Have In Scala?
Answer :
In Scala, Nothing kind don't have any values this is 0. It does no longer have any values. It is a subtype of all Value instructions and Reference instructions.
Question 51. How Many Values Of Type Unit Have In Scala?
Answer :
In Scala, Unit is some thing much like Java’s void key-word. It is used to symbolize “No fee exists”. It has one and most effective one fee that is ().
Question 52. What Is A Pure Function?
Answer :
A natural characteristic is a function without any observable aspect-outcomes. That method it returns always equal effects irrespective how typically we call it with equal inputs.
A natural characteristic always offers identical output for the equal inputs.
For Example:-
scala> 10 + 20
res0: Int = 30
scala>
scala> 10 + 20
res0: Int = 30
Here “+” a pure feature available in Int elegance. It gives same end result 30 for equal inputs 10 and 30, irrespective how in many instances we name it.
Question 53. In Fp, What Is The Difference Between A Function And A Procedure?
Answer :
Both are used to carry out computation, but they have got one major difference in Functional Programming global.
A function is a computation unit with out facet-impact where as a Procedure is likewise a computation unit with side-consequences.
Question 54. What Are The Major Differences Between Scala’s Auxiliary Constructors And Java’s Constructors?
Answer :
Scala’s Auxiliary constructor is nearly much like Java’s constructor with few variations.
Compared to Java’s constructors, Auxiliary constructors have the following few variations:
The auxiliary constructors are known as using “this” key-word.
All auxiliary constructor are described with the identical name that is “this”. In Java, we use class name to outline constructors.
Each auxiliary constructor need to start with a call to a previously described auxiliary constructor or the primary constructor.
We use ‘def’ key-word to define auxiliary constructors like approach/function definition. In Java, constructor definition and Method definition is distinctive.
Question fifty five. What Is The Use Of ‘yield’ Keyword In Scala’s For-comprehension Construct?
Answer :
We can use ‘yield’ keyword in Scala’s for-comprehension assemble. ‘for/yield’ is used to iterate a group of factors and generates new collection of equal kind. It does now not exchange the unique collection. It generates new collection of identical type as authentic collection type.
For example, if we use ‘for/yield’ assemble to iterate a List then it generates a new List best.
Scala> val listing = List(1,2,three,four,five)
list: List[Int] = List(1, 2, three, 4, five)
scala> for(l <- list) yield l*2
res0: List[Int] = List(2, 4, 6, 8, 10)
Question 56. What Is Guard In Scala’s For-comprehension Construct?
Answer :
In Scala, for-comprehension construct has an if clause which is used to write a condition to filter some elements and generate new collection. This if clause is also known as “Guard”.
If that guard is true, then add that element to new collection. Otherwise, it does not add that element to original collection.
Example:- For-comprehension Guard to generate only Even numbers into new collection.
Scala> val list = List(1,2,3,four,5,6,7,eight,9,10)
list: List[Int] = List(1, 2, 3, 4, 5 , 6 , 7 , 8 , nine , 10)
scala> for(l <- list if l % 2 =0 ) yield l
res0: List[Int] = List(2, 4, 6, 8, 10)
Question 57. How Scala Solves Inheritance Diamond Problem Automatically And Easily Than Java 8?
Answer :
If we use Java 8’s Interface with Default methods, we will get Inheritance Diamond Problem. Developer has to solve it manually in Java 8. It does not provide default or automatic resolution for this problem.
In Scala, we will get same problem with Traits but Scala is very clever and solves Inheritance Diamond Problem automatically using Class Linearization concept.
Question 58. In Scala, Pattern Matching Follows Which Design Pattern? In Java, ‘isinstanceof’ Operator Follows Which Design Pattern?
Answer :
In Scala, Pattern Matching follows Visitor Design Pattern. In the same way, Java’s ‘isinstanceof’ operator also follows Visitor Design Pattern.
Question 59. What Is The Current Latest Version Of Scala? What Is The Major Change Or Update In Scala 2.12?
Answer :
Current Scala’s stable is 2.11.7. It supports Java SE 7.
The major change or update in Scala 2.12 version is that it supports Java SE 8 or later versions only. Scala 2.12 is not a binary compatible with the 2.11.X series. It’s still in Mile Stone Builds only.
Question 60. What Is Option In Scala? What Are Some And None? What Is Option/some/none Design Pattern In Scala?
Answer :
In Scala, Option is used to represent optional values that is either exist or not exist.
Option is an abstract class. Option has two subclasses: Some and None. All three (Option, Some and None) are defined in “scala” package like “scala.Option”.
Option is a bounded collection in Scala, which contains either zero or one element. If Option contains zero elements that is None. If Option contains one element, that is Some.
Some is used to represent existing value. None is used to represent non-existent value.
Example:-
def get(val index: Int): Option[String]
Let us assume that this method is from List. This method has a return type of Option[String]. If List contains elements, this get method returns “Some[String]” element available in that index position. Otherwise, it returns “None” (that is no elements)
Some is a case class and None is an Object. As both are case class/object, we can use them in Pattern Matching very well.
The combination of all these three definitions is known as Option/Some/None Design Pattern in Scala.
Question 61. What Is Either In Scala? What Are Left And Right In Scala? Explain Either/left/right Design Pattern In Scala?
Answer :
In Scala, Either is an abstract class. It is used to represent one value of two possible types. It takes two type parameters: Either[A,B].
It exactly have two subtypes: Left and Right. If Either[A,B] represents an instance A that means it is Left. If it represents an instance B that means it is Right.
This is known as Either/Left/Right Design Pattern in Scala.
Question 62. What Is The Equivalent Construct Of Scala’s Option In Java Se 8? What Is The Use Of Option In Scala?
Answer :
Scala’s Option is similar to Java SE 8’s Optional. Java SE 8 has introduced a new utility class Optional to represent existing or non-existing of some value. Optional is available in java.Util package.
Both Scala’s Option and Java SE 8’s Optional are used to represent optional values. Both are used to avoid unwanted null checks and NullPointerException.
Question 63. What Are The Advantages Of Functional Programming (fp) Or Advantages Of Pure Functions?
Answer :
The following are the Advantages of Functional Programming (FP) or Advantages of Pure Functions:
More Modular
Easier to understand Or Easier reason about
Easier to test
Less prone to bugs
Easier to reuse
Easier to Parallelism and generalize
Question 64. What Are The Popular Scala-based Frameworks To Develop Restful Web Services Or Rest Api?
Answer :
There are many Scala-Based Framework to develop RESTful Web Services. Most popular frameworks are:
Play Framework: n Play, we call REST API URLs as routes. We place all routes at once place in Play framework. It is a stateless web framework to develop REST API easily.
Scalatra Framework: It is very simple and easy Scala-based web framework to develop REST API
Spray Framework: It is very concise and built on top of Akka framework so it’s better to develop REST API using Actor Model.
Lift Framework: It allows routing using Pattern Matching concept.
Question 65. What Is The Best Framework To Generate Rest Api Documentation For Scala-based Applications?
Answer :
Swagger is is the best tool for this purpose. It is very simple and open-source tool for generating REST APIs documentation with JSON for Scala-based applications.
If we use Play with Scala to develop your REST API, then use play-swagger module for REST API documentation.
If we use Spray with Scala to develop your REST API, then use spray-swagger module for REST API documentation.
Question 66. Like Hibernate For Java-based Applications, What Are The Popular Orm Frameworks Available To Use In Play/scala Based Applications?
Answer :
Like JPA, Hibernate and Toplink etc ORM Frameworks for Java-based applications, There are many ORM frameworks to use in Play/Scala based applications.
Popular ORM frameworks for Play/Scala based applications:
Slick
Anorm
SORM(Scala ORM)
Squeryl
Question 67. What Is The Best Tool To Develop Play/scala Applications To Persist Data In Mongodb Nosql Data Store?
Answer :
ReactiveMongo is the best Scala Driver to develop Play/Scala applications to persist data in MongoDB NoSQL data store. It supports fully non-blocking and asynchronous I/O operations.
Question 68. Popular Clients Who Are Using Play And Scala To Develop Their Applications?
Answer :
Thousands of clients are using Play and Scala in Production. The following list is the more popular clients who are using Play and Scala actively.
LinkedIn
The Guardian
Ocado
LuchidChart
GOV.UK
Question 69. What Is The Best Language To Use With Play Framework: Scala Or Java?
Answer :
Play 2 is completely written in Scala. If we use Java with Play framework, we need to face many issues because Java does not support full FP features.
Scala is the best option to use with Play framework to develop Highly Scalable, Better Performance with Concurrency/Parallelism and Low latency applications, because:
Play 2 is completely written in Scala.
It supports full FP features.
It is more expression language than Java.
It supports Akka Actor model very easily
It supports some new OOP feature like Traits.
Play’s built-in templates are developed in Scala
Question 70. How Scala Supports Both Highly Scalable And Highly Performance Applications?
Answer :
As Scala supports Multi-Paradigm Programming(Both OOP and FP) and uses Actor Concurrency Model, we can develop very highly Scalable and high-performance applications very easily.
Question 71. What Are The Available Build Tools To Develop Play And Scala Based Applications?
Answer :
The following three are most popular available Build Tools to develop Play and Scala Applications:
SBT
Maven
Gradle
Question 72. What Is Sbt? What Is The Best Build Tool To Develop Play And Scala Applications?
Answer :
SBT stands for Scala Build Tool. Its a Simple Build Tool to develop Scala-based applications.
Most of the people uses SBT Build tool for Play and Scala Applications. For example, IntelliJ IDEA Scala Plugin by default uses SBT as Build tool for this purpose.
Question 73. What Are The Available Unit Testing, Functional Testing And/or Bdd Frameworks For Play And Scala Based Applications?
Answer :
The following are most popular available Unit Testing, Functional Testing and/or BDD Frameworks for Play/Scala Based applications:
Spec2
ScalaTest
ScalaCheck
Mokito
Question 74. What Is The Best Code-coverage Tool Available For Play And Scala Based Applications?
Answer :
SCoverage is the Code-coverage tool for Play and Scala based applications.
SCoverage stands for Scala Code-coverage tool. It has three separate plug-ins to supports the following build tools:
SBT
Maven
Gradle
Question 75. What Is The Best Scala Style Checker Tool Available For Play And Scala Based Applications?
Answer :
Like Checkstyle for Java-Based Applications, Scalastyle is best Scala style checker tool available for Play and Scala based applications.
Scalastyle observes our Scala source code and indicates potential problems with it. It has three separate plug-ins to supports the following build tools:
SBT
Maven
Gradle
It has two separate plug-ins to supports the following two IDEs:
IntelliJ IDEA
Eclipse IDE
Question 76. Which Ides Support Play And Scala-based Applications Development And How?
Answer :
The following two popular IDEs support Play and Scala-Based Applications Development:
IntelliJ IDEA
Eclipse IDE
They support by using Scala Plugins like Eclipse IDE has a Scala IDE for Eclipse to support Play and Scala-Based Applications Development.
IntelliJ IDEA has a plug-in like “Scala Plugin for IntelliJ IDEA” to support “Scala, SBT and Play 2 Framework” based applications.
Question 77. What Is The Default Unit And Functional Testing Framework For Play? What Is The Default Build Tool For Play? What Is The Default Template Engine For Play? What Is The Built-in Web Server Available In Play Framework?
Answer :
Play Framework’s default Unit and Functional Testing Framework is Spec2. It is very easy to test Play/Scala based applications using Spec2 Framework.
Play Framework’s Default built-in template is “Twirl”. It was developed in Scala. By using these templates, we can develop Play/Scala based applications very easily.
The Built-in or Default Web Server available for Play Framework is Netty Server.
Question 78. Why Scala Is Better Than Java? What Are The Advantages Of Scala Over Java (java 8)? Compare To Java What Are The Major Advantages Or Benefits Of Scala?
Answer :
Because Scala supports the following extra features, it is better than Java 8:
Full FP Features
More Expression Language
Pattern Matching
Better support for Akka Actor Model
Automatic resolution for Inheritance Diamond Problem with Traits
Asynchronous and Non-blocking IO programming using Akka Framework
Fully Reactive Streaming API
Question 79. What Is An Anonymous Function In Scala? What Is A Function Literal In Scala? What Are The Advantages Of A Anonymous Function/function Literal In Scala?
Answer :
Anonymous Function is also a Function but it does not have any function name. It is also known as a Function Literal.
The advantages of a Anonymous Function/Function Literal in Scala:
We can assign a Function Literal to variable
We can pass a Function Literal to another function/method
We can return a Function Literal as another function/method result/return value.
Question 80. What Is An Higher-order Function (hof)?
Answer :
Higher Order Function (HOF) is also a function but which performs one, two or both of the following things:
Take other functions as arguments
Return functions as their results
Question 81. What Are The Differences Between Case Class And Normal Class?
Answer :
Case class is also a class, however when we compare it with normal class, it gives the following extra features or benefits:
By default, Case-class constructor parameters are ‘val’. We don’t need to declare parameters with ‘val’.
By default, Case-class constructor parameters become class fields.
These methods are added automatically: toString, equals, hashCode, copy. Apply and unapply.
It automatically gets Companion object.
No need to use ‘new’ keyword to create instance of Case Class.
Easy to use in Pattern Matching.
All these features are added by Scala Compiler at compile-time. It is not possible with normal class.
Question 82. What Are The Advantages Of Play/scala Stack To Develop Web Applications?
Answer :
The following are the major advantages of Play/Scala stack to develop web applications:
Open Source: Play is an Open-source free-software framework to develop web applications.
Better Productivity: Play framework’s Auto-reload feature improves Developer Productivity. No need to build, deploy and test our changes. Just do our changes and refresh the page to see our changes.
Stateless and Easy to Develop REST API: Play is HTTP based stateless model to serve web requests so it is very easy to develop REST API or RESTful Web Services.
Better Error-Handling: If we develop our web application using Play framework,it informs all errors in the browser in very useful format. It shows error message, the file location, line number where error occurred, highlighting the code-snippet to understand the error very easily.
High Performance and Better Scalability With Reactive: Play framework is developed by following Reactive design patterns and it is built on top of Netty sever to utilize Non-blocking IO Feature. Because of this feature, we can develop very highly Scalable and performance applications very easily.
Easy to Extend: Play is very flexible framework and supports developing plug-ins very easy to extend it’s features and functionality.
Highly Concurrency and Better Parallelism: As both Scala and Play supports Functional Programming, it is very easy to develop Highly Concurrency and Better Parallelism applications very easily because FP supports Immutability, Pure Functions (Functions without side-effects), Pattern Matching, Actor Model etc.
Better Reusability, Easy to Test and More Modular: As both Scala and Play supports Functional Programming, we can develop more modular and reusable applications. It is also very easy to test more modular applications.
Question 83. What Are The Java’s Oop Constructs Not Supported By Scala? What Are The Scala’s Oop Constructs Not Supported By Java? What Are The New Oops Constructs Introduced By Scala, But Not Supported By Java?
Answer :
Java’s OOP constructs, which are not supported by Scala:
There is no interface concept in Scala
There is no Enum concept in Scala
Scala’s OOP constructs, which are not supported by Java:
OR
The new OOPs constructs introduced by Scala, but not supported by Java:
Scala Traits
Solving Inheritance Diamond Problem automatically.
Question 84. What Is Call-by-name? Does Scala And Java Support Call-by-name? What Is The Difference Between Call-by-value And Call-by-name Function Parameters?
Answer :
Call-by-name means evaluates method/function parameters only when we need them or we access them. If we don’t use them, then it does not evaluate them.
Scala supports both call-by-value and call-by-name function parameters. However, Java supports only call-by-value, but not call-by-name.
Difference between call-by-value and call-by-name:
The major difference between these two are described below:
In Call-by-name, the function parameters are evaluated only whenever they are needed but not when the function is called.
In Call-by-value, the function parameters are evaluated when the function is called.
In Call-by-value, the parameters are evaluated before executing function and they are evaluated only once irrespective of how many times we used them in that function.
In Call-by-name, the parameters are evaluated whenever we access them and they are evaluated each time we use them in that function.
Scala Syntax Differences
Call-by-value:
1 def myFunction(a: Int, b: Int)
Here both a and b are Call-by-value parameters to myFunction.
Call-by-name:
1 def myFunction(a: Int, b: => Int)
Here each a is a Call-by-cost parameter and b is Call-via-call to myFunction.
Question 85. What Are The Popular Mvc Frameworks For Scala Language To Develop Web Applications?
Answer :
The following are the most popular MVC frameworks available for Scala Language to broaden Web Applications:
Play Framework
Scalatra Framework
Spray Framework
Lift Framework
Quest
 
   
    
 
  
  
  
  
  
 