πŸ”₯ Launch Offer: Interview Preparation Bundle @ β‚Ή2,499 β‚Ή9,999 β€’ 75% OFF. Offer expires in πŸ”₯ Launch Offer: Interview Preparation Bundle @ β‚Ή2,499 β‚Ή9,999 β€’ 75% OFF. Offer expires in Interview Preparation Bundle @ β‚Ή2,499 β‚Ή9,999

00:00:00 Get Bundle
home java interview java-coding-traps-output-based-questions
Interview guide Java Interview 45 sections

Java coding traps (output-based questions)

Here are Top Java Coding Traps (Output-Based Questions) β€” very common in interviews πŸ”₯Each includes code + output + explanation so you can answer confidently. Java Basic Traps…

Start reading
By javainterviewprep April 12, 2026 4 min read

Here are Top Java Coding Traps (Output-Based Questions) β€” very common in interviews πŸ”₯
Each includes code + output + explanation so you can answer confidently.

Java Basic Traps

1. String Pool Trap

String a = "Java";
String b = "Java";
System.out.println(a == b);

πŸ‘‰ Output: true

πŸ” Why?

  • Both refer to same string pool object
String a = new String("Java");
String b = new String("Java");
System.out.println(a == b);

πŸ‘‰ Output: false

2. Integer Cache Trap

Integer a = 127;
Integer b = 127;
System.out.println(a == b);

πŸ‘‰ Output: true

Integer a = 128;
Integer b = 128;
System.out.println(a == b);

πŸ‘‰ Output: false

πŸ” Why?

  • Cache range: -128 to 127

3. String Immutability

String str = "Java";
str.concat(" Rocks");
System.out.println(str);

πŸ‘‰ Output: Java

Why?

  • String is immutable

4. finally Block Override

public class Test {
public static void main(String[] args) {
System.out.println(test());
} static int test() {
try {
return 1;
} finally {
return 2;
}
}
}

πŸ‘‰ Output: 2

Why?

  • finally overrides return

5. Array Reference Trap

int[] arr = {1, 2, 3};
int[] copy = arr;copy[0] = 100;System.out.println(arr[0]);

πŸ‘‰ Output: 100

Why?

  • Both reference same array

6. Method Overloading Confusion

void test(int a) { System.out.println("int"); }
void test(Integer a) { System.out.println("Integer"); }test(10);

πŸ‘‰ Output: int

7. Null Method Call Trap

String str = null;
System.out.println(str instanceof String);

πŸ‘‰ Output: false

Why?

  • instanceof handles null safely

8. Static vs Instance

class A {
static void show() { System.out.println("A"); }
}class B extends A {
static void show() { System.out.println("B"); }
}A obj = new B();
obj.show();

πŸ‘‰ Output: A

Why?

  • Static methods β†’ compile-time binding

9. List Modification Trap

List<Integer> list = Arrays.asList(1, 2, 3);
list.add(4);

πŸ‘‰ Output: UnsupportedOperationException

10. Post vs Pre Increment

int i = 5;
int result = i++ + ++i;
System.out.println(result);

πŸ‘‰ Output: 12

Step:

  • i++ β†’ 5 (i becomes 6)
  • ++i β†’ 7
    πŸ‘‰ 5 + 7 = 12

How to Answer in Interview

πŸ‘‰ Don’t just give output β€” explain:

β€œThis happens due to string pool / immutability / caching / reference behavior.”

Must-Remember Traps

String pool
Integer cache
finally override
Arrays.asList() fixed size
i++ vs ++i

🧡 MULTITHREADING TRAPS (1–10)

1. Thread start() vs run()

class MyThread extends Thread {
public void run() {
System.out.println("Thread running");
}
}public class Test {
public static void main(String[] args) {
MyThread t = new MyThread();
t.run();
System.out.println("Main thread");
}
}

πŸ‘‰ Output (always same order):

Thread running
Main thread

βœ” run() β†’ normal method (no new thread)

2. Using start()

t.start();
System.out.println("Main thread");

πŸ‘‰ Output: (order NOT guaranteed ❗)

Main thread
Thread running

OR

Thread running
Main thread

3. Two Threads Execution

Thread t1 = new Thread(() -> System.out.println("A"));
Thread t2 = new Thread(() -> System.out.println("B"));t1.start();
t2.start();

πŸ‘‰ Output:

A B  OR  B A

βœ” Order unpredictable

4. sleep() Behavior

Thread t = new Thread(() -> {
try {
Thread.sleep(1000);
System.out.println("Done");
} catch(Exception e){}
});
t.start();
System.out.println("Main");

πŸ‘‰ Output:

Main
Done

πŸ”₯ 5. join() Trap

Thread t = new Thread(() -> System.out.println("Thread"));
t.start();
t.join();
System.out.println("Main");

πŸ‘‰ Output:

Thread
Main

βœ” join() waits for thread

6. Race Condition

int count = 0;Runnable r = () -> {
for(int i=0;i<1000;i++) count++;
};Thread t1 = new Thread(r);
Thread t2 = new Thread(r);t1.start(); t2.start();
t1.join(); t2.join();System.out.println(count);

πŸ‘‰ Output: unpredictable (< 2000 ❗)

7. synchronized Fix

synchronized(this) {
count++;
}

πŸ‘‰ Output: 2000 βœ…

8. Daemon Thread

Thread t = new Thread(() -> {
while(true) {}
});
t.setDaemon(true);
t.start();System.out.println("Main ends");

πŸ‘‰ Program exits immediately

9. Deadlock (Concept)

// Thread1 locks A β†’ waits B
// Thread2 locks B β†’ waits A

πŸ‘‰ Output: program hangs ❗

10. Callable vs Runnable

Callable<Integer> task = () -> 10;
FutureTask<Integer> ft = new FutureTask<>(task);new Thread(ft).start();
System.out.println(ft.get());

πŸ‘‰ Output: 10

🌊 STREAM TRAPS (11–20)

11. Stream Reuse

Stream<Integer> s = Stream.of(1,2,3);
s.forEach(System.out::println);
s.forEach(System.out::println);

πŸ‘‰ Output: Exception ❗

12. Lazy Execution

Stream.of(1,2,3)
.filter(x -> {
System.out.println(x);
return true;
});

πŸ‘‰ Output: nothing ❗

βœ” No terminal operation

13. Order of Execution

Stream.of(1,2,3)
.filter(x -> {
System.out.println("Filter: " + x);
return true;
})
.map(x -> {
System.out.println("Map: " + x);
return x;
})
.forEach(System.out::println);

πŸ‘‰ Runs element by element (not full filter then map)

14. Parallel Stream Order

Stream.of(1,2,3,4)
.parallel()
.forEach(System.out::print);

πŸ‘‰ Output order unpredictable

15. forEachOrdered()

.parallel().forEachOrdered(System.out::print);

πŸ‘‰ Output: 1234 βœ…

16. findFirst() vs findAny()

Stream.of(1,2,3).parallel().findAny();

πŸ‘‰ Output unpredictable

17. limit() with parallel

Stream.iterate(1, x->x+1)
.parallel()
.limit(5)
.forEach(System.out::print);

πŸ‘‰ Order not guaranteed

18. Side Effects

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

πŸ‘‰ Works, but bad practice ❗

19. distinct() behavior

Stream.of(1,1,2,2,3)
.distinct()
.forEach(System.out::print);

πŸ‘‰ Output: 123

20. map() vs flatMap()

List<List<Integer>> list = Arrays.asList(
Arrays.asList(1,2),
Arrays.asList(3,4)
);list.stream()
.flatMap(List::stream)
.forEach(System.out::print);

πŸ‘‰ Output: 1234

Final Interview Strategy

Focus on explaining:

  • Thread execution order
  • Race conditions
  • Stream laziness
  • Parallel stream behavior

Pro Tip

If stuck, say:

β€œOutput may vary due to thread scheduling / parallel execution.”