LearnFrenzy
Sign in Take a free test

Java Programming interview · Set 13 of 13

Java Tricky Questions – Collections, Strings and Exceptions

  • 6 follow-up questions
  • 4 key points
  • Code examples
Test yourself

Real bugs disguised as puzzles

These traps come from collection APIs, string identity, exception flow and boxing. Explain the API contract or language rule, then the safe alternative.

  • Collectionsremove(int), asList, mutated keys, iteration.
  • Strings & boxingPooling, == and Integer caching.
  • Flowfinally and return values.

These tricky questions come from real bugs in Java code bases. Each one shows the code, the surprising result and the rule — the kind of understanding that separates experienced developers in interviews.

1. What is printed? (List.remove with an int)

[1, 2]. remove(2) calls remove(int index), removing the element at index 2 (the value 3), not the value 2. To remove the value, call remove(Integer.valueOf(2)).

List<Integer> list = new ArrayList<>(List.of(1, 2, 3));
list.remove(2);
System.out.println(list);

2. What happens? (Arrays.asList and add)

UnsupportedOperationException. Arrays.asList returns a fixed-size list backed by the array: you can set elements but not add or remove. Wrap it in new ArrayList<>(...) for a growable list.

List<String> names = Arrays.asList("Asha", "Ravi");
names.add("Meera");

3. What is printed? (HashSet with a mutated key)

false. Changing a field used in hashCode after insertion moves the object's hash, so the set looks in the wrong bucket. Never mutate keys stored in hash-based collections.

class Point { int x; Point(int x) { this.x = x; }
    public int hashCode() { return x; }
    public boolean equals(Object o) { return o instanceof Point p && p.x == x; } }

Set<Point> set = new HashSet<>();
Point p = new Point(1);
set.add(p);
p.x = 2;
System.out.println(set.contains(p));

4. What happens? (removing inside for-each)

ConcurrentModificationException (usually). The for-each loop uses an iterator that detects structural changes. Use removeIf or Iterator.remove().

List<String> items = new ArrayList<>(List.of("a", "b", "c"));
for (String s : items) {
    if (s.equals("a")) items.remove(s);
}

5. What is printed? (String concatenation in a loop and ==)

false. The concatenation happens at runtime and creates a new String object; == compares references. Compile-time constants ("ja" + "va") would be pooled and print true.

String a = "java";
String part = "ja";
String b = part + "va";
System.out.println(a == b);

6. What is returned? (finally modifying a returned variable)

10. The value to return is evaluated (10) before finally runs; changing the local variable in finally does not affect it. (Returning from finally would.)

static int value() {
    int x = 10;
    try {
        return x;
    } finally {
        x = 20;
    }
}

7. What is printed? (HashMap null key and getOrDefault)

first then null. HashMap allows one null key, and getOrDefault returns the default only when the key is absent — a key mapped to null returns null.

Map<String, String> map = new HashMap<>();
map.put(null, "first");
map.put("k", null);
System.out.println(map.get(null));
System.out.println(map.getOrDefault("k", "default"));

8. What is printed? (Integer equality inside a collection)

true then false. contains uses equals (value comparison), but comparing two boxed Integers above 127 with == compares references.

List<Integer> nums = List.of(1000);
System.out.println(nums.contains(1000));
Integer a = 1000, b = 1000;
System.out.println(a == b);

Remember

Key points

  • remove(int) vs remove(Object)

    Box the value to remove by value.

  • asList is fixed-size

    Wrap in ArrayList to grow.

  • Do not mutate hash keys

    Use immutable keys.

  • equals() for objects

    Never == for Strings or wrappers.

Interview drill

Follow-up questions

What the interviewer asks after your first answer. Turn on practice mode, answer each one aloud, then reveal.

  1. 1 Is List.of(1, 2).contains(null) allowed? Intermediate

    It throws NullPointerException — immutable List.of collections reject null queries and elements.

  2. 2 What does new ArrayList<>(10).size() return? Basic

    0 — the argument is initial capacity, not size.

  3. 3 Does TreeMap allow a null key? Intermediate

    No with natural ordering (NullPointerException); only with a comparator that handles null.

  4. 4 What does "abc".equals(null) return? Basic

    false — equals must return false for null, never throw.

  5. 5 What does Collections.unmodifiableList protect against? Intermediate

    Changes through that view only; changes to the underlying list still show through it. Use List.copyOf for a true snapshot.

  6. 6 Why does catching Exception before IOException fail to compile? Basic

    The IOException catch would be unreachable; catch more specific exceptions first.

Preparing with friends?

Share this page with your study group or placement batch.

Search LearnFrenzy

Esc

Quick links