This section includes InterviewSolutions, each offering curated multiple-choice questions to sharpen your knowledge and support exam preparation. Choose a topic below to get started.
| 1. |
If I Want To Become A Fullstack Scala Developer, Which Technology Stack I Should Learn? |
|
Answer» If you want to become a Fullstack Scala Developer, you should learn the following TECHNOLOGY stack:
If you want to become a Fullstack Scala Developer, you should learn the following technology stack: |
|
| 2. |
What Is The Difference Between :: And #:: In Scala? What Is The Difference Between ::: And #::: In Scala? |
|
Answer» In Scala Collection API,
In Scala Collection API, |
|
| 3. |
Explain The Main Difference Between List And Stream In Scala Collection Api? How Do We Prove That Difference? When Do We Choose Stream? |
|
Answer» In Scala, both List and Stream are from Collection API and works almost similar. Both are Immutable COLLECTIONS. However, there is one main difference between List and Stream in Scala Collection API: That is List elements are evaluated Eagerly and Stream elements are evaluated Lazily that means when we access them. scala> VAR list1 = List(1,2,3,4) Here we can observe that all List elements evaluated at the time of creating List object. However, if we do same thing on Stream, we cannot SEE all elements. We can see only first evaluated element and remaining elements are evaluated lazily as shown below: scala> var s1 = Stream(1,2,3,4) When we want LAZY collection to evaluate elements only when we access them then it’s better to use Stream. In Scala, both List and Stream are from Collection API and works almost similar. Both are Immutable collections. However, there is one main difference between List and Stream in Scala Collection API: That is List elements are evaluated Eagerly and Stream elements are evaluated Lazily that means when we access them. scala> var list1 = List(1,2,3,4) Here we can observe that all List elements evaluated at the time of creating List object. However, if we do same thing on Stream, we cannot see all elements. We can see only first evaluated element and remaining elements are evaluated lazily as shown below: scala> var s1 = Stream(1,2,3,4) When we want Lazy collection to evaluate elements only when we access them then it’s better to use Stream. |
|
| 4. |
What Is The Use Of ‘???’ In Scala-based Applications? |
|
Answer» This ‘???’ three question marks is not an operator, a method in Scala. It is used to MARK a method which is ‘In PROGRESS’ that means Developer should PROVIDE implementation for that one. This method is define in scala.PreDef class as shown below: def ??? : Nothing = throw new NotImplementedError If we RUN that method without providing implementation, then it throws ‘NotImplementedError’ error as shown below: scala> def add(a:Int, b:Int) : Int = ??? scala.NotImplementedError: an implementation is MISSING This ‘???’ three question marks is not an operator, a method in Scala. It is used to mark a method which is ‘In Progress’ that means Developer should provide implementation for that one. This method is define in scala.PreDef class as shown below: def ??? : Nothing = throw new NotImplementedError If we run that method without providing implementation, then it throws ‘NotImplementedError’ error as shown below: scala> def add(a:Int, b:Int) : Int = ??? scala.NotImplementedError: an implementation is missing |
|
| 5. |
What Is Extractor In Scala? What Is The Difference Between Constructor And Extractor In Scala? What Is The Use Of Extractor In Scala? |
|
Answer» Not only in Java and Scala, in almost all OOP languages Constructor is used to create (or assemble) an object or an instance of a Class using it’s parameters (or components). Extractor is quite OPPOSITE to Constructor. In Scala, Extractor is used to decompose or disassemble an object into it’s parameters (or components). In Scala, apply method is a Constructor. Internally, Extractor uses unapply method to decompose an OBJECTS into it’s parts (or parameters). In Scala, Extractor is mainly used in PATTERN MATCHING CONCEPT. We will discuss Pattern Matching concept soon. Not only in Java and Scala, in almost all OOP languages Constructor is used to create (or assemble) an object or an instance of a Class using it’s parameters (or components). Extractor is quite opposite to Constructor. In Scala, Extractor is used to decompose or disassemble an object into it’s parameters (or components). In Scala, apply method is a Constructor. Internally, Extractor uses unapply method to decompose an objects into it’s parts (or parameters). In Scala, Extractor is mainly used in Pattern Matching concept. We will discuss Pattern Matching concept soon. |
|
| 6. |
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 DEVELOP Web Applications: The following are the most popular MVC frameworks available for Scala Language to develop Web Applications: |
|
| 7. |
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:
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 both a is a Call-by-value parameter and b is Call-by-name to myFunction. 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: 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 both a is a Call-by-value parameter and b is Call-by-name to myFunction. |
|
| 8. |
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:
Scala’s OOP constructs, which are not supported by Java: OR The new OOPs constructs introduced by Scala, but not supported by Java:
Java’s OOP constructs, which are not supported by Scala: Scala’s OOP constructs, which are not supported by Java: OR The new OOPs constructs introduced by Scala, but not supported by Java: |
|
| 9. |
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:
The following are the major advantages of Play/Scala stack to develop web applications: |
|
| 10. |
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:
All these features are added by Scala Compiler at compile-time. It is not possible with normal class. Case class is also a class, however when we compare it with normal class, it gives the following extra features or benefits: All these features are added by Scala Compiler at compile-time. It is not possible with normal class. |
|
| 11. |
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: Higher Order Function (HOF) is also a function but which performs one, two or both of the following things: |
|
| 12. |
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:
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: |
|
| 13. |
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:
Because Scala supports the following extra features, it is better than Java 8: |
|
| 14. |
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. 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. |
|
| 15. |
Which Ides Support Play And Scala-based Applications Development And How? |
|
Answer» The following two popular IDES SUPPORT Play and SCALA-Based Applications Development:
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. The following two popular IDEs support Play and Scala-Based Applications Development: 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. |
|
| 16. |
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:
It has two separate plug-ins to supports the following two IDEs:
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: It has two separate plug-ins to supports the following two IDEs: |
|
| 17. |
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:
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: |
|
| 18. |
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:
The following are most popular available Unit Testing, Functional Testing and/or BDD Frameworks for Play/Scala Based applications: |
|
| 19. |
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. 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. |
|
| 20. |
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:
The following three are most popular available Build Tools to develop Play and Scala Applications: |
|
| 21. |
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. 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. |
|
| 22. |
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. 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: |
|
| 23. |
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.
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. |
|
| 24. |
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. 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. |
|
| 25. |
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:
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: |
|
| 26. |
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.
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. |
|
| 27. |
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:
There are many Scala-Based Framework to develop RESTful Web Services. Most popular frameworks are: |
|
| 28. |
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:
The following are the Advantages of Functional Programming (FP) or Advantages of Pure Functions: |
|
| 29. |
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. 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. |
|
| 30. |
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. 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. |
|
| 31. |
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. 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. |
|
| 32. |
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. 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. |
|
| 33. |
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. In Scala, Pattern Matching follows Visitor Design Pattern. In the same way, Java’s ‘isinstanceof’ operator also follows Visitor Design Pattern. |
|
| 34. |
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. 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. |
|
| 35. |
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,4,5,6,7,8,9,10) 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,4,5,6,7,8,9,10) |
|
| 36. |
What Is The Use Of ‘yield’ Keyword In Scala’s For-comprehension Construct? |
|
Answer» We can use ‘yield’ keyword in Scala’s for-comprehension construct. ‘for/yield’ is used to iterate a collection of elements and generates new collection of same type. It does not change the original collection. It generates new collection of same type as original collection type. For EXAMPLE, if we use ‘for/yield’ construct to iterate a List then it generates a new List only. scala> val list = List(1,2,3,4,5) We can use ‘yield’ keyword in Scala’s for-comprehension construct. ‘for/yield’ is used to iterate a collection of elements and generates new collection of same type. It does not change the original collection. It generates new collection of same type as original collection type. For example, if we use ‘for/yield’ construct to iterate a List then it generates a new List only. scala> val list = List(1,2,3,4,5) |
|
| 37. |
What Are The Major Differences Between Scala’s Auxiliary Constructors And Java’s Constructors? |
|
Answer» Scala’s Auxiliary constructor is almost similar to Java’s constructor with few differences. Compared to Java’s CONSTRUCTORS, Auxiliary constructors have the FOLLOWING few differences:
Scala’s Auxiliary constructor is almost similar to Java’s constructor with few differences. Compared to Java’s constructors, Auxiliary constructors have the following few differences: |
|
| 38. |
In Fp, What Is The Difference Between A Function And A Procedure? |
|
Answer» Both are USED to PERFORM computation, however they have one major DIFFERENCE in Functional Programming world. A function is a computation unit without side-effect where as a Procedure is ALSO a computation unit with side-effects. Both are used to perform computation, however they have one major difference in Functional Programming world. A function is a computation unit without side-effect where as a Procedure is also a computation unit with side-effects. |
|
| 39. |
What Is A Pure Function? |
|
Answer» A PURE function is a function without any observable side-effects. That means it returns always same results irrespective how many times we call it with same inputs. A pure function always gives same output for the same inputs. For EXAMPLE:- scala> 10 + 20 Here “+” a pure function available in Int class. It gives same result 30 for same inputs 10 and 30, irrespective how many times we call it. A pure function is a function without any observable side-effects. That means it returns always same results irrespective how many times we call it with same inputs. A pure function always gives same output for the same inputs. For Example:- scala> 10 + 20 Here “+” a pure function available in Int class. It gives same result 30 for same inputs 10 and 30, irrespective how many times we call it. |
|
| 40. |
How Many Values Of Type Unit Have In Scala? |
|
Answer» In Scala, Unit is something SIMILAR to JAVA’s void keyword. It is used to represent “No VALUE exists”. It has ONE and only one value that is (). In Scala, Unit is something similar to Java’s void keyword. It is used to represent “No value exists”. It has one and only one value that is (). |
|
| 41. |
How Many Values Of Type Nothing Have In Scala? |
|
Answer» In Scala, Nothing type have no values that is ZERO. It does not have any values. It is a subtype of all VALUE CLASSES and REFERENCE classes. In Scala, Nothing type have no values that is zero. It does not have any values. It is a subtype of all Value classes and Reference classes. |
|
| 42. |
What Is Range In Scala? How To Create A Range In Scala? |
|
Answer» Range is a Lazy Collection in SCALA. Range is a class available in ‘scala’ package 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, 3, 4, 5, 6, 7, 8, 9, 10) scala> 1 until 10 res1: scala.collection.immutable.Range = Range(1, 2, 3, 4, 5, 6, 7, 8, 9) Range is a Lazy Collection in Scala. Range is a class available in ‘scala’ package 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, 3, 4, 5, 6, 7, 8, 9, 10) scala> 1 until 10 res1: scala.collection.immutable.Range = Range(1, 2, 3, 4, 5, 6, 7, 8, 9) |
|
| 43. |
How To Implement Interfaces In Scala? |
|
Answer» As we know from Java BACKGROUND, we use interface to define contact. HOWEVER, there is no interface concept in Scala. EVEN, Scala doesn’t have interface keyword. Scala has a more powerful and flexible concept i.e. TRAIT for this purpose. As we know from Java background, we use interface to define contact. However, there is no interface concept in Scala. Even, Scala doesn’t have interface keyword. Scala has a more powerful and flexible concept i.e. trait for this purpose. |
|
| 44. |
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 simple words, if a Scala class and object shares the same name and defined in the same source file, then that class is known as “Companion Class” and that object is known as “Companion Object”. When we CREATE a Class by using Scala “class” keyword and Object by using Scala “object” keyword with same name and within the same source file, then that class is known as “Companion Class” and that object is known as “Companion Object”. Example:- Employee.scala In Scala, The MAIN PURPOSE of Companion Object is to define apply methods and avoid using new keyword in CREATING an instance of that Companion class object. In simple words, if a Scala class and object shares the same name and defined in the same source file, then that class is known as “Companion Class” and that object is known as “Companion Object”. When we create a Class by using Scala “class” keyword and Object by using Scala “object” keyword with same name and within the same source file, then that class is known as “Companion Class” and that object is known as “Companion Object”. Example:- Employee.scala In Scala, The main purpose of Companion Object is to define apply methods and avoid using new keyword in creating an instance of that Companion class object. |
|
| 45. |
What Is Object In Scala? Is It A Singleton Object Or Instance Of A Class? |
|
Answer» Unlike Java, Scala has two MEANINGS about ‘object’. Don’t get confuse about this, I will explain it clearly. In Java, we have only one MEANING for object that is “An instance of a class”. Like Java, the first meaning of object is “An instance of a class”. VAL p1 = new Person("Scala","Java") Second meaning is that object is a keyword in Scala. It is USED to define Scala Executable programs, Companion Objects, Singleton Objects etc. Unlike Java, Scala has two meanings about ‘object’. Don’t get confuse about this, I will explain it clearly. In Java, we have only one meaning for object that is “An instance of a class”. Like Java, the first meaning of object is “An instance of a class”. val p1 = new Person("Scala","Java") Second meaning is that object is a keyword in Scala. It is used to define Scala Executable programs, Companion Objects, Singleton Objects etc. |
|
| 46. |
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 class keyword to define instance members and object keyword to define static members. Scala does not have static keyword, but still we can define them by USING object keyword. The MAIN design decision about this is that the clear separation between instance and static members. Loosely COUPLING between them. And other major reason is to avoid static keyword so that Scala will become a Pure-OOP Language. In Scala, we use class keyword to define instance members and object keyword to define static members. Scala does not have static keyword, but still we can define them by using object keyword. The main design decision about this is that the clear separation between instance and static members. Loosely coupling between them. And other major reason is to avoid static keyword so that Scala will become a Pure-OOP Language. |
|
| 47. |
Does A Companion Object Access Private Members Of It’s Companion Class In Scala? |
|
Answer» Generally, PRIVATE MEMBERS means ACCESSIBLE only WITHIN that class. However Scala’s Companion class and Companion Object has provided another feature. In Scala, a Companion object can ACCESS private members of it’s Companion class and Companion class can access it’s Companion object’s private members. Generally, private members means accessible only within that class. However Scala’s Companion class and Companion Object has provided another feature. In Scala, a Companion object can access private members of it’s Companion class and Companion class can access it’s Companion object’s private members. |
|
| 48. |
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 can declare a private Primary Constructor very easily. Just define a Primary Constructor as it is and ADD ‘private’ just after class NAME and before parameter LIST as shown below: class Person private (name: String) As it’s a private constructor, we cannot call it from outside. We should PROVIDE a factory method (that is apply method) as shown above and use that constructor INDIRECTLY. In Scala, we can declare a private Primary Constructor very easily. Just define a Primary Constructor as it is and add ‘private’ just after class name and before parameter list as shown below: class Person private (name: String) As it’s a private constructor, we cannot call it from outside. We should provide a factory method (that is apply method) as shown above and use that constructor indirectly. |
|
| 49. |
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 instance of a Class without using ‘new’ KEYWORD, internally it make a call to appropriate apply method available in Companion OBJECT. Here appropriate apply method means that MATCHED with parameters. When do we choose this option: When we need to provide private private constructor and we need to avoid using ‘new’ keyword, we can implement only apply method with same SET of parameters and allow our class users to create it without new keyword. In Scala, when we create an instance of a Class without using ‘new’ keyword, internally it make a call to appropriate apply method available in Companion object. Here appropriate apply method means that matched with parameters. When do we choose this option: When we need to provide private private constructor and we need to avoid using ‘new’ keyword, we can implement only apply method with same set of parameters and allow our class users to create it without new keyword. |
|
| 50. |
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 methods play very important ROLE. They are also very useful in Play Framework in mapping and unmapping data between Form data and MODEL data. In simple words, apply method: To compose or assemble an object from it’s components. unapply method: To decompose or dis-assemble an object into it’s components. Scala’s apply method: It is used to compose an object by using its components. Suppose if we WANT to create a Person object, then use firstName and laststName two components and compose Person object as shown below. class Person(val firstName: String, val lastName: String) Scala’s unapply method: It is used to decompose an object into its components. It FOLLOWS reverse process of apply method. Suppose if we have a Person object, then we can decompose this object into it’s two components: firstName and laststName as shown below. class Person(val firstName: String, val lastName: String) In Scala, apply and unapply methods play very important role. They are also very useful in Play Framework in mapping and unmapping data between Form data and Model data. In simple words, apply method: To compose or assemble an object from it’s components. unapply method: To decompose or dis-assemble an object into it’s components. Scala’s apply method: It is used to compose an object by using its components. Suppose if we want to create a Person object, then use firstName and laststName two components and compose Person object as shown below. class Person(val firstName: String, val lastName: String) Scala’s unapply method: It is used to decompose an object into its components. It follows reverse process of apply method. Suppose if we have a Person object, then we can decompose this object into it’s two components: firstName and laststName as shown below. class Person(val firstName: String, val lastName: String) |
|