Bird
Raised Fist0
Javaprogramming~20 mins

Finally block in Java - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
πŸŽ–οΈ
Finally Block Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of try-catch-finally with return
What is the output of this Java code?
Java
public class Test {
    public static int testMethod() {
        try {
            return 1;
        } catch (Exception e) {
            return 2;
        } finally {
            System.out.println("Finally block executed");
        }
    }
    public static void main(String[] args) {
        System.out.println(testMethod());
    }
}
A
1
Finally block executed
B
Finally block executed
1
C
Finally block executed
2
D
2
Finally block executed
Attempts:
2 left
πŸ’‘ Hint
Remember that finally block runs even if there is a return in try.
❓ Predict Output
intermediate
2:00remaining
Finally block with exception thrown
What is the output of this Java code?
Java
public class Test {
    public static void main(String[] args) {
        try {
            throw new RuntimeException("Error");
        } finally {
            System.out.println("Finally block runs");
        }
    }
}
A
Finally block runs
Exception in thread "main" java.lang.RuntimeException: Error
B
Exception in thread "main" java.lang.RuntimeException: Error
Finally block runs
CFinally block runs
DNo output
Attempts:
2 left
πŸ’‘ Hint
Finally block runs even if exception is thrown.
❓ Predict Output
advanced
2:00remaining
Finally block overriding return value
What is the output of this Java code?
Java
public class Test {
    public static int test() {
        try {
            return 10;
        } finally {
            return 20;
        }
    }
    public static void main(String[] args) {
        System.out.println(test());
    }
}
A10
BCompilation error
CRuntime exception
D20
Attempts:
2 left
πŸ’‘ Hint
Finally block return overrides try return.
❓ Predict Output
advanced
2:00remaining
Finally block with exception and catch
What is the output of this Java code?
Java
public class Test {
    public static void main(String[] args) {
        try {
            int a = 5 / 0;
        } catch (ArithmeticException e) {
            System.out.println("Catch block");
        } finally {
            System.out.println("Finally block");
        }
    }
}
ACatch block
B
Finally block
Catch block
C
Catch block
Finally block
DRuntime exception
Attempts:
2 left
πŸ’‘ Hint
Catch runs first, then finally.
🧠 Conceptual
expert
2:00remaining
Finally block and System.exit() behavior
What happens when System.exit() is called inside a try block with a finally block present?
AFinally block does not execute, program exits immediately
BFinally block executes before program exits
CProgram throws an exception before exiting
DFinally block executes only if System.exit() is called in catch
Attempts:
2 left
πŸ’‘ Hint
System.exit() stops JVM immediately.

Practice

(1/5)
1.

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

easy
A. To catch exceptions thrown in the try block
B. To throw new exceptions
C. To execute code regardless of whether an exception occurs or not
D. To declare exceptions that a method can throw

Solution

  1. Step 1: Understand the role of try-catch-finally

    The try block contains code that might throw exceptions, catch handles them, and finally runs code after both.
  2. Step 2: Identify the purpose of finally

    The finally block always executes, whether an exception occurs or not, to finalize or clean up resources.
  3. Final Answer:

    To execute code regardless of whether an exception occurs or not -> Option C
  4. Quick Check:

    finally always runs = B [OK]
Hint: finally always runs after try/catch blocks [OK]
Common Mistakes:
  • Thinking finally only runs if an exception occurs
  • Confusing finally with catch block
  • Assuming finally can catch exceptions
2.

Which of the following is the correct syntax to add a finally block after a try-catch in Java?

try {
    // code
} catch(Exception e) {
    // handle
} ??? {
    // cleanup
}
easy
A. finally
B. end
C. finalize
D. final

Solution

  1. Step 1: Recall Java exception syntax

    Java uses the keyword finally to define the block that runs after try and catch.
  2. Step 2: Match the correct keyword

    Among options, only finally is the valid keyword for this block.
  3. Final Answer:

    finally -> Option A
  4. Quick Check:

    finally keyword syntax = D [OK]
Hint: finally keyword always spelled 'finally' [OK]
Common Mistakes:
  • Using 'final' instead of 'finally'
  • Confusing with 'finalize' method
  • Using invalid keywords like 'end'
3.

What will be the output of the following Java code?

public class Test {
    public static void main(String[] args) {
        try {
            System.out.print("Try-");
            return;
        } catch(Exception e) {
            System.out.print("Catch-");
        } finally {
            System.out.print("Finally");
        }
        System.out.print("End");
    }
}
medium
A. Try-Catch-End
B. Try-Catch-Finally-End
C. Try-End
D. Try-Finally

Solution

  1. Step 1: Analyze try block execution

    The try block prints "Try-" and then returns, so normally method would exit here.
  2. Step 2: Check finally block behavior with return

    Even with return, finally block executes, printing "Finally" before method exits.
  3. Step 3: Confirm code after finally

    Code after finally (System.out.print("End")) is unreachable due to return, so not executed.
  4. Final Answer:

    Try-Finally -> Option D
  5. Quick Check:

    finally runs even after return = C [OK]
Hint: finally runs even if try returns early [OK]
Common Mistakes:
  • Assuming code after finally runs after return
  • Thinking catch block runs without exception
  • Ignoring finally block execution
4.

Identify the error in the following code snippet:

try {
    int a = 5 / 0;
} finally {
    System.out.println("Cleanup");
}
medium
A. No error, code is valid
B. finally block cannot be used without catch
C. Syntax error in try block
D. Missing catch block for exception

Solution

  1. Step 1: Check try-finally syntax rules

    Java allows try-finally without catch; try must be followed by catch or/and finally.
  2. Step 2: Analyze exception handling

    Division by zero throws ArithmeticException at runtime, finally executes cleanup, then exception propagates to caller.
  3. Step 3: Confirm no error

    The code compiles and runs validly (prints "Cleanup" before propagating exception); no syntax or structural error.
  4. Final Answer:

    No error, code is valid -> Option A
  5. Quick Check:

    try-finally valid without catch = D [OK]
Hint: try-finally without catch is valid [OK]
Common Mistakes:
  • Thinking finally requires catch block
  • Confusing runtime exception with syntax error
  • Believing finally alone causes compile error
5.

Consider this method:

public static int test() {
    try {
        System.out.print("Try-");
        throw new RuntimeException();
    } catch(RuntimeException e) {
        System.out.print("Catch-");
        return 1;
    } finally {
        System.out.print("Finally-");
        return 2;
    }
}

What will System.out.print(test()); output?

hard
A. Try-Catch-Finally-1
B. Try-Catch-Finally-2
C. Try-Finally-2
D. Catch-Finally-1

Solution

  1. Step 1: Trace try block execution

    Try prints "Try-" then throws RuntimeException.
  2. Step 2: Catch block handles exception

    Catch prints "Catch-" and returns 1, but return is not final yet.
  3. Step 3: finally block overrides return

    Finally prints "Finally-" and returns 2, overriding previous return 1.
  4. Step 4: Combine printed output and return value

    Printed output is "Try-Catch-Finally-" and method returns 2, so print(test()) outputs "2" after prints.
  5. Final Answer:

    Try-Catch-Finally-2 -> Option B
  6. Quick Check:

    finally return overrides catch return = A [OK]
Hint: finally return overrides other returns [OK]
Common Mistakes:
  • Ignoring finally return overriding catch return
  • Assuming catch return is final
  • Missing printed output before return