Screening tests lean heavily on output-prediction MCQs because they reveal whether you truly read code or just recognise it. The good news: almost all of them reuse a small set of language-specific gotchas you've already met. The skill is a calm, systematic read rather than a guess.
A checklist for any 'what prints?' question
Run through five things in order: (1) which language are the rules from? (2) the operand types (int vs float, value vs reference)? (3) the exact operator behaviour (division, increment, precedence)? (4) any mutation/aliasing of shared objects? (5) scope and binding (closures, defaults).
Four classic gotchas
# Python: mutable default argument
def add(x, lst=[]): lst.append(x); return lst
print(add(1)); print(add(2)) # [1] then [1, 2] (shared!)
// Java: == vs equals
String a = new String("x"), b = new String("x");
System.out.println(a == b); // false (different objects)
/* C */ printf("%d", 5 / 2); // 2 (integer division)
# Python: print(5 / 2) # 2.5 (true division)⚡ The edge
- Most output traps are one of a handful you've seen: integer vs true division, post/pre increment, == vs equals / is, string immutability, mutable defaults, and aliasing. Recognise the family and you have the answer.
- Read types and mutation, not just syntax. The same line behaves differently depending on whether operands are ints or floats, and whether an object is shared — slow down and track both.
Worked example
Predict the output: (Python) a=[1,2]; b=a; b.append(3); print(a)
- b = a does not copy the list; both names refer to the same list object (aliasing).
- b.append(3) mutates that shared list in place.
- So printing a shows the change too: [1, 2, 3].
Answer: [1, 2, 3] — b is an alias for the same list, so appending through b is visible through a.
Worked example
Predict the output: (Java) int i = 1; System.out.println(i++ + ++i);
- i++ uses the current value 1, then i becomes 2 (post-increment).
- ++i increments i to 3 first, then uses 3 (pre-increment).
- So the sum is 1 + 3 = 4 (and i ends at 3). Unlike C, Java defines this evaluation order.
Answer: 4 — i++ contributes 1 (then i=2), ++i makes i=3 and contributes 3; 1 + 3 = 4.
⚠ Watch out
- Integer vs true division (5/2): 2 in C/Java, 2.5 in Python.
- == vs equals / is: comparing references vs content (Java), identity vs value (Python).
- Mutable defaults and aliasing in Python; string immutability everywhere it applies.