All topics
Control Flow
Comparisons and logical operators, if and else, the classic switch with fall-through, arrow switch and switch expressions, while, do-while and for loops, break, continue and labels.
Sort:
8 questions
JuniorTheoryCommonWhat does the program print?
JuniorTheoryCommon
What does the program print?
1357
Open full question →Common mistakes
- ✗Expecting
13579:breakends the loop at once, before the rest of the body runs, so 9 is never printed. - ✗Expecting
1:continuedoes not leave the loop; it skips the rest of this lap, and the loop goes on with the nexti.
Follow-up questions
- →What would the program print if
continuewere replaced withbreak? - →Why can
continueturn awhileloop into an endless one?
JuniorTheoryCommonWhat does the program print?
JuniorTheoryCommon
What does the program print?
10 11
Open full question →Common mistakes
- ✗Expecting
10: that is thewhilerule. Ado-whilechecks its condition after the body, so the body runs once even though10 < 5is false. - ✗Expecting an endless loop:
do-whilerepeats while the condition is true, not until it becomes true, so it stops after the first check.
Follow-up questions
- →How would the output change if the
do-whilewere replaced by awhilewith the same condition? - →When does a
do-whileloop fit a task better than awhileloop?
JuniorTheoryCommonWhat does the program print?
JuniorTheoryCommon
What does the program print?
B
Open full question →Common mistakes
- ✗Expecting nothing: indentation does not group statements in Java; without braces only
print("A")belongs to theif, andprint("B")always runs. - ✗Expecting a compile error: braces are optional for a one-statement branch, so the code compiles and only the indentation misleads.
Follow-up questions
- →How would you change the code so that neither letter is printed?
- →Why do style guides require braces even around a one-line branch?
JuniorTheoryCommonWhat does the program print?
JuniorTheoryCommon
What does the program print?
1232
Open full question →Common mistakes
- ✗Expecting
1232369: abreakwith a label ends the loop that the label marks, here the outer one, so the laps fori = 3never run. - ✗Expecting an endless loop: a label is not a place to jump back to;
break outerleaves the marked loop and goes on after it.
Follow-up questions
- →What would the program print with
continue outerin place ofbreak outer? - →How else can you leave two nested loops at once without a label?
JuniorTheoryCommonWhat happens when the program is compiled and run?
JuniorTheoryCommon
What happens when the program is compiled and run?
It does not compile
Open full question →Common mistakes
- ✗Expecting
6 4: a variable declared in theforheader lives only inside the loop, soiis an unknown name after it and javac reportscannot find symbol. - ✗Expecting a failure at run time: javac checks every name before the program runs, so nothing is printed, not even
sum.
Follow-up questions
- →How would you change the code to print the value
ihas after the loop? - →Can two loops, one after the other in
main, both declareint i?
JuniorTheoryCommonWhat does the program print?
JuniorTheoryCommon
What does the program print?
false
Open full question →Common mistakes
- ✗Expecting an
ArithmeticException:&&evaluates left to right and skips the right side once the left isfalse, so10 / xnever runs. - ✗Expecting a compile error: the compiler does not follow the value of
x; a division by a variable compiles, and only a run could fail.
Follow-up questions
- →What changes if the two sides of
&&are swapped? - →When does
||skip its right side?
JuniorTheoryCommonWhat happens when the program is compiled and run?
JuniorTheoryCommon
What happens when the program is compiled and run?
It does not compile
Open full question →Common mistakes
- ✗Expecting
nullor a blank line: a switch expression must cover every possible value ofn, and withoutdefaultthe compiler rejects it. - ✗Expecting a run-time error: the compiler checks coverage before the program runs, whatever value
nholds.
Follow-up questions
- →How does
yieldhand a value out of a block branch of a switch expression? - →Why does a switch expression over an
enumneed nodefaultwhen it lists every constant?
JuniorTheoryCommonWhat does the program print?
JuniorTheoryCommon
What does the program print?
bc
Open full question →Common mistakes
- ✗Expecting
b: acasewith a colon does not end on its own; withoutbreakexecution falls intocase 3without checking its value. - ✗Expecting
abcorbcd:switchstarts at the matchingcase, not at the top, and thebreakaftercends it beforedefault.
Follow-up questions
- →How would you make
d = 2print onlyb? - →When is falling through to the next case useful on purpose?