Quiz
Question 1: Variable Declarations
Consider the following Scala code snippet:
val x: Int = 10
var y: Int = 20
y = x + 10
val z: Int = y + 10
What is the value of z at the end of this execution?
A) 20
B) 30
C) 40
D) 50
Question 2: Function Definition
Review the Scala function below:
def multiply(a: Int, b: Int): Int = a * b
What does this function do?
A) Adds two numbers
B) Subtracts two numbers
C) Multiplies two numbers
D) Divides two numbers
Question 3: Immutable List Operations
Given the Scala code:
val numbers = List(1, 2, 3, 4, 5)
val doubled = numbers.map(_ * 2)
What does the doubled list contain?
A) List(2, 4, 6, 8, 10)
B) List(1, 2, 3, 4, 5)
C) List(0, 2, 4, 6, 8)
D) List(3, 4, 5, 6, 7)
Question 4: Pattern Matching
Consider this Scala code snippet using pattern matching:
val greeting = "Hello"
greeting match {
case "Hello" => "World"
case "Goodbye" => "Everyone"
case _ => "Unknown"
}
What is the result of the pattern matching?
A) World
B) Everyone
C) Unknown
D) Error
Question 5: Variable Mutability
Which keyword is used to declare a mutable variable in Scala?
A) let
B) var
C) val
D) const
Question 6: Scala's Type Inference
What does Scala's type inference feature allow you to do?
A) Automatically detect errors in your code
B) Infer the type of variables at runtime
C) Omit the type of a variable when it is declared
D) Change the type of a variable after it is initialized
Question 7: Function Declarations
How do you declare a function in Scala that takes two integers and returns their sum?
A) def sum(x: Int, y: Int): Int = { return x + y }
B) function sum(x: Int, y: Int): Int = x + y
C) def sum(x: Int, y: Int) => x + y
D) Both A and C are correct
Question 8: Using a while Loop
Consider the following Scala code snippet:
var sum = 0
var i = 1
while (i <= 5) {
sum += i
i += 1
}
println(sum)
What is the output of this code?
A) 10
B) 15
C) 5
D) 20
Question 9: Iterating with for Loop
Analyze the Scala code below:
var product = 1
for (i <- 1 to 4) {
product *= i
}
println(product)
What does this code print?
A) 10
B) 24
C) 12
D) 4
Question 10: Nested for Loops
Consider the following Scala code snippet that uses nested for loops:
val rows = 3
val cols = 2
for (row <- 1 to rows) {
for (col <- 1 to cols) {
print(s"($row,$col) ")
}
println()
}
What does this code print?
A) (1,1) (1,2)
(2,1) (2,2)
(3,1) (3,2)
B) (1,1) (2,1) (3,1)
(1,2) (2,2) (3,2)
C) (1,1) (1,2) (2,1) (2,2) (3,1) (3,2)
D) None of the above
Question 11: Basic if-else Logic
Consider the Scala code snippet below:
val age = 20
val canVote = if (age >= 18) {
println("You can vote.")
true
} else {
println("You cannot vote.")
false
}
println(s"Can vote: $canVote")
What is the output of this code?
A) You can vote.
Can vote: true
B) You cannot vote.
Can vote: false
C) You can vote.
Can vote: false
D) Compilation error.
Question 12: Nested if-else with Logical Operators
Review the Scala code snippet involving nested if-else and logical operators:
val age = 25
val hasLicense = true
if (age >= 18) {
if (hasLicense) {
println("You can drive.")
} else {
println("You cannot drive without a license.")
}
} else {
println("You are too young to drive.")
}
What is the output of this program?
A) You can drive.
B) You cannot drive without a license.
C) You are too young to drive.
D) None of the above
Question 13: if-else with Compound Conditions
Consider the following Scala code snippet:
val temperature = 30
val isRaining = false
if (temperature > 25 && !isRaining) {
println("It's a good day for a walk.")
} else if (temperature > 25 && isRaining) {
println("It's warm, but it's raining.")
} else {
println("Stay indoors.")
}
What does this code print?
A) It's a good day for a walk.
B) It's warm, but it's raining.
C) Stay indoors.
D) Compilation error.
Question 14: Complex if-else with Function Calls
def checkEligibility(age: Int, hasPermit: Boolean): String = {
if (age >= 18) {
if (hasPermit) "Eligible" else "Not eligible: No permit"
} else {
"Not eligible: Too young"
}
}
val result = checkEligibility(17, hasPermit = true)
println(result)
What is the output of this code?
A) Eligible
B) Not eligible: No permit
C) Not eligible: Too young
D) Compilation error
Question 15: Basic Pattern Matching
Consider the following Scala code snippet:
val number = 3
val result = number match {
case 1 => "One"
case 2 => "Two"
case 3 => "Three"
case _ => "Other"
}
println(result)
What is the output of this code?
A) One
B) Two
C) Three
D) Other
Question 16: Defining and Calling a Simple Function
def greet(name: String): String = {
return "Hello, " + name + "!"
}
val greeting = greet("Alice")
println(greeting)
What is the output of this code?
A) Hello, Alice
B) Hello, Alice!
C) Greet: Alice
D) None of the above