Cosmic Module
J
Qubits of DPK
March 14, 2026
Core Java
Purpose: These are the exact mistakes MAANG interviewers look for. Read before every interview.
Data Types & Conversion Traps
java
QUBITS OF DPK
1// Trap 1 — long without L suffix
2long big = 10000000000; // ❌ compile error
3long big = 10000000000L; // ✅
4
5// Trap 2 — float without f suffix
6float f = 3.14; // ❌ 3.14 is double
7float f = 3.14f; // ✅
8
9// Trap 3 — Casting truncates, NOT rounds
10double d = 9.9;
11int i = (int) d; // 9, NOT 10!
12
13// Trap 4 — Overflow on narrow cast
14int n = 130;
15byte b = (byte) n; // -126 (bits wrap!)
16
17// Trap 5 — byte + byte = int
18byte a = 10, b = 20;
19byte c = a + b; // ❌ compile error! Result is int
20int c = a + b; // ✅
21
22// Trap 6 — Integer division before assignment
23double result = 10 / 3; // 3.0 NOT 3.33!
24double result = 10.0 / 3; // 3.33 ✅Literals Traps
java
QUBITS OF DPK
1// Trap 1 — Underscore position
2int x = _100; // ❌ start
3int x = 100_; // ❌ end
4int x = 1_00; // ✅
5
6// Trap 2 — Boolean case
7boolean b = True; // ❌ must be lowercase
8boolean b = true; // ✅
9
10// Trap 3 — null on primitive
11int x = null; // ❌ only for reference types️ Operator Traps
java
QUBITS OF DPK
1// Trap 1 — Post vs pre increment WITH carry
2int x = 5;
3System.out.println(x++); // 5 (use then increment)
4System.out.println(++x); // 7 (x is now 6, then increment to 7)
5
6// Trap 2 — == on Strings
7"hello" == "hello" // unreliable!
8"hello".equals("hello") // ✅ always correct
9
10// Trap 3 — Assignment vs comparison
11if (x = 5) { } // ❌ assignment inside if!
12if (x == 5) { } // ✅
13
14// Trap 4 — Short circuit order for null safety
15if (str.equals("hi") && str != null) // ❌ NPE risk!
16if (str != null && str.equals("hi")) // ✅ safe order
17
18// Trap 5 — NaN never equals itself
19Double.NaN == Double.NaN // false!
20Double.isNaN(value) // ✅ correct check
21
22// Trap 6 — Integer division
2310 / 3 = 3 (not 3.33!)
24(double) 10 / 3 = 3.33 ✅Control Flow Traps
java
QUBITS OF DPK
1// Trap 1 — Semicolon after for loop (VERY common!)
2for (int i = 0; i < 5; i++); // empty body!
3{ System.out.println("hi"); } // runs ONCE only
4
5// Trap 2 — Switch fall-through
6switch (x) {
7 case 1: System.out.println("one"); // no break!
8 case 2: System.out.println("two"); // ALSO runs if x==1!
9}
10
11// Trap 3 — Dangling else
12if (a > 0)
13 if (a > 5) System.out.println("big");
14else System.out.println("neg"); // belongs to inner if!
15
16// Trap 4 — float/double not allowed in switch
17switch (3.14) { } // ❌ compile error
18
19// Trap 5 — break only exits INNERMOST loop
20for (outer) {
21 for (inner) {
22 break; // exits inner only!
23 }
24}Array Traps
java
QUBITS OF DPK
1// Trap 1 — length is property not method
2arr.length // ✅
3arr.length() // ❌ compile error
4
5// Trap 2 — Off by one
6int[] arr = new int[5];
7arr[5] = 10; // ❌ ArrayIndexOutOfBoundsException (0-4 only!)
8
9// Trap 3 — Reference copy shares heap
10int[] a = {1,2,3};
11int[] b = a; // same array!
12b[0] = 99;
13System.out.println(a[0]); // 99!
14// Fix: Arrays.copyOf(a, a.length)
15
16// Trap 4 — for-each cannot modify primitives
17for (int x : arr) { x = 0; } // original unchanged!
18
19// Trap 5 — Object array default is null
20String[] names = new String[3];
21names[0].length(); // ❌ NPE! Must initialize first
22
23// Trap 6 — Jagged array uninitialized rows
24int[][] j = new int[3][];
25j[0].length; // ❌ NPE! Row not initializedString Traps
java
QUBITS OF DPK
1// Trap 1 — concat without reassign
2name.concat(" Kumar"); // result lost!
3name = name.concat(" Kumar"); // ✅
4
5// Trap 2 — == on Strings
6String a = "Hi", b = "Hi"; a == b // true (pool)
7String c = new String("Hi"); a == c // false (heap)
8// ALWAYS use .equals() for String comparison!
9
10// Trap 3 — StringBuilder not thread safe
11// Use StringBuffer in multithreaded context
12
13// Trap 4 — toString() mandatory for StringBuilder
14StringBuilder sb = new StringBuilder("Hi");
15String s = sb; // ❌ compile error
16String s = sb.toString(); // ✅
17
18// Trap 5 — String concatenation in loop
19for (int i=0; i<10000; i++) result += i; // 10000 objects!
20// Fix: StringBuilder.append()Static Traps
java
QUBITS OF DPK
1// Trap 1 — Static cannot access instance directly
2static void show() {
3 System.out.println(instanceVar); // ❌ no object!
4}
5
6// Trap 2 — Static block runs before main
7class Main {
8 static { System.out.println("static"); } // runs FIRST
9 public static void main(String[] a) { System.out.println("main"); }
10}
11// Output: static then main
12
13// Trap 3 — this not allowed in static
14static void m() { this.x = 5; } // ❌️ OOP Traps
java
QUBITS OF DPK
1// Trap 1 — NullPointerException
2Car car = null;
3car.accelerate(); // ❌ NPE
4
5// Trap 2 — Reference sharing
6Car a = new Car(); Car b = a;
7b.speed = 100;
8System.out.println(a.speed); // 100! Same object!
9
10// Trap 3 — this() must be first in constructor
11Car() {
12 System.out.println("hi"); // ❌ must be before this()
13 this("BMW");
14}
15
16// Trap 4 — Parameterized constructor removes default
17class Car { Car(String brand) { } }
18new Car(); // ❌ no default constructor!
19
20// Trap 5 — Constructor has no return type
21void Car() { } // this is a METHOD, not a constructor!
22
23// Trap 6 — super() must be first in child constructor
24Dog() {
25 breed = "Lab"; // ❌ must come after super()
26 super("name");
27}Polymorphism Traps
java
QUBITS OF DPK
1// Trap 1 — ClassCastException
2Animal a = new Cat();
3Dog d = (Dog) a; // ❌ runtime crash!
4if (a instanceof Dog) { Dog d = (Dog) a; } // ✅
5
6// Trap 2 — Static methods NOT polymorphic
7// Static resolved at compile time, not runtime
8
9// Trap 3 — Wrapper Integer cache
10Integer a = 127, b = 127; a == b // true (cached)
11Integer x = 128, y = 128; x == y // false! Use equals()
12
13// Trap 4 — NullPointerException with unboxing
14Integer i = null;
15int x = i; // ❌ NPE on unboxing null!
16
17// Trap 5 — equals() without hashCode()
18// If you override equals(), MUST override hashCode() too!
19// HashSet/HashMap break without itInterface Traps
java
QUBITS OF DPK
1// Trap 1 — Interface variables are final
2interface I { int X = 10; }
3class C implements I { void m() { X = 20; } } // ❌
4
5// Trap 2 — Default method diamond conflict
6interface A { default void show() { } }
7interface B { default void show() { } }
8class C implements A, B {
9 // MUST override show() to resolve conflict!
10 public void show() { A.super.show(); } // ✅
11}
12
13// Trap 3 — @FunctionalInterface with 2 abstract methods
14@FunctionalInterface
15interface Wrong {
16 void m1(); void m2(); // ❌ not functional!
17}Exception Traps
java
QUBITS OF DPK
1// Trap 1 — Catching parent before child
2catch (Exception e) { } // ❌ child unreachable!
3catch (NullPointerException e) { } // never reached
4// Fix: specific FIRST, general LAST
5
6// Trap 2 — Empty catch (swallowing)
7catch (Exception e) { } // ❌ silent failure, impossible to debug!
8
9// Trap 3 — finally overriding return
10try { return 1; }
11finally { return 2; } // ⚠️ returns 2! finally can override return
12
13// Trap 4 — throw vs throws
14throw new Exception(); // actually throws
15throws Exception // only declaresThreading Traps
java
QUBITS OF DPK
1// Trap 1 — run() vs start()
2t1.run(); // ❌ runs in current thread!
3t1.start(); // ✅ creates new thread
4
5// Trap 2 — count++ not atomic
6count++; // Read + Increment + Write = 3 steps = race condition!
7// Fix: synchronized or AtomicInteger
8
9// Trap 3 — Starting thread twice
10t1.start(); t1.start(); // ❌ IllegalThreadStateException!Collections Traps
java
QUBITS OF DPK
1// Trap 1 — ConcurrentModificationException
2for (String s : list) { list.remove(s); } // ❌!
3list.removeIf(s -> s.startsWith("D")); // ✅
4
5// Trap 2 — HashMap null key
6map.put(null, "v"); // ✅ HashMap allows
7new Hashtable().put(null, "v"); // ❌ NPE!
8
9// Trap 3 — Stream reuse
10Stream<Integer> s = list.stream();
11s.forEach(System.out::println);
12s.count(); // ❌ IllegalStateException! Already consumed
13
14// Trap 4 — HashSet with custom objects
15// Must override equals() AND hashCode() or duplicates sneak in!
16
17// Trap 5 — TreeMap null key
18new TreeMap().put(null, "v"); // ❌ NullPointerException!