Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Quiz

Question 1: Collection Filtering

Review the Scala code below:

val nums = List(-1, 2, -3, 4, 5)
val positiveNums = nums.filter(_ > 0)

What does positiveNums contain?

A) List(-1, -3)
B) List(2, 4, 5)
C) List(1, 2, 3, 4, 5)
D) List(-3, -1, 5, 4, 2)

Question 2: Tuples and Destructuring

Consider the Scala tuple:

val person = ("John", 30)

How do you access the age from the person tuple?

A) person(1)
B) person._2
C) person[1]
D) person.age

Question 3: Defining Classes

How do you correctly define a simple class in Scala that takes one parameter?

A) class MyClass(param: Type)
B) def MyClass(param: Type)
C) object MyClass { val param: Type }
D) new class MyClass(param: Type)

Question 4: Scala Collections

Which of the following collections is immutable?

A) ArrayBuffer
B) List
C) Array
D) ListBuffer

Question 5: Object-Oriented Programming

Which of the following statements about Scala is true?

A) Scala supports multiple inheritance through classes.
B) Scala does not allow the use of generic types.
C) Scala allows defining singleton objects using the object keyword.
D) Scala classes cannot have parameters.

Question 68: Defining and Instantiating a Simple Class

Consider the following Scala code snippet:

class Person(name: String, age: Int) {
  def greet(): String = s"Hello, my name is $name and I am $age years old."
}

val alice = new Person("Alice", 30)
println(alice.greet())

val bob = new Person("Bob", 25)
println(bob.greet())

What is the output of this code?

A) Hello, my name is Alice and I am 30 years old.
Hello, my name is Bob and I am 25 years old.
B) Compilation error
C) Hello, my name is Person and I am 30 years old.
Hello, my name is Person and I am 25 years old.
D) None of the above

Question 6: Class with Private Members

Review this Scala code snippet for a class with private members and methods:

class Counter {
  private var count = 0

  private def checkLimit(): Boolean = count < 10

  def increment(): Unit = {
    if (checkLimit()) count += 1
  }

  def getCount(): Int = count
}

val myCounter = new Counter()
for (_ <- 1 to 15) myCounter.increment()
println(myCounter.getCount())

What is the output of this program?

A) 10
B) 15
C) 0
D) Compilation error

Question 8: Overriding Methods in Subclasses

Consider the following Scala code snippet demonstrating method overriding:

class Animal {
  def sound(): String = "Some sound"
}

class Dog extends Animal {
  override def sound(): String = "Woof"
}

val myAnimal = new Animal()
println(myAnimal.sound())

val myDog = new Dog()
println(myDog.sound())

What does this code print?

A) Some sound
Woof
B) Woof
Woof
C) Some sound
Some sound
D) Compilation error

Question 9: Abstract Classes and Traits

Analyze the Scala code snippet involving an abstract class and its implementation:

abstract class Shape {
  def area: Double
}

class Circle(radius: Double) extends Shape {
  def area: Double = Math.PI * radius * radius
}

val circle = new Circle(5)
println(f"Circle area: ${circle.area}%.2f")

What is the output of this code?

A) Circle area: 78.54
B) Circle area: 25.00
C) Compilation error
D) None of the above

Question 10: Defining and Implementing Traits

Consider the following Scala code snippet demonstrating a trait and its implementation:

trait Greeter {
  def greet(name: String): Unit
}

class FormalGreeter extends Greeter {
  def greet(name: String): Unit = {
    println(s"Good day, $name.")
  }
}

val greeter = new FormalGreeter()
greeter.greet("Alice")

What is the output of this code?

A) Good day, Alice.
B) Hello, Alice.
C) Compilation error
D) None of the above

Question 11: Mixing in Multiple Traits

Analyze the Scala code snippet below, which mixes multiple traits into a class:

trait Walker {
  def walk(): Unit = println("Walking...")
}

trait Runner {
  def run(): Unit = println("Running...")
}

class Person extends Walker with Runner

val p = new Person()
p.walk()
p.run()

What does this code print?

A) Walking...
Running...
B) Compilation error
C) Walking...
D) Running...

Question 12: Overriding Trait Methods in Classes

Review the following Scala code snippet involving overriding trait methods in a class:

trait Animal {
  def makeSound(): Unit
}

class Dog extends Animal {
  override def makeSound(): Unit = {
    println("Woof")
  }
}

val myDog = new Dog()
myDog.makeSound()

What is the output of this program?

A) Woof
B) Compilation error
C) The program prints nothing
D) None of the above

Question 13: Abstract and Concrete Methods in Traits

Consider the Scala code snippet demonstrating a trait with both abstract and concrete methods:

trait Calculator {
  def add(a: Int, b: Int): Int
  def subtract(a: Int, b: Int): Int = a - b
}

class BasicCalculator extends Calculator {
  def add(a: Int, b: Int): Int = a + b
}

val calc = new BasicCalculator()
println(calc.add(5, 3))
println(calc.subtract(5, 3))

What does this code print?

A) 8
2
B) Compilation error
C) 8
D) 2

Question 14: Combining map and filter

Review the Scala code snippet involving a combination of map and filter:

val names = List("Alice", "Bob", "Charlie", "David")
val filteredNames = names.filter(_.length > 4).map(_.toUpperCase)

println(filteredNames)

What is the output of this program?

A) List("ALICE", "CHARLIE", "DAVID")
B) List("BOB", "DAVID")
C) List("ALICE", "CHARLIE")
D) List("CHARLIE", "DAVID")

Question 15: Matching Tuples

Review the Scala code snippet involving matching tuples:

val pair = (1, "apple")
val description = pair match {
  case (i, s) if i == 1 => s"One $s"
  case (i, s) if i > 1 => s"$i ${s}s"
  case _ => "Unknown"
}
println(description)

What is the output of this program?

A) One apple
B) 1 apples
C) Unknown
D) Compilation error

Question 16: Matching with Lists

Consider the following Scala code snippet that matches lists:

val myList = List(1, 2, 3)
val listDescription = myList match {
  case List(_, _, _) => "A list with three elements."
  case List(_, _) => "A list with two elements."
  case _ => "A list with a different number of elements."
}
println(listDescription)

What does this code print?

A) A list with three elements.
B) A list with two elements.
C) A list with a different number of elements.
D) None of the above

Question 17: Matching with Guards

Examine the Scala code snippet involving match expressions with guards:

val age = 25
val accessLevel = age match {
  case a if a >= 18 && a < 21 => "Partial access"
  case a if a >= 21 => "Full access"
  case _ => "No access"
}
println(accessLevel)

What is the output of this code?

A) Partial access
B) Full access
C) No access
D) Compilation error

Question 18: For-Comprehension with Multiple Generators

Review this Scala for-comprehension involving multiple generators:

val xValues = List(1, 2, 3)
val yValues = List(4, 5)

val pairs = for {
  x <- xValues
  y <- yValues
} yield (x, y)

println(pairs)

What does this code print?

A) List((1,4), (1,5), (2,4), (2,5), (3,4), (3,5))
B) List((1,4), (2,5), (3,4))
C) List([1,4], [1,5], [2,4], [2,5], [3,4], [3,5])
D) Compilation error