Challenge - 5 Problems
Exception Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
β Predict Output
intermediateOutput 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"); } } }
Attempts:
2 left
π‘ Hint
Think about how checked exceptions must be caught or declared.
β Incorrect
IOException is a checked exception. The code catches it and prints the message.
β Predict Output
intermediateOutput 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"); } }
Attempts:
2 left
π‘ Hint
Unchecked exceptions do not need to be caught or declared.
β Incorrect
NullPointerException is unchecked and not caught, so the program terminates with the exception.
π§ Conceptual
advancedDifference between checked and unchecked exceptions
Which statement correctly describes the difference between checked and unchecked exceptions in Java?
Attempts:
2 left
π‘ Hint
Think about compiler enforcement for exception handling.
β Incorrect
Checked exceptions are checked at compile time and must be handled or declared. Unchecked exceptions are not checked at compile time.
β Predict Output
advancedCompilation 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"); } }
Attempts:
2 left
π‘ Hint
Consider if declaring unchecked exceptions in throws clause is allowed.
β Incorrect
Declaring unchecked exceptions in throws clause is allowed but not required. The code compiles and throws the exception at runtime.
β Predict Output
expertOutput 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"); } } }
Attempts:
2 left
π‘ Hint
Consider which exceptions are caught and which are not.
β Incorrect
The method may throw IOException (checked) or IllegalArgumentException (unchecked). Only IOException is caught. If unchecked exception occurs, program terminates.
