Problem overview
This quiz shows two functions. welcomeUser prints Hey coder!. mainFunction prints Welcome back! and then calls welcomeUser(). The question is what the code prints, with these options:
- A:
Hey coder!followed byWelcome back!; - B: only
Welcome back!; - C: errors;
- D:
Welcome back!followed byHey coder!.
You answer by assigning the option letter, as a string, to the variable answer.
Approach: follow the flow of execution
The execution flow is the order in which statements actually run. Two rules decide it:
- Inside a function body, statements run from top to bottom, one after the other.
- When a statement is a call to another function, the flow jumps into that function, runs its whole body and comes back to the line after the call.
Where a function is written in the file does not decide when it runs. In the quiz, welcomeUser is defined first, but defining it doesn't print anything. Its body runs when it is called, and this happens in the middle of mainFunction.
Step by step
- Find the starting point. The lesson treats
mainFunctionas the main program, so its statements are the ones that run. - Run the first statement. It is
println("Welcome back!"), so the output starts withWelcome back!. - Run the second statement. It is the call
welcomeUser(). The flow goes into the function, runsprintln("Hey coder!")and returns. - Read off the output. It is
Welcome back!and thenHey coder!on a new line each. That is option D. - Check for errors. Both functions are well-formed, and
mainFunctioncalls a function that exists with the right name and parentheses. The code compiles, so option C is out.
The solution in Kotlin
val answer: String = "D"
/* Explanation:
We first only define the welcomeUser() function, so the println("Hey coder!") won't actually be executed for now.
The mainFunction() is our "main program" so all the statements inside there will be executed in the order they are written.
First one is the "println("Welcome back!")", which will be executed and the output so far is:
Welcome back!
Then, the "welcomeUser()" function call will be executed.
So the code inside the function body (println("Hey coder!")) will get executed now and the output is:
Welcome back!
Hey coder!
*/
The Kotlin details in this short solution:
- A read-only variable.
val answer: String = "D"declares a variable that can't be reassigned. The option letter is a string, so it is written in double quotes. - Quotes inside a comment. The explanation contains lines such as
println("Welcome back!"). Inside a/* ... */comment, those quotes are ordinary text and don't have to be balanced or escaped. - Function order in the file. If
welcomeUserwere defined belowmainFunction, the output would be the same. The output depends on the statements inmainFunction, not on where each function's text appears.
Seeing it run
Put the two functions in a file with a main that calls mainFunction(), and the program prints the following:
Welcome back!
Hey coder!
Try to predict the output of a program before running it. If your prediction and the result disagree, the difference tells you which rule of the flow you have misapplied.
Edge cases and common mistakes
- Choosing A because of the file order. The tempting reasoning is that
welcomeUserappears first, so its message comes first. Writing a function first only defines it. The flow follows the calls inmainFunction. - Choosing B because the function seems not to run. It is called, so it does run. A function that is defined and never called would be the case where nothing is printed.
- Choosing C because there is no
main. The quiz shows the code in the lesson's own setting, wheremainFunctionis the main program. Judge what the code does, not how it would be launched elsewhere. - Forgetting that a call returns. After
welcomeUser()finishes, the flow continues inmainFunction. Here the call is the last statement, so the program then ends. If another statement followed it, that statement would print afterHey coder!.