"What does this code print?" questions test whether you know how Java really behaves, not just its syntax. Each question below shows the code, the output and the rule behind it: string pooling, Integer caching, finally overriding return, overload resolution, initialisation order and Java 8 stream gotchas.

Java Tricky Output Questions

// Q1: What is the output?
int i = 0;
System.out.println(i++ + ++i); // 0 + 2 = 2 (i becomes 2)

// Q2: What is the output?
Integer a = 127, b = 127;
Integer c = 128, d = 128;
System.out.println(a == b);   // true (Integer cache)
System.out.println(c == d);   // false (outside cache)

// Q3: What happens here?
String s1 = new String("hello");
String s2 = new String("hello");
System.out.println(s1 == s2);      // false
System.out.println(s1.equals(s2)); // true

// Q4: What is the output?
System.out.println(1 + 2 + "3");    // "33" (left to right: 3+"3")
System.out.println("1" + 2 + 3);    // "123" (String + int + int)
System.out.println("1" + (2 + 3));  // "15" (parens first: 1+"5")

// Q5: Does this compile? What happens?
int[] arr = new int[5];
System.out.println(arr[5]); // ArrayIndexOutOfBoundsException

// Q6: What is the output?
public static int tricky() {
    try {
        return 1;
    } finally {
        return 2; // finally OVERRIDES try return!
    }
}
System.out.println(tricky()); // 2

// Q7: What is the output?
List list = new ArrayList();
list.add("Hello");
list.add(42);
for (Object o : list) System.out.println(o); // Hello \n 42 (no error)

// Q8: Guess the output
double d = 1.0 / 0;
System.out.println(d);           // Infinity (not exception!)
System.out.println(0.0 / 0.0);  // NaN
System.out.println(d == d);      // true
double nan = Double.NaN;
System.out.println(nan == nan);  // FALSE! NaN != NaN always

// Q9: What is the output?
String s = null;
System.out.println("Value: " + s); // "Value: null" (not NPE!)
System.out.println(s + " world"); // "null world"
s.length(); // NullPointerException HERE

// Q10: What is printed?
for (int x = 0; x < 3; x++) {
    int y = x;
    new Thread(() -> System.out.println(y)).start();
}
// Prints 0, 1, 2 in some order (y is effectively final, captured correctly)

// Q11: What is the output?
int x = 5;
int y = x++ + ++x; // 5 + 7 = 12 (x becomes 7)
System.out.println(x + " " + y); // 7 12

// Q12: Tricky equals
Object obj1 = true ? new Integer(1) : new Double(2.0);
System.out.println(obj1); // 1.0 !! (ternary numeric promotion)

// Q13: Static vs instance
class Parent { static String x = "Parent"; }
class Child extends Parent { static String x = "Child"; }
Parent p = new Child();
System.out.println(p.x); // "Parent" (static = compile-time, ref type)

// Q14: Autoboxing trap
Integer i1 = 1000;
Integer i2 = 1000;
System.out.println(i1 == i2);     // false
System.out.println(i1 < i2);      // false (unboxed comparison)
System.out.println(i1 <= i2);     // true (unboxed: 1000 <= 1000)

// Q15: String switch
String s = null;
switch (s) { case "a": break; } // NullPointerException!

// Q16: Interface default conflict
interface A { default void greet() { System.out.println("A"); } }
interface B { default void greet() { System.out.println("B"); } }
class C implements A, B {
    public void greet() { A.super.greet(); } // must override!
}
Advertisement

Java 8 Tricky Interview Questions

What happens if default method in interface conflicts with method from Object class?

Interface cannot have a default method that overrides Object's methods (equals, hashCode, toString). This is explicitly forbidden by the Java spec to avoid confusion. If you try, you get a compile error.

Can a lambda expression throw a checked exception?

Only if the functional interface's abstract method declares that checked exception in its throws clause. Runnable.run() declares nothing → lambda can't throw checked exceptions. To throw checked exceptions from lambdas, use a custom functional interface or wrap in try-catch.

What is the difference between Stream.of() and Arrays.stream()?

Stream.of(arr) where arr is an int[] creates Stream<int[]> (one element — the array itself!). Arrays.stream(arr) creates IntStream from the array elements. For object arrays both work similarly. Always use Arrays.stream() for primitive arrays.

What is flatMap used for in streams?

flatMap is for 1-to-many transformations. When each element maps to a Stream/collection, flatMap flattens them into one stream. Without flatMap: Stream<Stream<String>>. With flatMap: Stream<String>. Example: List<List<String>> → Stream<String>, or getting all words from multiple sentences.