Problem overview
This is a multiple-choice quiz about a short piece of Kotlin code. The code defines a function named sayMyName whose body prints Heisenberg!, and it does nothing else. The question is what happens when this code is run, and the options are:
- A: it prints
Heisenberg!; - B: it produces errors;
- C: it prints nothing;
- D: it prints
sayMyName.
You answer by giving the variable answer the letter of your choice. The point is to answer by reading the code, not by running it.
Approach: definition versus call
Ask two separate questions about any function you see.
- Is there a definition? A definition, which starts with
fun, tells Kotlin that a function exists and what its body contains. - Is there a call? A call is the function's name followed by parentheses, like
sayMyName(). Only a call makes the body execute.
In the quiz there is a definition and no call. The statements inside the body, the println here, are stored as part of the function and not executed. Nothing in the code asks for them to run.
Step by step
- Look for a call. The only lines are the header
fun sayMyName() {, theprintlnand the closing brace. No other line mentionssayMyName. - Rule out option A. The
printlnis inside the body, which never runs, soHeisenberg!is never printed. - Rule out option D. A function's name is never printed just because it is defined. The only way to see it would be to pass it to
printlnas text, which this code doesn't do. - Rule out option B. The definition is well-formed: the keyword, a name, parentheses, a body in braces and a call to the built-in
printlnwith a correct string. It compiles. - Choose C. The code compiles, nothing calls the function and nothing is printed.
- Store the letter in
answer.
The solution in Kotlin
class Solution {
fun magicNumber(n: Int) {
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 and nothing will be printed to the console.
*/
println(answer)
}
}
The code in the editor is a small template and has nothing to do with the quiz code itself. The line that matters is var answer: String = "C". A few Kotlin details about it:
- The type comes after the name.
answer: Stringdeclares a variable namedanswerwhose type isString. The letter goes in double quotes, because it is text and not a name:"C"is a string, while a bareCwould be read as a reference to something calledC. varversusval. Herevaris used, which allows later reassignment. Since the answer is never changed,valwould be the more natural choice. Both pass the lesson's tests.- The explanation is a comment. Everything between
/*and*/is ignored by the compiler, so it can't affect the result. - The parameter
nis unused. The function takes anIntthat its body never reads. This is allowed, and it doesn't change the answer.
What running a call would do
If the quiz code did call the function, the result would change. This program defines sayMyName and calls it once from main, so it prints Heisenberg! a single time:
fun sayMyName() {
println("Heisenberg!")
}
fun main() {
sayMyName()
}
Heisenberg!
Without the call inside main, the same program compiles and prints nothing.
Edge cases and common mistakes
- Reading the body as if it ran. The most common wrong answer is A. The
printlnis right there, so it is tempting to see its output. Check for a call first. - Treating "nothing printed" as an error. A program with no output is valid. Errors come from the compiler rejecting the code, not from the lack of output.
- Writing the answer without quotes or with the wrong case. The value must be the string
"C", written in capital letters. - Running the quiz code to check. Trace the code by hand instead. That habit carries over to every later question about what code does.