CS U111 · mid-sem prep · drill

Operators, I/O & selection: predict the output

Six short programs per sheet, freshly generated each time, in the ten trick patterns that the last three mid-sem papers kept reusing: &&/|| stopping early, ++ before or after, the comma operator, integer division, ?: chains, switch without break, scanf formats, & versus &&, characters as numbers, and printf widths. A sheet takes about fifteen minutes.

How to use it

Trace each program on paper first, then type exactly what it prints. If it prints several lines, press Enter between them or type ⏎. Extra spaces don't count against you, except in the printf widths family (O10), where spaces are the whole point. Reveal marks every answer. See the method shows the step-by-step trace and the one rule the program tests. If a family keeps beating you, read its rule at the bottom of this page, then that topic in the operators & selection notes.

Why these ten

They are the question types from the three past mid-sem papers of this course's predecessor, CS F111 (past papers, worked). Each rule at the bottom links to the original questions. These mechanics are new to almost everyone. School computer science rarely covers the comma operator or scanf field widths, so a few sheets of practice are a real edge.

Operators
Selection
Input / output
The ten rules the families test

O1. && stops at the first operand that is 0; || stops at the first that is non-zero. Anything skipped, including a ++ or -- inside it, never runs. && binds tighter than ||, and both give exactly 0 or 1. Past papers: 2024 Q1 · 2025 Q2(d)

O2. x++ hands over the old value and then increments; ++x increments first and hands over the new value. In a ternary only the chosen branch runs. Past papers: 2024 Q3

O3. A comma list is evaluated left to right, and its value is the last item. The comma has the lowest precedence of all, so z = 5, 20; sets z to 5. Past papers: 2024 Q3

O4. int ÷ int drops the fraction (toward zero), and % keeps the sign of the left side. The fraction survives only if an operand is already a double when the division happens. A cast on the result is too late. Past papers: 2024 Q5 · 2025 Q4(c) · 2026 Q5

O5. c1 ? A : c2 ? B : C is an if–else-if ladder: the first true condition wins. Only the chosen branch is evaluated. Past papers: 2026 Q6 · 2024 Q14

O6. switch jumps to the matching label (or to default, wherever it is written) and runs downward through every later label until a break. Past papers: 2024 Q7 · 2025 Q2(i)

O7. A width (%2d) is the most characters one conversion may read. A plain character in the format must match the next input character exactly, spaces included. %d and %f skip leading spaces, but %c does not. scanf returns how many variables it filled, and stops at the first mismatch. Past papers: 2024 Q2 · 2024 Q15

O8. & and | are bitwise: they always evaluate both sides and combine bits, so 1 & 4 is 0. && and || are logical and short-circuit. n & mask with mask <<= 1 tests one bit at a time. Past papers: 2025 Q2(b), (h) · 2026 Q10

O9. A char is its code: '0' is 48, 'A' is 65, 'a' is 97. c - '0' gives a digit's value, %c prints the character and %d prints the code. Past papers: 2025 Q4(d)

O10. %5d means at least 5 characters, padded on the left; %-5d pads on the right. .2 means 2 decimals, rounded. A number wider than the field is never cut. Past papers: 2024 Q2 · 2025 Q2(e), (j)