Scala Online Quiz Test

Welcome to our Scala Online Quiz Test! This quiz comprises 30 multiple-choice questions designed to test your knowledge of Scala, a powerful programming language that blends functional and object-oriented programming concepts.

Whether you are new to Scala or have some experience, this test will challenge you with questions on Scala's syntax, control structures, collections, and more. It's a great way to see how well you understand Scala and to help you pinpoint areas where you might need to improve. Ready to test your Scala skills? Let’s dive in!

1. What is the output of the following Scala code?

val list = List(1, 2, 3)
println(list.head)
a) 1
b) 2
c) 3
d) None of the above

2. How do you declare a variable in Scala that can be reassigned?

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

3. What does the following Scala function do?

def multiply(x: Int, y: Int): Int = x * y
a) Returns the product of x and y
b) Compiles with an error
c) Returns the sum of x and y
d) Does nothing

4. What is the primary construct for handling exceptions in Scala?

a) if-else
b) try-catch
c) switch-case
d) error-function

5. Which of the following is NOT a valid way to define a function in Scala?

a) def f = (x: Int) => x + 1
b) val f = (x: Int) => x + 1
c) lazy val f = (x: Int) => x + 1
d) var f = (x: Int) => x + 1

6. What is pattern matching in Scala?

a) A method to iterate over a collection
b) A technique to check the type of an object
c) A feature that allows checking a value against a pattern
d) A regex method to find patterns in strings

7. How do you create an immutable list in Scala?

a) val numbers = List(1, 2, 3)
b) var numbers = List(1, 2, 3)
c) val numbers = new List(1, 2, 3)
d) var numbers = new List(1, 2, 3)

8. What does case class in Scala primarily support?

a) Serialization
b) Mutable states
c) Pattern matching
d) Exception handling

9. How is concurrency handled in Scala?

a) Through callbacks
b) Using threads
c) Using futures and promises
d) Concurrency is not supported in Scala

10. What is a trait in Scala?

a) A type of class
b) A feature similar to interfaces in Java
c) A special kind of function
d) A package management feature

11. How do you define an implicit conversion in Scala?

a) implicit def intToString(x: Int): String = x.toString
b) def implicit intToString(x: Int): String = x.toString
c) implicit conversion intToString(x: Int): String = x.toString
d) convert implicit intToString(x: Int): String = x.toString

12. What is the correct way to declare a singleton object in Scala?

a) class SingletonObject
b) object SingletonObject
c) singleton object SingletonObject
d) def SingletonObject

13. Which Scala feature allows the creation of anonymous classes?

a) case classes
b) abstract classes
c) traits
d) sealed traits

14. How do you handle a null pointer exception in Scala in a more functional way?

a) Using try-catch blocks
b) Using the Option type
c) Using null checks
d) Scala automatically handles null pointer exceptions

15. Which method on a collection can be used to transform its items in Scala?

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

16. What does the following Scala code output?

val x = Some(5)
println(x.getOrElse(0))
a) 5
b) 0
c) Some(5)
d) None

17. How do you execute a side effect only when a Scala Future is completed?

a) future.run()
b) future.foreach(println)
c) future.map(println)
d) future.done()

18. What is the use of sealed keyword in Scala?

a) It prevents a class from being extended in any file other than where it's defined.
b) It seals a method to prevent it from being overridden.
c) It is used to make a variable immutable.
d) It seals a package.

19. How do you create an array in Scala?

val arr = Array(1, 2, 3)
a) val arr = Array(1, 2, 3)
b) var arr = Array(1, 2, 3)
c) val arr = new Array(1, 2, 3)
d) val arr = [1, 2, 3]

20. Which Scala collection is immutable and maintains insertion order?

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

21. What does lazy val do in Scala?

a) Delays the initialization of a variable until it is first accessed
b) Makes a variable thread-safe
c) Initializes a variable immediately
d) None of the above

22. What does the @tailrec annotation do in Scala?

a) Ensures that a method is tail recursive
b) Makes a method run in constant space
c) Optimizes a method to run faster
d) None of the above

23. What does the following Scala function return?

def addOne(x: Int): Int = x + 1
a) The input value incremented by one
b) The input value decremented by one
c) The input value multiplied by two
d) None of the above

24. Which construct in Scala is used to match against values?

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

25. What is the correct way to define a primary constructor in Scala?

a) class Person(val name: String, val age: Int)
b) class Person(name: String, age: Int) { val n = name; val a = age }
c) class Person { def this(name: String, age: Int) }
d) def Person(name: String, age: Int)

26. How do you create a tuple in Scala?

a) val hostPort = ("localhost", 80)
b) val hostPort = Tuple("localhost", 80)
c) val hostPort = new Tuple("localhost", 80)
d) val hostPort = ["localhost", 80]

27. Which Scala feature allows you to automatically generate getters and setters for class fields?

a) Traits
b) Pattern Matching
c) Case Classes
d) Implicit Classes

28. How do you declare an abstract class in Scala that cannot be instantiated directly?

abstract class Vehicle {
    def start(): Unit
}
a) abstract class Vehicle { def start(): Unit }
b) class abstract Vehicle { def start(): Unit }
c) class Vehicle { abstract def start(): Unit }
d) vehicle class { def start(): Unit }

29. What is the output of the following Scala code?

val numbers = List(1, 2, 3)
val doubled = numbers.map(_ * 2)
println(doubled)
a) List(2, 4, 6)
b) List(1, 2, 3, 2, 4, 6)
c) [2, 4, 6]
d) [1, 2, 3, 2, 4, 6]

30. How do you implement an interface in Scala?

a) class Car implements Drivable { def drive() = println("Driving") }
b) class Car extends Drivable { def drive() = println("Driving") }
c) class Car uses Drivable { def drive() = println("Driving") }
d) class Car with Drivable { def drive() = println("Driving") }

Comments