The question
The quiz shows two methods and asks what the program prints:
void greetCoder() {
Console.WriteLine("Hello!");
Console.WriteLine("Let's code!");
}
void mainFunction() {
greetCoder();
greetCoder();
}
The four options differ in how the two messages are arranged: A is Hello! and Let's code! twice in a row; B groups both Hello! lines before both Let's code! lines; C prints the pair once; D prints it three times.
Tracing the program
The first four lines only define greetCoder. Defining prints nothing. The output comes from mainFunction, which is the program's starting point, and it contains two calls:
- The first call runs the whole body of
greetCoderfrom top to bottom: it printsHello!and thenLet's code!, each on its own line. The call ends. - The second call starts from the beginning of the body again and prints the same two lines.
The total output is:
Hello!
Let's code!
Hello!
Let's code!
Why the other options are wrong
- B would mean the two calls interleave their lines. A call never pauses halfway to let another one start. Each call finishes its whole body before the next statement in
mainFunctionruns. - C would mean the second call does nothing. Every call runs the body again, no matter how many times the method has already run.
- D counts three runs, but the code has only two call lines. The definition itself isn't a run.
The answer in C#
Option A is correct. The solution stores that letter in a string variable called answer, and the lesson's own explanation comment stays below it:
string answer = "A";
/* Explanation:
In the first 4 lines of code we only define the function, so no "Console.WriteLine()" 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 letter must be a capital inside double quotes, and the variable must be named exactly answer. In C# "A" is a string and 'A' would be a single character of a different type, so keep the double quotes.
Variations to try in your head
Once the basic trace is clear, change the code and predict again. These variations all follow from the same rule.
A third call
If mainFunction had a third greetCoder(); line, the pair of messages would print three times, six lines in all. That is what option D describes, so the option is plausible only for a program with three call lines. A quick check is to multiply: the number of calls times the number of print statements in the body gives the number of lines. Here that is 2 calls times 2 prints, which is 4 lines.
A method that calls another method
A method body can contain calls too. Consider this pair:
void Inner() {
Console.WriteLine("inner");
}
void Outer() {
Console.WriteLine("start");
Inner();
Console.WriteLine("end");
}
Each call to Outer() prints start, then runs all of Inner (which prints inner), then prints end. Calling Outer() twice therefore gives this output:
start
inner
end
start
inner
end
Even here, the second call to Outer doesn't begin until the first one has completely finished, including everything it called.
No calls at all
If mainFunction were empty, nothing would print, however many statements greetCoder contained. A body that is never called is dead weight until something calls it.
Details the quiz code relies on
- A call needs parentheses.
greetCoder();is a call. WritinggreetCoder;is a compile error, because a bare method name isn't a statement. - Calls are statements, so each ends with a semicolon. The method definition's closing brace doesn't need one.
- Naming. The quiz uses lowercase names like
greetCoder. Convention in C# is PascalCase,GreetCoder, which is what you'll see in the coding exercises. The compiler accepts both, as long as every use matches the definition letter for letter.
A habit for quiz questions
Don't try to see the whole answer at once. Cross out the definitions, since they print nothing, and list only the calls in the order they run. Then replace each call with the body it runs. What you get is a plain list of print statements, and the output is that list from top to bottom. The same habit works for any program that uses methods, however many of them there are.
Why repeat a call instead of copying the lines
This quiz shows the reason methods exist. Without greetCoder, the program would need four print statements in mainFunction, and a change to a message would have to be made twice. With the method, the messages are written once and each call reuses them. If a third greeting were needed, you'd add one line, not two, and if the wording changed, you'd edit one place.
What to remember
- A call runs the entire method body, top to bottom, every time it is made.
- The number of runs equals the number of calls, not the number of definitions.
- Lines inside a method run in order and are never interleaved with another call's lines.