Scala, a hybrid functional and object-oriented programming language, offers a rich set of features and a concise way to express complex programming patterns. Test your Scala knowledge with this set of 25 multiple-choice questions!
1. What does the word 'Scala' stand for?
Answer:
Explanation:
The name 'Scala' is a portmanteau of "scalable" and "language", indicating its design to grow with the demands of its users.
2. Which keyword is used in Scala to declare a class?
Answer:
Explanation:
In Scala, the class keyword is used to declare a class. It allows for the creation of objects and encapsulation of data and behavior.
3. How do you define a singleton object in Scala?
Answer:
Explanation:
In Scala, the object keyword is used to define a singleton object. This ensures that there's only one instance of the object throughout the JVM.
4. Which Scala operator is used for string concatenation?
Answer:
Explanation:
Scala uses the + operator for string concatenation, similar to Java.
5. Which operator is used for checking the equality of two objects in Scala?
Answer:
Explanation:
In Scala, the == operator is used to check the equality of two objects. It checks the contents for objects, unlike Java where it checks the reference.
6. Which data type is immutable in Scala?
Answer:
Explanation:
In Scala, Lists are immutable, meaning their elements cannot be changed after they are created.
7. How do you define a variable in Scala that cannot be modified?
Answer:
Explanation:
In Scala, val is used to declare a constant, or immutable, variable.
8. Which keyword is used for inheritance in Scala?
Answer:
Explanation:
In Scala, the extends keyword is used to denote inheritance from a superclass.
9. What does the following code return: Some(5).getOrElse(0)?
Answer:
Explanation:
getOrElse returns the value inside the Some if it's present, otherwise it returns the default value provided, which is 0 in this case.
10. In Scala, what is the base trait for all data types?
Answer:
Explanation:
In Scala, Any is the root trait (or super trait) of every type.
11. Which of the following is NOT a valid Scala collection?
Answer:
Explanation:
ArrayDict is not a part of the Scala's standard collection library.
12. What is the result of the following code: "Hello".tail?
Answer:
Explanation:
The tail method returns all characters of the string except the first one.
13. What is a correct way to define a function in Scala?
Answer:
Explanation:
Functions in Scala are defined using the def keyword.
14. Which of the following can represent an optional value in Scala?
Answer:
Explanation:
In Scala, Option is used to represent a value that might be present or absent.
15. Which method can transform an Option value?
Answer:
Explanation:
The map method is used to transform the value inside an Option without affecting its optional nature.
16. In Scala, what is a trait?
Answer:
Explanation:
A trait in Scala represents a set of abstract and non-abstract methods. It's akin to an interface in other languages, but can also contain concrete methods.
17. What is the result of this Scala code: List(1, 2, 3, 4).reduceLeft(_ – _)?
Answer:
Explanation:
reduceLeft applies the function from left to right: (((1-2)-3)-4) = -2.
18. Which of the following creates an instance of a Scala tuple containing two elements?
Answer:
Explanation:
In Scala, tuples are created using parentheses.
19. Which method will throw an exception if an Option is None?
Answer:
Explanation:
Calling get on a None will throw a NoSuchElementException.
20. In Scala, which of the following represents a mutable list?
Answer:
Explanation:
While List and Vector are immutable collections, MutableList allows modification.
21. Which operator checks the type of a variable in Scala?
Answer:
Explanation:
isInstanceOf is used to check the type of a variable in Scala.
22. How do you create an instance of a class Person in Scala?
Answer:
Explanation:
Instances of classes in Scala are created using the new keyword followed by the class name.
23. Which keyword is used for pattern matching in Scala?
Answer:
Explanation:
match is the keyword used for pattern matching in Scala, although case is used within the match expression to define patterns.
24. How do you concatenate two lists in Scala?
Answer:
Explanation:
In Scala, two lists can be concatenated using the ++ operator.
25. Which Scala keyword is used to define an abstract class?
Answer:
Explanation:
The abstract keyword in Scala is used to declare a class as abstract, meaning it can't be instantiated directly and may contain abstract (undefined) methods.