Problem overview
The quiz shows a single Scala function and asks what happens when the code runs:
def sayMyName(): Unit = {
println("Heisenberg!")
}
There are four options: it prints Heisenberg! (A), it produces errors (B), it prints nothing (C), or it prints sayMyName (D). You answer by assigning the letter to a variable called answer, so the task is to reason about the code, not to run it.
Approach: separate defining from calling
Two different things can happen to a function, and the quiz tests whether you can tell them apart:
- Defining it with
deftells Scala the function exists and what its body is. The body is stored, not executed. - Calling it, by writing its name followed by parentheses, runs the body.
The code above contains a definition and nothing else. There is no sayMyName() anywhere, so the body never executes and the println inside it never runs.
Checking each option
- A: it prints
Heisenberg!. This would be true if the code contained the callsayMyName(). It doesn't, so nothing prints. - B: it produces errors. The definition is valid. It has
def, a name, empty parentheses, the result typeUnit, an=and a body in braces, so it compiles. Defining a function and never using it is not an error. - C: it prints nothing. This is the right answer.
- D: it prints
sayMyName. A function's name is never printed by defining it. The only text the code can print is the string insideprintln, and that happens only on a call.
The answer is C.
The solution in Scala
The starter code gives you a Solution class with a method that prints the value of answer. Your job is to declare that variable with your choice at the top of the method:
class Solution {
def magicNumber(n: Int): Unit = {
var answer: String = "C"
/* Explanation:
Remember, the code inside a function will not be executed unless we call that function!
In our code, we only define the function sayMyName(), without calling it afterwards, so the println() won't be executed & nothing will be printed to the console.
*/
println(answer)
}
}
Some Scala details in these lines:
- The type annotation.
var answer: String = "C"states the type,String. Scala would infer it from the literal, sovar answer = "C"means the same. - Quotes matter. The letter is a string in double quotes. Without the quotes,
Cwould be read as a name, and the compiler reportsNot found: C. - Declare it before use. The
println(answer)line refers toanswer, so the declaration has to come first. Without it, the compiler saysNot found: answer. - var and val. The variable is never reassigned, so
valwould also work and is the more usual choice for a value that doesn't change.varis fine here. - Indentation. Scala 3 rejects a block that mixes tabs and spaces in the indentation, so indent consistently.
Seeing the difference
If the quiz code had one more line, the answer would change. With a call added after the definition:
def sayMyName(): Unit = {
println("Heisenberg!")
}
sayMyName()
this prints Heisenberg! once, which is option A. Each additional call prints it again.
Common mistakes
- Treating a definition as a call. Seeing a
printlnin the code and assuming it runs. Aprintlninside a function body runs only when the function is called. - Expecting an error for an unused function. Unused functions are legal.
- Leaving out the quotes around the letter.
- Running the code to find the answer. The quiz is about predicting the result from the rules, which is the skill the later lessons build on.
