Java · Question 8 · Medium

What will be 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());
    }
}
  1. 1 F
  2. F 1
  3. 1
  4. F
Answer and explanation

Answer: B. F 1

Explanation: return 1 sets the return value, but the finally block runs before f() actually returns, printing "F ". Then main prints the returned 1.

Previous: What will be the output of the following Java program? · Next: What will be the output of the following Java program?

Practise java

Try a fresh set of java questions with an explanation after every answer, free and without signing up: Java practice. Or sit a full placement mock test.

More java questions

More technical & programming practice

Python · Data Structures · Algorithms · Object-Oriented Programming · DBMS · SQL · Operating Systems · Computer Networks

LogicWiz Exams home · All practice topics · Placement mock tests · Daily 10 challenge