Interview replay

Full round replay — pass-by-value

10 minjunior06 yrs

Timed verbal replay with pass/fail criteria per follow-up.

How to run this

Answer out loud, timed. Do not read the entry first. Then compare against "The Answer" and "Interviewer's Next Move" and mark yourself.

The opener

Is Java pass-by-value or pass-by-reference?

Budget: 30 seconds. Going long here is itself a fail signal.

Follow-ups

  1. 1. “So objects are passed by reference?

    Testing: The single most common wrong answer, and it sounds reasonable.

    Scoring

    Pass: No. The reference is passed by value — the method gets its own copy of it. Both copies point at the same object, so mutation is visible, but the method cannot repoint the caller's variable.

    Fail: Yes, or 'primitives by value, objects by reference'.

  2. 2. “Prove it.

    Testing: Whether they know the experiment, not just the slogan.

    Scoring

    Pass: A swap method taking two objects. It fails, exactly as it does for primitives. Pass-by-reference would make the parameters alias the caller's variables — that is what C++ T& does, and Java has no syntax for it.

    Fail: Only offers the primitive swap, which proves nothing about objects.

  3. 3. “Then why did my ArrayList change after I passed it into a method?

    Testing: Can they separate following the reference from moving it?

    Scoring

    Pass: The method followed the reference and mutated the shared object. list.add(x) is visible; list = new ArrayList<>() inside the method is not.

    Fail: Treats it as evidence for pass-by-reference.

  4. 4. “Are arrays special?

    Testing: A common carve-out people invent.

    Scoring

    Pass: No — arrays are objects, identical rules. arr[0] = 99 is visible, arr = new int[]{...} is not.

    Fail: Says arrays are passed by reference.

  5. 5. “Why do String and Integer seem to behave differently?

    Testing: Immutability, not different parameter semantics.

    Scoring

    Pass: They are immutable, so there is no mutation to observe — only reassignment, which is never visible. Autoboxing hides that an object was involved at all.

    Fail: Believes primitives-vs-wrappers changes how arguments are passed.

  6. 6. “What does setting a parameter to null inside a method do?

    Testing: Whether the rule generalises or was memorised for one case.

    Scoring

    Pass: Nothing the caller can see. It nulls the method's copy of the reference; the caller still holds theirs.

    Fail: Expects the caller's variable to become null, or the object to be freed.

  7. 7. “How would you write a method that swaps two values?

    Testing: Does the constraint change their design, or do they fight it?

    Scoring

    Pass: Return both — a record, an array, or mutate a container the caller owns. Wanting output parameters usually means the method should return something.

    Fail: Reaches for a wrapper class purely to fake output parameters, with no acknowledgement that it is a workaround.

  8. 8. “A method must not modify the list I pass it. How do you guarantee that?

    Testing: Mechanism versus good intentions — the senior-level answer.

    Scoring

    Pass: Pass something immutable — List.copyOf — or have the method copy before mutating, and type the parameter as what it may do. Documentation is not enforcement. Note it protects the list, not mutable elements inside it.

    Fail: 'Write it in the Javadoc' or 'use final on the parameter' — final stops reassignment, not mutation.

Score yourself

8/8 — solid, and you can defend it under pushback 6-7 — you have the rule; sharpen the proof and the design answers 4-5 — you know the slogan, not the mechanism; redo the Challenge 0-3 — run the Warm-up, then reread "Understand It"

← Back to Is Java pass-by-value or pass-by-reference?