Loading

Q1.

Which of the following statements about the finally block is true?

A. It executes only when an exception occurs

B. It executes only when no exception occurs

C. It always executes regardless of exception occurrence

D. It executes only if there is no catch block

Q2.

When will the finally block NOT be executed?

A. When there is no exception in the try block

B. When an exception is caught by the catch block

C. When System.exit(0) is called in the try block

D. When multiple catch blocks exist

Q3.

What is the main purpose of the finally block in Java?


A. Declaring variables
B. Defining exception types
C. Executing cleanup code (like closing resources)
D. Catching exceptions

Q4.

What happens if both try and finally blocks have a return statement?

A. The value from the try block is returned

B. The value from the finally block is returned

C. Compilation error occurs

D. Runtime exception occurs

Q5.

Which of the following is NOT correct about the finally block?

A. A try block can have at most one finally block

B. The finally block is optional in Java

C. The finally block must always follow the catch block

D. The finally block ensures execution of cleanup code