All topics
Control Flow and Methods
if and switch, the switch expression, loops, break and continue, methods with their parameters and return values.
Sort:
7 questions
JuniorTheoryVery commonWhat happens when this program is compiled and run?
JuniorTheoryVery common
What happens when this program is compiled and run?
It does not compile
Open full question →Common mistakes
- ✗Expecting
AB: in C# acasebranch cannot run on into the next one, and a branch withoutbreakstops compilation withCS0163. - ✗Expecting the compiler to add the
breakitself and printA: it adds nothing and reports an error instead.
Follow-up questions
- →How would you fix the program so that it prints
A? - →When may two
caselabels stand one under the other with nobreakbetween them?
JuniorTheoryCommonWhat does the program print?
JuniorTheoryCommon
What does the program print?
1245
Open full question →Common mistakes
- ✗Expecting
12:continuedoes not leave the loop the waybreakdoes; it ends only the current iteration, and the loop goes on withi = 4. - ✗Expecting the program to hang: in a
forloop the stepi++runs aftercontinuetoo; a loop hangs when it is awhilethat changes its variable aftercontinue.
Follow-up questions
- →What does the program print if
continueis replaced withbreak? - →How would you rewrite the loop without
continueso that the output stays the same?
JuniorTheoryCommonWhat does the program print?
JuniorTheoryCommon
What does the program print?
AC
Open full question →Common mistakes
- ✗Expecting
BC: the first true condition from the top picks the branch, not the strictest one, so withx = 7thex > 3branch runs. - ✗Expecting
ABC: anelse ifis checked only when every condition above it is false, so two true conditions never run two branches.
Follow-up questions
- →How would you reorder the branches so that the program prints
BCforx = 7? - →What does the program print when
xis2?
JuniorTheoryCommonWhat does the program print?
JuniorTheoryCommon
What does the program print?
12
Open full question →Common mistakes
- ✗Expecting
10: the stepi += 2runs after the body, so the first iteration hasi = 2, and the sum is2 + 4 + 6. - ✗Expecting
20: before an iteration withi = 8, the condition8 <= 6is false, so the loop ends without adding8.
Follow-up questions
- →What does the program print if the condition becomes
i < 6? - →How many times is the condition
i <= 6checked while the loop runs?
JuniorTheoryCommonWhat happens when this program is compiled and run?
JuniorTheoryCommon
What happens when this program is compiled and run?
It does not compile
Open full question →Common mistakes
- ✗Expecting
7 0: a method does not return0on its own; the compiler requires areturnon every path and stops the build withCS0161. - ✗Expecting a failure only when
Abs(7)runs: the compiler checks every path of the method before the program starts, so nothing is printed.
Follow-up questions
- →How would you fix
Absso that the program builds and prints7 7? - →Why is the method still rejected if it ends with
if (n >= 0) return n;?
JuniorTheoryCommonWhat does the program print?
JuniorTheoryCommon
What does the program print?
123
Open full question →Common mistakes
- ✗Expecting
1:breakends only the nearest loop, the inner one, while the outer loop goes on and prints2and3. - ✗Expecting
112233:breakdoes not skip one iteration the waycontinuedoes; it ends the inner loop, soj = 3never comes.
Follow-up questions
- →How would you change the program so that
breakstops both loops? - →How many times does
Console.Write(i)run if you remove theiftogether with thebreak?
JuniorTheoryCommonWhat does the program print (lines separated by spaces)?
JuniorTheoryCommon
What does the program print (lines separated by spaces)?
20 10
Open full question →Common mistakes
- ✗Expecting
20 20:pointsis a copy ofscore, so doubling it changes only the copy;Bonusreturns20, andscorestays10. - ✗Expecting
10 10or a compile error: a parameter is an ordinary variable of the method, so it can be assigned, andBonusreturns the new value.
Follow-up questions
- →How would you change the program so that
scoreitself becomes20? - →What would the program print if the parameter were named
scoretoo?