Interview
"What Does This Print?" — Java Puzzles (35)
Work each puzzle on paper first, then check the answer.
Focus: operator precedence, autoboxing, short-circuit, String pool, generics erasure.
Runnable checks: compile snippets in jshell or small main methods.
Puzzles
Puzzle 1
1System.out.println(1 + 2 + "3");2System.out.println("1" + 2 + 3);Answer: 33 then 123.
Why: Left-to-right: 1+2=3, then string concat "3". Second line all string concat after first "1".
Puzzle 2
1Integer a = 127;2Integer b = 127;3Integer c = 128;4Integer d = 128;5System.out.println(a == b);6System.out.println(c == d);Answer: true then false.
Why: Integer cache -128..127; 128 is new objects.
Puzzle 3
1String s = "hello";2s += " world";3String t = "hello world";4System.out.println(s == t);Answer: false (usually).
Why: += creates new String on heap; t is literal pool "hello world" unless interned.
Puzzle 4
1System.out.println(Math.round(2.5));2System.out.println(Math.round(-2.5));Answer: 2 and -2.
Why: round uses half-up toward positive infinity for .5 cases on doubles (not bankers rounding).
Puzzle 5
1int i = 0;2System.out.println(i++ + ++i);Answer: 2.
Why: Postfix 0 + prefix 2 (i becomes 2 before second use).
Puzzle 6
1Boolean b1 = true;2Boolean b2 = true;3System.out.println(b1 == b2);Answer: true.
Why: Boolean.TRUE cached instances for autoboxed true.
Puzzle 7
1try {2 System.out.println("try");3 return;4} finally {5 System.out.println("finally");6}Answer: Prints try then finally.
Why: finally runs before method actually returns.
Puzzle 8
1List<String> list = Arrays.asList("a", "b");2list.add("c");Answer: UnsupportedOperationException at runtime.
Why: Arrays.asList returns fixed-size list.
Puzzle 9
1Map<String, Integer> m = new HashMap<>();2m.put("a", 1);3m.put("a", 2);4System.out.println(m.size());5System.out.println(m.get("a"));Answer: 1 and 2.
Why: Same key replaces value; size unchanged.
Puzzle 10
1System.out.println(null + true);Answer: Compile error.
Why: null + boolean — string concatenation only if one operand is String; true is boolean.
Puzzle 11
1Object o = true ? Integer.valueOf(1) : "x";2System.out.println(o.getClass().getName());Answer: java.lang.Integer.
Why: Ternary requires compatible types; both branches become Object; first branch is Integer.
Puzzle 12
1int[] a = {1, 2};2int[] b = a;3b[0] = 9;4System.out.println(a[0]);Answer: 9.
Why: Arrays are objects; reference copy shares same array.
Puzzle 13
1StringBuilder sb = new StringBuilder("ab");2sb.append("c").delete(0, 1);3System.out.println(sb.toString());Answer: bc.
Why: delete removes index 0 char a.
Puzzle 14
1System.out.println(0.1 + 0.2 == 0.3);Answer: false.
Why: Floating-point representation error.
Puzzle 15
1List<Integer> list = List.of(1, 2, 3);2list.set(0, 9);Answer: UnsupportedOperationException.
Why: List.of is immutable.
Puzzle 16
1Stream.of(1, 2, 3).peek(System.out::println).count();Answer: Prints 1, 2, 3 then returns 3.
Why: count is terminal; peek runs on pipeline execution.
Puzzle 17
1Optional<String> o = Optional.ofNullable(null);2System.out.println(o.orElse("x"));Answer: x.
Why: Empty optional uses orElse default.
Puzzle 18
1class Parent { String greet() { return "P"; } }2class Child extends Parent { String greet() { return "C"; } }3Parent p = new Child();4System.out.println(p.greet());Answer: C.
Why: Virtual method dispatch on runtime type Child.
Puzzle 19
1System.out.println("A" + 1 + 2);2System.out.println(1 + 2 + "A");Answer: A12 then 3A.
Why: String promotion left-to-right.
Puzzle 20
1int x = 5;2System.out.println(x > 2 ? x < 4 ? "a" : "b" : "c");Answer: b.
Why: Ternary right-associative: x>2 ? (x<4?"a":"b") : "c" → true branch, x<4 false → b.
Puzzle 21
1System.out.println(new String("hi") == "hi");2System.out.println(new String("hi").intern() == "hi");Answer: false then true.
Why: new String new object; intern() enters pool.
Puzzle 22
1List raw = new ArrayList<>();2raw.add(1);3raw.add("two");4System.out.println(raw.size());Answer: 2.
Why: Raw list no compile-time type check; both added.
Puzzle 23
1List<?> list = List.of(1, 2);2list.add(3);Answer: Compile error.
Why: ? unbounded wildcard — add not allowed (except null).
Puzzle 24
1int i = 1;2switch (i) {3 case 1: System.out.print("1 ");4 case 2: System.out.print("2 ");5 default: System.out.print("d ");6}Answer: 1 2 d (with classic fall-through).
Why: No break — falls through cases.
Puzzle 25
1record R(int x) {}2R r1 = new R(1);3R r2 = new R(1);4System.out.println(r1.equals(r2));5System.out.println(r1 == r2);Answer: true then false.
Why: Record value equality; distinct objects.
Puzzle 26
1Thread t = new Thread(() -> System.out.println("run"));2t.start();3t.start();Answer: Second start() throws IllegalThreadException.
Why: Thread already started.
Puzzle 27
1String s = null;2System.out.println(s instanceof String);Answer: false.
Why: instanceof false for null; no NPE.
Puzzle 28
1System.out.println(10 / 0.0);2System.out.println(10.0 / 0.0);Answer: Infinity then Infinity.
Why: Floating divide by zero → Infinity (not exception).
Puzzle 29
1char c = 'A';2System.out.println(c + 1);3System.out.println(c + 1 + "");Answer: 66 then 66 as string? Wait: c+1 int 66; c+1+"" → 66 string because 66+"" string concat.
Answer: 66 then "66".
Puzzle 30
1Map map = new HashMap();2map.put(1, "one");3map.put(1, "ONE");4System.out.println(map.get(1));Answer: ONE.
Why: Integer key 1; second put replaces.
Puzzle 31
1System.out.println(~1);Answer: -2.
Why: Bitwise NOT of 1 (...0001) → ...1110 = -2 in two's complement.
Puzzle 32
1boolean flag = false;2if (flag = true) {3 System.out.println("yes");4}Answer: Prints yes (compiles — assignment in condition).
Why: flag = true assigns and evaluates true. Style smell, not puzzle error.
Puzzle 33
1Deque<Integer> d = new ArrayDeque<>();2d.push(1);3d.push(2);4System.out.println(d.pop());Answer: 2.
Why: Stack LIFO — push 1 then 2, pop returns 2.
Puzzle 34
1int[][] m = {{1, 2}, {3}};2System.out.println(m[1][0]);Answer: 3.
Puzzle 35
1System.out.println("Java".substring(1, 3));Answer: av.
Why: substring(begin, end) end exclusive; indices 1..2.
Study Tips
- Trace types on paper — promotion rules bite often.
- Distinguish
==(reference/primitive) vsequals(value). - Know which APIs return views vs copies vs immutable instances.
- Classic switch falls through unless
->orbreak. - Re-run puzzling snippets in
jshell— muscle memory beats memorizing answers.