Menu

Earn Premium with Referrals

Invite your friends and earn Premium rewards through our referral program.

See how it works and start inviting friends.

Output Questions - Part 1
JAVA

Output Questions - Part 1

Practice 15 Java predict-the-output questions covering common patterns in campus assessments and technical interviews.

1. What is the value printed by this code?

public class Test {
    public static void main(String[] args) {
        int i = 0;
        i = i++ + i;
        System.out.println(i);
    }
}

Output: 1

Java evaluates the operands of + left to right. The left operand is i++: it yields the old value 0 and increments i to 1. Then the right operand i is evaluated — and it is now 1. So the sum is 0 + 1 = 1, which is assigned back to i.

The common trap is assuming both operands see i = 0. They don’t: the post-increment inside the left operand has already bumped i before the right operand is read. This is one of the most repeated increment/output questions in campus assessments.

2. What is printed by this pre/post-increment sequence?

public class Test {
    public static void main(String[] args) {
        int x = 5;

        System.out.println(x++);
        System.out.println(++x);
        System.out.println(x);
    }
}

Output:

5
7
7

x++ prints the current value 5, then increments x to 6. ++x increments x to 7 first, then prints 7. The final x is 7. The distinction is when the side effect happens: post-increment returns the old value, pre-increment returns the new value.

3. Which results does this string comparison print?

public class Test {
    public static void main(String[] args) {
        String a = "Java";
        String b = "Java";
        String c = new String("Java");

        System.out.println(a == b);
        System.out.println(a == c);
        System.out.println(a.equals(c));
    }
}

Output:

true
false
true

a and b are compile-time literals, so both refer to the same object in the string pool — == (reference comparison) is true. c is a distinct heap object via new, so a == c is false. equals() compares content, so a.equals(c) is true. The == vs .equals() trap is a staple of Java output questions.

4. What is the output of these string-concatenation lines?

public class Test {
    public static void main(String[] args) {
        System.out.println(10 + 20 + "Java");
        System.out.println("Java" + 10 + 20);
    }
}

Output:

30Java
Java1020

+ is evaluated left to right. In the first line, both operands of the first + are int, so 10 + 20 = 30, then 30 + "Java" concatenates to "30Java". In the second line, "Java" + 10 is already string concatenation, giving "Java10", then "Java10" + 20 gives "Java1020". The numeric addition only happens when both operands are numeric.

5. What does this character-addition expression print?

public class Test {
    public static void main(String[] args) {
        System.out.println('j' + 'a' + 'v' + 'a');
    }
}

Output: 418

Characters are char, which promotes to int in arithmetic. 'j' = 106, 'a' = 97, 'v' = 118, 'a' = 97. So 106 + 97 + 118 + 97 = 418. If even one operand were a String, the whole expression would concatenate instead — that boundary is the entire point of the question.

6. Why do these two lines print different results?

public class Test {
    public static void main(String[] args) {
        System.out.println('A' + 1);
        System.out.println("A" + 1);
    }
}

Output:

66
A1

'A' is a char; in 'A' + 1 both operands are numeric, so it promotes to int and adds: 65 + 1 = 66. "A" + 1 has a String operand, so it concatenates to "A1". Numeric addition versus string concatenation, decided purely by operand types.

7. What does this Integer-caching question print?

public class Test {
    public static void main(String[] args) {
        Integer a = 100;
        Integer b = 100;

        Integer c = 400;
        Integer d = 400;

        System.out.println(a == b);
        System.out.println(c == d);
    }
}

Output:

true
false

Autoboxing caches Integer values in the range -128 to 127, reusing the same object. 100 falls in that range, so a and b are the same object and == is true. 400 is outside the cache, so c and d are separate objects and == is false. Use equals() for value comparison.

8. == vs .equals() on boxed Integers

public class Test {
    public static void main(String[] args) {
        Integer a = 400;
        Integer b = 400;

        System.out.println(a == b);
        System.out.println(a.equals(b));
    }
}

Output:

false
true

Both are 400, outside the cached range, so autoboxing creates two distinct objects — == compares references and is false. equals() compares the integer values and is true. A companion to the caching question: always compare wrappers with equals().

9. Which overload is chosen when passing null?

class Test {

    static void show(Object o) {
        System.out.println("Object");
    }

    static void show(String s) {
        System.out.println("String");
    }

    public static void main(String[] args) {
        show(null);
    }
}

Output: String

null is compatible with both Object and String. Java picks the most specific applicable type, and String is more specific than Object, so show(String) is invoked. If both overloads were unrelated types at the same level, null would be ambiguous and the code would not compile.

10. How does the compiler resolve these overloaded calls?

class Test {

    static void show(int x) {
        System.out.println("int");
    }

    static void show(double x) {
        System.out.println("double");
    }

    public static void main(String[] args) {
        show(10);
        show(10.5);
    }
}

Output:

int
double

10 is an int, matching show(int) exactly. 10.5 is a double, matching show(double) exactly. Overload resolution happens at compile time based on the argument’s static type — no runtime dispatch here.

11. Which overload does a byte argument promote to?

class Test {

    static void show(int x) {
        System.out.println("int");
    }

    static void show(long x) {
        System.out.println("long");
    }

    public static void main(String[] args) {
        byte b = 10;
        show(b);
    }
}

Output: int

byte cannot widen directly to char, but it can widen to int. When the compiler considers overloads, int is reachable via widening and is a closer match than long, so show(int) is chosen. Overload resolution prefers the smallest widening step.

12. Which sound() runs for Animal a = new Dog()?

class Animal {
    void sound() {
        System.out.println("Animal");
    }
}

class Dog extends Animal {
    void sound() {
        System.out.println("Dog");
    }
}

public class Test {
    public static void main(String[] args) {
        Animal a = new Dog();
        a.sound();
    }
}

Output: Dog

The reference type is Animal, but instance method dispatch uses the actual object type at runtime (dynamic dispatch). The object is a Dog, so the overridden sound() in Dog executes. Compile-time type controls what you can call; runtime type controls which implementation runs.

13. Which x is printed when a field is “overridden”?

class Parent {
    int x = 10;
}

class Child extends Parent {
    int x = 20;
}

public class Test {
    public static void main(String[] args) {
        Parent p = new Child();

        System.out.println(p.x);
    }
}

Output: 10

Fields are hidden, not overridden — field access is resolved at compile time by the reference type. The reference is Parent, so p.x reads Parent.x = 10. Contrast with methods, which use dynamic dispatch. This is the exact trap the question tests.

14. In what order do constructors run?

class A {
    A() {
        System.out.println("A");
    }
}

class B extends A {
    B() {
        System.out.println("B");
    }
}

public class Test {
    public static void main(String[] args) {
        new B();
    }
}

Output:

A
B

Every subclass constructor implicitly calls super() first (if no this(...)/super(...) call is written), so the parent constructor runs before the child constructor body. Construction is top-down: A first, then B.

15. Which runs first — the static block or main?

class Test {

    static {
        System.out.println("Static");
    }

    public static void main(String[] args) {
        System.out.println("Main");
    }
}

Output:

Static
Main

Static initializer blocks execute when the class is loaded, which happens before main runs. Order of execution: static blocks (and static variable initializers, in source order) run at class-load time, then main().

My Private Notes

Notes are auto-saved locally to this device.