The question
The quiz shows two methods, one of which calls the other:
void welcomeUser() {
Console.WriteLine("Hey coder!");
}
void mainFunction() {
Console.WriteLine("Welcome back!");
welcomeUser();
}
You pick the output from four options: A is Hey coder! then Welcome back!; B is just Welcome back!; C is a compile error; D is Welcome back! then Hey coder!.
Tracing the flow
The quiz is about execution flow: which line runs when. Take it one step at a time.
welcomeUseris defined first. That only registers the method. Its print statement doesn't run yet.mainFunctionis the program's starting point, so its statements run in the order they are written.- The first statement prints
Welcome back!. - The second statement is the call
welcomeUser();. Control jumps intowelcomeUser, which printsHey coder!. - When that body ends, control returns to
mainFunction, which has no more statements.
The output is:
Welcome back!
Hey coder!
That is option D.
Why the other options are wrong
- A reverses the order. It would be right only if
mainFunctioncalledwelcomeUser()before its own print line. Position in the body decides when something happens, not which method was defined first. - B forgets the call. The method is called, so its message appears.
- C expects an error, but the code is valid. Each method has a return type, a name, parentheses and a braced body, and the call has parentheses and a semicolon.
The answer in C#
The solution assigns the letter D to a string variable, followed by the lesson's explanation comment:
string answer = "D";
/* Explanation:
We first only define the welcomeUser() function, so the Console.WriteLine("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 "Console.WriteLine("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 (Console.WriteLine("Hey coder!");) will get executed now and the output is:
Welcome back!
Hey coder!
*/
A note on indentation
The lines inside each method are indented, and that is what makes it easy to see which statements belong to which method. In C# the indentation is only for people. The compiler goes by the curly braces, so moving a statement inside or outside a method requires moving it relative to the braces, not just changing its spaces. Python reads indentation as structure, but C# does not.
Variations to try in your head
Changing one thing and predicting again is the best way to make the idea stick. Using the same two methods:
Swapping the statements in mainFunction
If mainFunction called welcomeUser(); first and printed Welcome back! afterwards, the output would be reversed:
Hey coder!
Welcome back!
That output is option A, which shows the option is not nonsense: it is the answer to a slightly different program. The quiz works by offering the results of such near misses, so read the order of statements carefully.
Never calling welcomeUser
If the call were deleted, only Welcome back! would print. That is option B. Again it is the answer to a program with one fewer line.
Moving the definition
If welcomeUser were defined below mainFunction instead of above it, the output would not change. Where a method sits in the class doesn't affect when it runs, because the compiler reads all the definitions before the program starts. Only the calls and the order of the statements decide the output.
What the quiz leaves out
The quiz code never says who calls mainFunction. Quizzes of this kind treat it as the starting point of the program, the way the lesson exercises treat MainFunction. That is why the trace begins there. Methods that nobody calls, directly or indirectly, never run, so a quiz needs one entry point to give the code something to do.
A step-by-step habit
For any question like this, write the output as you go. Begin with an empty output, then walk through the starting method one statement at a time. When a statement prints, add its text. When a statement is a call, go to that method and walk through its body the same way, then return and carry on. By the end the list you've written is the answer. Doing it on paper takes a minute, and it removes the guesswork that makes this kind of question feel tricky.
The lesson's title mentions indentation. The indented lines are a visual hint about which statements belong to which method, but the braces are what truly decide it.
Reading the options
Quiz options are written to match common slips: the order reversed, a line forgotten, or an error that isn't there. After you trace the program, compare your trace with each option rather than choosing the first one that looks familiar. Only one option should match the trace exactly, line for line.
What to remember
- Statements in a body run top to bottom.
- A call pauses the caller, runs the whole called body, then continues with the next statement in the caller.
- The place where a method is defined doesn't decide when it runs. Calls do.