Scala Test 1 — Questions and Answers
Question 1: Which of the following expressions has the proper value? <br> List(1,2,3)flatMap(x=>List(x,4))
- List(1,4,2,4,3,4) (Correct answer)
- List(1,2,3,4)
- List (1,2,3,x,4)
- List(4,4,4)
Correct answer: List(1,4,2,4,3,4)
The `flatMap` function in Scala applies a given function to each element of a collection, which returns a new collection for each element, and then flattens all these resulting collections into a single one. For `List(1,2,3).flatMap(x => List(x,4))`, each number `x` is transformed into `List(x,4)`. This results in `List(1,4)`, `List(2,4)`, and `List(3,4)`, which are then concatenated to form `List(1,4,2,4,3,4)`.
Question 2: Scala is also known as:
- Cloud Computing Language
- Style Sheets Language
- Scripting Language (Correct answer)
- None of the above
Correct answer: Scripting Language
Scala is a powerful, general-purpose programming language that runs on the JVM, supporting both object-oriented and functional paradigms. Due to its concise syntax and the availability of an interactive interpreter (REPL), Scala can also be effectively used for scripting tasks, allowing developers to write and execute code quickly without the full compilation process typically associated with larger applications.
Question 3: Which of the following claims regarding Either is correct?
- An instance of Either can be an instance of Left or Right
- It is a sealed abstract class
- We can use it to deal with possible missing values
- All of the above (Correct answer)
Correct answer: All of the above
In Scala, `Either[L, R]` is a sealed abstract class that represents a value which can be of type `L` (typically for 'Left' or error) or type `R` (typically for 'Right' or success). This makes it a powerful tool for error handling and dealing with optional or potentially missing values, as it explicitly forces the developer to consider both possible outcomes. Therefore, all the given statements accurately describe `Either`.
Question 4: Case classes are Left and Right. Is this statement true or false?
- False
- True (Correct answer)
Correct answer: True
In Scala's `Either` type, `Left` and `Right` are indeed case classes. They serve as concrete implementations of the `Either` abstract class, allowing an `Either` instance to encapsulate a value of either the 'left' type or the 'right' type. This design facilitates pattern matching, enabling developers to elegantly handle both success and failure scenarios.
Question 5: Choose the appropriate value from the following line of code: <br> "abcde" ensures that (_.length>3)
- abcde (Correct answer)
- the code throws an errorabc
- abc
- abcd
Correct answer: abcde
The `ensure` method in Scala checks if a given predicate holds true for the value it's called on. If the predicate evaluates to `true`, the original value is returned. If it's `false`, an `IllegalArgumentException` is thrown. In this case, the string "abcde" has a length of 5, and the condition `(_.length > 3)` (5 > 3) is true, so the original string "abcde" is returned.
Question 6: Scala is a combination of the words:
- Sequential and Language
- Scalar and Language
- Scalable and Language (Correct answer)
- Script and Language
Correct answer: Scalable and Language
The name 'Scala' is a portmanteau, derived from the words 'Scalable' and 'Language.' This name reflects its design philosophy to be a language that can scale effectively from small scripts to large, complex enterprise applications, while supporting both object-oriented and functional programming paradigms.
Question 7: Which of the following claims about a functor in Scala is incorrect?
- It maps objects or entities of one category to those of another
- It is a mapping between categories
- It is a built-in construct in Scala (Correct answer)
- None of the above
Correct answer: It is a built-in construct in Scala
A Functor is a concept from functional programming and category theory, describing a type that can be 'mapped over' (e.g., `List`, `Option`). It represents a mapping between categories, preserving their structure. While Scala's standard library types exhibit functorial behavior, `Functor` itself is not a built-in keyword or construct in the Scala language; it's typically implemented as a type class in functional programming libraries like Cats or Scalaz.
Question 8: Which of the following statements about Java and Scala is correct?
- We can call Scala from Java and Java from Scala
- Both work with popular IDEs like Eclipse, Netbeans, and IntelliJ
- Both run on the JVM
- All of the above (Correct answer)
Correct answer: All of the above
Scala is designed for seamless interoperability with Java. Both languages compile to bytecode that runs on the Java Virtual Machine (JVM), allowing Scala code to call Java libraries and Java code to call Scala code. Furthermore, both Scala and Java development are fully supported by popular Integrated Development Environments (IDEs) like Eclipse, NetBeans, and IntelliJ IDEA, making them highly compatible and versatile.
Question 9: In the REPL, how do you halt execution?
- Pressing Ctrl+W
- Pressing Ctrl+C (Correct answer)
- Pressing Ctrl+Q
- None of the above
Correct answer: Pressing Ctrl+C
Pressing Ctrl+C is the standard keyboard shortcut used in most command-line interfaces and REPLs (Read-Eval-Print Loops) to send an interrupt signal. This signal, known as SIGINT, typically causes the running program or process, including the Scala REPL, to terminate its current execution. It's a universal method for halting processes in a terminal environment.
Which of the following expressions has the proper value?
List(1,2,3)flatMap(x=>List(x,4))