0
0
Javaprogramming~20 mins

Finally block in Java - Practice Problems & Coding Challenges

Choose your learning style9 modes available
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.