0
0
Javaprogramming~20 mins

Checked vs unchecked exceptions in Java - Practice Questions

Choose your learning style9 modes available
Challenge - 5 Problems
πŸŽ–οΈ
Exception Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of code with checked exception handling
What is the output of this Java code snippet?
Java
import java.io.*;

public class Test {
    public static void main(String[] args) {
        try {
            throw new IOException("IO error");
        } catch (IOException e) {
            System.out.println("Caught checked exception");
        }
    }
}
ACompilation error: unhandled exception
BCaught unchecked exception
CCaught checked exception
DNo output
Attempts:
2 left
πŸ’‘ Hint
Think about how checked exceptions must be caught or declared.
❓ Predict Output
intermediate
2:00remaining
Output of code with unchecked exception
What happens when this Java code runs?
Java
public class Test {
    public static void main(String[] args) {
        throw new NullPointerException("Null pointer");
    }
}
AProgram throws NullPointerException and terminates
BProgram prints "Null pointer"
CCompilation error: unhandled exception
DProgram runs without output
Attempts:
2 left
πŸ’‘ Hint
Unchecked exceptions do not need to be caught or declared.
🧠 Conceptual
advanced
2:00remaining
Difference between checked and unchecked exceptions
Which statement correctly describes the difference between checked and unchecked exceptions in Java?
ANeither checked nor unchecked exceptions require catching or declaring.
BUnchecked exceptions must be caught or declared; checked exceptions do not require this.
CBoth checked and unchecked exceptions must always be caught or declared.
DChecked exceptions must be caught or declared; unchecked exceptions do not require this.
Attempts:
2 left
πŸ’‘ Hint
Think about compiler enforcement for exception handling.
❓ Predict Output
advanced
2:00remaining
Compilation error due to unchecked exception declaration
What error occurs when compiling this Java code?
Java
public class Test {
    public static void main(String[] args) throws NullPointerException {
        throw new NullPointerException("Error");
    }
}
ACompilation error: cannot declare unchecked exception in throws clause
BCompiles and runs, throws NullPointerException
CNo output, program runs normally
DCompilation error: unhandled exception
Attempts:
2 left
πŸ’‘ Hint
Consider if declaring unchecked exceptions in throws clause is allowed.
❓ Predict Output
expert
2:00remaining
Output and exception behavior with mixed checked and unchecked exceptions
What is the output when running this Java program?
Java
import java.io.IOException;

public class Test {
    public static void riskyMethod() throws IOException {
        if (false) {
            throw new IOException("Checked IO error");
        } else {
            throw new IllegalArgumentException("Unchecked argument error");
        }
    }

    public static void main(String[] args) {
        try {
            riskyMethod();
        } catch (IOException e) {
            System.out.println("Caught checked exception");
        }
    }
}
AProgram terminates with IllegalArgumentException
BCaught checked exception
CCompilation error: unhandled exception
DNo output
Attempts:
2 left
πŸ’‘ Hint
Consider which exceptions are caught and which are not.