InterviewSolution
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. |
Explain map() and flatmap(). |
|
Answer» Map() and its close cousin flatMap() are often used when we deal with data structures in Scala and both are higher-order functions.
|
|
| 2. |
Write different types of scope provided for variables in Scala. |
|
Answer» According to their declarations, Scala VARIABLES are CATEGORIZED into three SCOPES as given below:
|
|
| 3. |
What do you mean by ofDim()? |
|
Answer» Scala provides us with a function called ofDim that declares multidimensional arrays using the matrix format. Array.ofDim can be used to declare multidimensional arrays. There's no limit to the number of dimensional arrays you can CREATE. Syntax: val arrayname = Array.ofDim[data_type](number of rows, number of columns)or var arrayname = Array(Array(elements), Array(elements)) object MultiArrayExample { DEF main(args: Array[String]) { val multiArr= Array.ofDim[Int](2, 2) multiArr(0)(0) = 5 multiArr(0)(1) = 10 multiArr(1)(0) = 15 multiArr(1)(1) = 20 for(i <- 0 to 1; j <- 0 to 1) { println("Element "+ i + j + " = " + multiArr(i)(j)) } } }Output: Element 00 = 5 Element 01 = 10 Element 10 = 15 Element 11 = 20 |
|
| 4. |
Explain BitSet. |
|
Answer» A set is a collection of unique items that cannot be repeated. In Scala, non-negative INTEGER SETS are called Bitsets, and they are represented as variable-size arrays of bits packed into 64-bit words. The largest number in a bitset represents its memory footprint. Syntax: var BS: BitSet = BitSet(element1, element2, element3, ....)Here, BS represents the name of the BitSet that was created. scala.collection.immutable.BitSet and scala.collection.mutable.BitSet are the two versions of BitSet provided in Scala. Although both are identical, mutable data structures change the bits in place, thus making them less concurrency friendly than immutable data structures. Example: import scala.collection.immutable.BitSet object InterviewBit { def main(ARGS:Array[String]) { println("Initialize a BitSet") val numbers: BitSet = BitSet(5, 6, 7, 8) println(s"ELEMENTS of BitSet are = $bitSet") println(s"Element 6 = ${bitSet(6)}") println(s"Element 3 = ${bitSet(3)}") println(s"Element 7 = ${bitSet(7)}") }Output: Initialize a BitSet Elements of BitSet are = BitSet(5,6,7,8) Element 6 = true Element 3 = false Element 7 = true |
|
| 5. |
What is the use of apply and unapply methods in Scala? |
|
Answer» In Scala, an extractor defines a method unapply(), as well as an OPTIONAL method, apply(). For mapping and unmapping data between FORM and MODEL data, both apply and unapply methods are used.
|
|
| 6. |
What is the importance of App in Scala? |
|
Answer» A helper class, named App, is provided by Scala that provides the MAIN METHOD and its MEMBERS together. App trait allows Objects to be TURNED into executable programs quickly. The App class in Scala can be extended instead of writing your own main method. This WAY you can produce concise and scalable applications. Example: object InterviewBit extends App { println("Hello Scala") } |
|
| 7. |
Explain what you mean by literals and write its types. |
|
Answer» Literals, or constants, are any CONSTANT value that can be assigned to a variable. A literal is a set of symbols used to DESCRIBE a constant value in the code. They are classified into the FOLLOWING types:
|
|
| 8. |
Explain Option and write its usage. |
|
Answer» The Scala Option[T] is referred to as a carrier of one element or none for a specified type. As the name suggests, this container holds either a Some or a None object, which represents a missing value. It holds Some[T] if a value is stored, otherwise none. In simple words, Scala options are WRAPPERS for missing values. object option { def main(args: Array[String]) { val employee: Map("InterviewBit" -> "Aarav", "Scaler" -> "ANKESH") val a= employee.get("InterviewBit") val B= employee.get("Informatica") println(a); println(b); } }Output: Some(Aarav) NoneHere, the key of the value InterviewBit is FOUND, therefore, Some is returned, however, the key of the value Informatica is not found, therefore, None is returned. |
|
| 9. |
What are different types of Scala variables? |
|
Answer» It is well known that variables are merely RESERVED memory locations for storing values. In SCALA, variables are MAINLY of two types:
Syntax: var Variable_name: Data_type = "value";Example: var Company: String = "InterviewBit”;
Syntax: var Variable_name: Data_type = "value";Example: val Company: String = "InterviewBit"; |
|
| 10. |
What are tuples and what is their usage in Scala? |
|
Answer» A tuple is a HETEROGENEOUS data structure, consisting of a fixed number of elements, each with a different data type. A tuple, however, can contain objects of different types, as well as being immutable. They are particularly useful when returning multiple values from methods. A tuple's type is DETERMINED by how many elements it contains as well as the type of those elements. Scala uses Tuple2, Tuple3, etc., up to Tuple22, to represent types of TUPLES. Scala currently limits the number of elements in a tuple to 22, and if the number of elements in the tuple exceeds 22, an error will be GENERATED. Example: A tuple storing an integer, and a string. val name = (23, "InterviewBit")The inferred type of ingredient is (Integer, String), which is shorthand for Tuple2[INT, String]. Note: Tuples can be used to overcome this limit. It is possible for a tuple to contain other tuples. |
|
| 11. |
Explain the term stream in Scala. |
|
Answer» Streams, a Scala feature, are essentially lazy lists in which elements are only evaluated as needed. In this WAY, Scala allows for faster processing. Streams are SIMILAR to lists in terms of performance. Syntax: val str = 1 #:: 2 #:: 3 #:: Stream.emptyScala lets you construct Lists by using the :: operator, while you can build Streams by using the #:: operator with Stream.empty at the end of the expression. A stream's head in the above syntax is 1, while its tail is 2 and 3. Example: object MainObject { def main(args:ARRAY[String]){ val stream = 20 #:: 30 #:: 45 #:: Stream.empty println(stream) } } Stream(20, ?)Observe the output and you will see a question MARK instead of the second element. Scala only evaluates lists if they are needed. |
|
| 12. |
What are case classes in Scala? |
|
Answer» Scala CASE classes are like regular classes except for the fact that they are good for MODELING immutable data and serve as useful in pattern matching. Case classes include public and immutable parameters by default. These classes support pattern matching, which makes it easier to write logical code. The following are some of the characteristics of a Scala case class:
Syntax: case class className(parameters)Example: case class Student(name:String, age:Int) object MainObject { def main(args:Array[String]) { var c = Student(“AARAV”, 23) println("Student name:" + c.name); println("Student age: " + c.age); } }Output: Student Name: Aarav Student Age: 23 |
|
| 13. |
Name some of the frameworks that Scala supports. |
| Answer» | |
| 14. |
Write some benefits of using Scala. |
|
Answer» It is important for a language to offer attractive features if it hopes to challenge Java's dominance. In this regard, Scala brings many positive ATTRIBUTES to the table and its ability to compete with Java proves its prominence. The following are some of these positive attributes:
|
|
| 15. |
What are some main features of Scala? |
|
Answer» Some languages bring unique benefits to a particular kind of project, making them the best choice. The interoperability of Scala and its functional programming paradigm propelled Scala's rapid growth. In order to address Java's critics, Scala is designed to be concise. The following are some of Scala's unique features that set it apart from other programming languages:
Additionally, it offers:
|
|