Question discussion

What is the output of the following Java program?public class Main { static int f() { try { return 1; } finally { System.out.print("F"); }…

927Medium DSSSB TGT Computer Science 0 replies

What is the output of the following Java program?

public class Main {
    static int f() {
        try { return 1; }
        finally { System.out.print("F"); }
    }
    public static void main(String[] args) {
        System.out.print(f());
    }
}
  • AF1
  • B1
  • C1F
  • DF
Correct answer: A

Explanation

The finally block runs before the method returns, so "F" is printed first, then the returned value 1.

Discussion 0

Nobody has replied yet. Start the discussion!