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 2
JAVA

Output Questions - Part 2

Practice 15 more Java output questions covering static members, constructors, exceptions, control flow, and String behavior.

16. In what order do the static block, main, and a constructor run?

class Test {

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

    Test() {
        System.out.println("Constructor");
    }

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

Output:

Static
Main
Constructor

The static block runs at class-load time, before main executes. main prints Main, then new Test() invokes the constructor, printing Constructor. Static init happens once, at load, regardless of how many objects are created.

17. How many increments does a static counter see?

class Test {

    static int count = 0;

    Test() {
        count++;
    }

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

        System.out.println(count);
    }
}

Output: 3

static variables belong to the class, not to any instance — all objects share one copy. Each of the three constructor calls increments the same count, ending at 3. Had count been non-static, each object would have had its own copy and the print would have been 0.

18. Which constructor runs first with this(10)?

class Test {

    Test() {
        this(10);
        System.out.println("Default");
    }

    Test(int x) {
        System.out.println("Parameterized");
    }

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

Output:

Parameterized
Default

this(10) must be the first statement of a constructor and delegates to the parameterized constructor, which prints Parameterized and returns; then the rest of the no-arg constructor runs, printing Default. Execution is delegated-constructor-first.

19. What happens with an explicit super() call?

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

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

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

Output:

A
B

The explicit super() invokes the parent constructor before the child body continues. Whether implicit or explicit, the parent constructor always completes before the child constructor body runs.

20. Does finally run after a normal try?

public class Test {
    public static void main(String[] args) {
        try {
            System.out.println("Try");
        } finally {
            System.out.println("Finally");
        }
    }
}

Output:

Try
Finally

In the normal (no-exception) path, finally runs immediately after the try block. finally executes whether the try block completes normally, throws, or a return is pending.

21. When does finally run relative to a return?

class Test {

    static int getValue() {
        try {
            return 10;
        } finally {
            System.out.println("Finally");
        }
    }

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

Output:

Finally
10

When return 10 is reached, the finally block executes before the method actually returns the value. So Finally is printed first, then the pending return value 10 is delivered to the caller’s println.

22. What wins — return in try or return in finally? ⚠️

class Test {

    static int getValue() {
        try {
            return 10;
        } finally {
            return 20;
        }
    }

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

Output: 20

A return inside finally overrides any pending return from try (or catch). The 10 is discarded and 20 becomes the method’s result. The compiler even warns that the try return is unreachable. Interview tip: never return from finally unless you intend to swallow the original result.

23. What does continue skip?

public class Test {
    public static void main(String[] args) {

        for (int i = 1; i <= 5; i++) {

            if (i == 3)
                continue;

            System.out.print(i + " ");
        }
    }
}

Output: 1 2 4 5

When i == 3, continue jumps to the next iteration without executing the print. So 3 is skipped; the others print in order.

24. What does break do to this loop?

public class Test {
    public static void main(String[] args) {

        for (int i = 1; i <= 5; i++) {

            if (i == 3)
                break;

            System.out.print(i + " ");
        }
    }
}

Output: 1 2

break exits the loop entirely when i == 3. 1 and 2 print; once the loop hits 3 it terminates, so 3, 4, 5 never print.

25. What happens when a switch has no break?

public class Test {
    public static void main(String[] args) {

        int x = 2;

        switch (x) {
            case 1:
                System.out.println("One");

            case 2:
                System.out.println("Two");

            case 3:
                System.out.println("Three");

            default:
                System.out.println("Default");
        }
    }
}

Output:

Two
Three
Default

x = 2 matches case 2. With no break, execution falls through to case 3 and default, printing all three. Fall-through only happens in the direction from the matched case downward; case 1 is not reached.

26. Does a do-while body run when the condition is false?

public class Test {
    public static void main(String[] args) {

        int i = 10;

        do {
            System.out.println(i);
            i++;
        } while (i < 5);
    }
}

Output: 10

do-while checks the condition after the body, so the body always executes at least once. 10 prints, i becomes 11, then 11 < 5 is false and the loop ends.

27. What does short-circuit && print?

public class Test {
    public static void main(String[] args) {

        int x = 10;

        if (x > 5 && ++x > 10) {
            System.out.println(x);
        }

        System.out.println(x);
    }
}

Output:

11
11

x > 5 is true, so && must evaluate the right side. ++x makes x = 11, and 11 > 10 is true — the if body prints 11. The final println also prints 11 because the increment already happened.

28. Does short-circuit || evaluate the right side?

public class Test {
    public static void main(String[] args) {

        int x = 10;

        if (x > 5 || ++x > 10) {
            System.out.println(x);
        }
    }
}

Output: 10

The first condition x > 5 is true, and || short-circuits — the right operand ++x is never evaluated. x stays 10, which is what the if body prints. Contrast with &&, where a true left operand forces evaluation of the right.

29. Why doesn’t concat() change the string?

public class Test {
    public static void main(String[] args) {

        String s = "Java";

        s.concat(" Programming");

        System.out.println(s);
    }
}

Output: Java

Strings are immutable. concat() returns a new String ("Java Programming") and leaves s untouched, because the result was never assigned back. To keep the change you’d write s = s.concat(" Programming"). The question tests whether you remember to capture the return value.

30. Do compile-time constants share the pool?

public class Test {
    public static void main(String[] args) {

        String a = "Java";
        String b = "Ja" + "va";

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

Output: true

Both literals are compile-time constants, and "Ja" + "va" is folded into the constant "Java" at compile time. Both resolve to the same string-pool object, so == is true. If b had been built at runtime (e.g., from a variable), it would be a different object and == would be false.

My Private Notes

Notes are auto-saved locally to this device.