Skip to content
CodeKruPro

finally changes the variable after return

Predict the outputMedium-HardJava only
Main.java
public class Main {
    static int number() {
        int x = 1;
        try {
            return x;
        } finally {
            x = 5;
        }
    }

    static StringBuilder text() {
        StringBuilder sb = new StringBuilder("a");
        try {
            return sb;
        } finally {
            sb.append("b");
        }
    }

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

This question is part of Pro+

Pro+ opens every question in Interview Prep and the mock interview. Every article and the first 2 questions of each topic stay free. Hands-on Selenium and API tasks follow their course lesson's own lock.

See Pro+

Already have Pro+? Sign in