0
0
Javaprogramming~20 mins

Exception propagation in Java - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
πŸŽ–οΈ
Exception Propagation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this Java code with exception propagation?
Consider the following Java code. What will be printed when the main method runs?
Java
public class Test {
    public static void main(String[] args) {
        try {
            methodA();
        } catch (Exception e) {
            System.out.println("Caught in main: " + e.getMessage());
        }
    }

    static void methodA() throws Exception {
        methodB();
    }

    static void methodB() throws Exception {
        throw new Exception("Error in methodB");
    }
}
ACaught in main: null
BException in thread "main" java.lang.Exception: Error in methodB
CCaught in main: Error in methodB
DNo output
Attempts:
2 left
πŸ’‘ Hint
Trace how the exception thrown in methodB moves up to main and is caught there.
❓ Predict Output
intermediate
2:00remaining
What happens if a checked exception is not declared or caught?
What error will the following Java code produce when compiled?
Java
public class Test {
    public static void main(String[] args) {
        methodA();
    }

    static void methodA() {
        methodB();
    }

    static void methodB() throws Exception {
        throw new Exception("Error");
    }
}
ACompilation error: unhandled exception Exception
BRuntime exception thrown: Exception
CProgram runs and prints nothing
DCompilation error: methodB not found
Attempts:
2 left
πŸ’‘ Hint
Checked exceptions must be declared or caught.
πŸ”§ Debug
advanced
2:00remaining
Why does this code cause a compilation error related to exception propagation?
Identify the cause of the compilation error in this code and select the correct explanation.
Java
public class Test {
    public static void main(String[] args) {
        try {
            methodA();
        } catch (Exception e) {
            System.out.println("Caught: " + e.getMessage());
        }
    }

    static void methodA() {
        methodB();
    }

    static void methodB() throws Exception {
        throw new Exception("Error");
    }
}
Amain does not catch the exception thrown by methodB
BmethodA does not declare 'throws Exception' but calls methodB which throws Exception
CmethodB does not throw any exception
DmethodA catches the exception but does not rethrow it
Attempts:
2 left
πŸ’‘ Hint
Check methodA's signature and its call to methodB.
❓ Predict Output
advanced
2:00remaining
What is the output when multiple exceptions propagate and are caught?
What will be printed when this Java program runs?
Java
public class Test {
    public static void main(String[] args) {
        try {
            methodA();
        } catch (Exception e) {
            System.out.println("Caught in main: " + e.getMessage());
        }
    }

    static void methodA() throws Exception {
        try {
            methodB();
        } catch (NullPointerException e) {
            System.out.println("Caught in methodA: " + e.getMessage());
        }
    }

    static void methodB() throws Exception {
        throw new Exception("Error in methodB");
    }
}
ACaught in main: null
BCaught in methodA: Error in methodB
CNo output
DCaught in main: Error in methodB
Attempts:
2 left
πŸ’‘ Hint
Check which exception type is caught in methodA and which propagates to main.
🧠 Conceptual
expert
3:00remaining
How many exceptions are caught and printed in this nested propagation example?
Given the code below, how many times will a message be printed to the console?
Java
public class Test {
    public static void main(String[] args) {
        try {
            methodA();
        } catch (Exception e) {
            System.out.println("Caught in main: " + e.getMessage());
        }
    }

    static void methodA() throws Exception {
        try {
            methodB();
        } catch (Exception e) {
            System.out.println("Caught in methodA: " + e.getMessage());
            throw e;
        }
    }

    static void methodB() throws Exception {
        throw new Exception("Error in methodB");
    }
}
A2
B1
C3
D0
Attempts:
2 left
πŸ’‘ Hint
Count each catch block that prints a message during propagation.