Problem overview
This quiz shows two Kotlin functions. greetCoder prints two lines, Hello! and Let's code!. mainFunction calls greetCoder() two times in a row. You are asked what the code produces when run, and you pick one of four outputs:
- A:
Hello!andLet's code!, then both lines again (four lines); - B:
Hello!twice, thenLet's code!twice; - C:
Hello!andLet's code!once (two lines); - D: the pair of lines three times (six lines).
The answer goes into the variable answer as a string holding the option letter.
Approach: trace the calls
Don't guess from the look of the code. Walk through it the way Kotlin does, and keep a running list of what has been printed.
A function definition stores the body without running it. A call jumps into the body, runs every statement from top to bottom, and then returns to the line after the call. Each call is independent, so the second call starts from the first line of the body again. The body doesn't remember that it already ran.
Step by step
- Set the definitions aside. The definition of
greetCoderprints nothing by itself. The same is true of the header ofmainFunction. - Run the first call. It executes the two statements in order. The output so far is
Hello!followed byLet's code!. - Return to
mainFunction. The first call is finished, and execution continues with the next statement, the second call. - Run the second call. The body runs again from the top, so the output grows by the same two lines.
- Compare the full output with the options. It is the four lines
Hello!,Let's code!,Hello!,Let's code!. That is option A. - Store
"A"inanswer.
The solution in Kotlin
val answer: String = "A"
/* Explanation:
In the first 4 lines of code we only define the function, so no "println()" statement will actually get executed.
Then, inside the main function, we call the function for the first time, so all the statements inside the function body will get executed from top to bottom.
So, the first function call will print "Hello!", then it will print "Let's code!" on two separate lines, and then this function call ends.
The output so far is:
Hello!
Let's code!
Then we call the function for the second time, and again all the statements inside the function body will get executed from top to bottom.
So the second function call will again print "Hello!", then it will print "Let's code!" on two separate lines, and then the function call ends.
So the output is:
Hello!
Let's code!
Hello!
Let's code!
*/
The Kotlin details in this short solution:
valfits here. The answer is chosen once and never changes, so a read-onlyvalis the right kind of variable. Assigning to it a second time wouldn't compile.- The type follows the name.
answer: Stringsays thatanswerholds text. The letter is in double quotes because it is a string literal. - The explanation is a block comment. Text between
/*and*/is ignored by the compiler, even if it spans many lines and contains quotes or code-like text.
Seeing it run
Running the quiz's code as a standalone program, with a main that calls mainFunction, prints exactly the four lines of option A:
Hello!
Let's code!
Hello!
Let's code!
Use this only after you have traced the code yourself. The point of the quiz is to build the habit of predicting the output.
Edge cases and common mistakes
- Picking B. This treats each call as running one statement and then handing control to the other call. A call always finishes its whole body before the next statement of the caller starts, so the lines can't interleave.
- Picking C. This assumes a function runs only once, however many times it is called. Every call runs the body again.
- Picking D. This counts the definition as one more run. A definition never prints anything, and the two calls produce two copies of the output.
- Miscounting lines. Each call prints two lines, not one. Two calls therefore print four.
As a general rule, the number of times a body runs equals the number of calls that actually execute, and the output of each run is the same when the body doesn't depend on any input.