Scala MCQ

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?

a) Scalability
b) Scalable Language
c) Scalar Language
d) Scalation

Answer:

b) Scalable Language

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?

a) struct
b) class
c) type
d) object

Answer:

b) class

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?

a) Using the single keyword
b) Using the singleton keyword
c) Using the object keyword
d) Using the instance keyword

Answer:

c) Using the object keyword

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?

a) +
b) &
c) concat
d) .

Answer:

a) +

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?

a) ==
b) ===
c) equals
d) is

Answer:

a) ==

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?

a) Array
b) List
c) StringBuilder
d) StringBuffer

Answer:

b) List

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?

a) var x = 10
b) const x = 10
c) define x = 10
d) val x = 10

Answer:

d) val x = 10

Explanation:

In Scala, val is used to declare a constant, or immutable, variable.

8. Which keyword is used for inheritance in Scala?

a) extends
b) inherits
c) using
d) with

Answer:

a) extends

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)?

a) 5
b) 0
c) Some(5)
d) None

Answer:

a) 5

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?

a) Object
b) DataType
c) Base
d) Any

Answer:

d) Any

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?

a) List
b) Map
c) Set
d) ArrayDict

Answer:

d) ArrayDict

Explanation:

ArrayDict is not a part of the Scala's standard collection library.

12. What is the result of the following code: "Hello".tail?

a) "Hello"
b) "H"
c) "ello"
d) Error

Answer:

c) "ello"

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?

a) function add(a: Int, b: Int): Int { return a + b; }
b) def add(a: Int, b: Int): Int = { a + b }
c) fun add(a: Int, b: Int): Int { a + b }
d) define add(a: Int, b: Int): Int => a + b

Answer:

b) def add(a: Int, b: Int): Int = { a + b }

Explanation:

Functions in Scala are defined using the def keyword.

14. Which of the following can represent an optional value in Scala?

a) Maybe
b) Option
c) Opt
d) Nullable

Answer:

b) Option

Explanation:

In Scala, Option is used to represent a value that might be present or absent.

15. Which method can transform an Option value?

a) transform
b) change
c) modify
d) map

Answer:

d) map

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?

a) A data type
b) A type of variable
c) An abstract class type
d) A type of function

Answer:

c) An abstract class type

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(_ – _)?

a) -8
b) -2
c) 10
d) 2

Answer:

b) -2

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?

a) [1, "Hello"]
b) Tuple(1, "Hello")
c) (1, "Hello")
d) {1, "Hello"}

Answer:

c) (1, "Hello")

Explanation:

In Scala, tuples are created using parentheses.

19. Which method will throw an exception if an Option is None?

a) get
b) orElse
c) getOrElse
d) isEmpty

Answer:

a) get

Explanation:

Calling get on a None will throw a NoSuchElementException.

20. In Scala, which of the following represents a mutable list?

a) List
b) Vector
c) MutableList
d) ImmutableList

Answer:

c) MutableList

Explanation:

While List and Vector are immutable collections, MutableList allows modification.

21. Which operator checks the type of a variable in Scala?

a) ::
b) :
c) as
d) isInstanceOf

Answer:

d) isInstanceOf

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?

a) new Person
b) Person.new
c) Person()
d) create Person

Answer:

a) new Person

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?

a) match
b) switch
c) case
d) pattern

Answer:

a) match

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?

a) ++
b) +
c) .add
d) concat

Answer:

a) ++

Explanation:

In Scala, two lists can be concatenated using the ++ operator.

25. Which Scala keyword is used to define an abstract class?

a) abs
b) abstract
c) abstraction
d) virtual

Answer:

b) abstract

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.


Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top