Java Exception Handling (try, catch, finally) Quiz

Java
0 Passed
0% acceptance

40 in-depth questions (20 with code) covering types of exceptions, try/catch syntax, finally block purpose, stack traces, and best practices in Java exception handling.

40 Questions
~80 minutes
1

Question 1

What are the three main types of exceptions in Java?

A
Checked exceptions, unchecked exceptions (runtime), and errors
B
Compile-time, runtime, and logical errors
C
Syntax, semantic, and runtime exceptions
D
Mild, moderate, and severe exceptions
2

Question 2

What is a checked exception?

A
Exceptions that must be either caught or declared in the method signature
B
Exceptions that occur at runtime only
C
Exceptions that are automatically handled by the JVM
D
Exceptions that cannot be caught
3

Question 3

What is an unchecked exception?

A
Runtime exceptions and errors that don't require explicit handling
B
Exceptions that are checked at compile-time
C
Exceptions that must be caught
D
Exceptions from external libraries
4

Question 4

What is the purpose of the finally block?

A
To execute cleanup code regardless of whether an exception occurs
B
To catch exceptions that weren't caught by catch blocks
C
To replace the catch block
D
To throw exceptions
5

Question 5

What is a stack trace?

A
A report showing the call stack at the point where an exception occurred
B
A list of all exceptions in the program
C
The order in which catch blocks are executed
D
A summary of finally blocks
6

Question 6

When should you catch an exception?

A
When you can meaningfully handle or recover from the exception
B
Always, to prevent program crashes
C
Never, let them propagate
D
Only checked exceptions
7

Question 7

What is the output of this basic try-catch example?

java
public class Test {
    public static void main(String[] args) {
        try {
            int x = 10 / 0;
        } catch (ArithmeticException e) {
            System.out.println("Caught: " + e.getMessage());
        }
    }
}
A
Caught: / by zero
B
Compilation error
C
Runtime exception
D
No output
8

Question 8

What happens if an exception is thrown in a finally block?

A
It suppresses any exception from the try or catch blocks
B
It replaces the original exception
C
It is ignored
D
It causes a compilation error
9

Question 9

What is the difference between throw and throws?

A
throw throws an exception object, throws declares that a method might throw exceptions
B
They are synonyms
C
throw is for checked exceptions only
D
throws creates new exceptions
10

Question 10

What is the result of this finally execution example?

java
public class Test {
    public static void main(String[] args) {
        System.out.println(test());
    }
    static int test() {
        try {
            return 1;
        } finally {
            return 2;
        }
    }
}
A
2
B
1
C
Compilation error
D
Runtime exception
11

Question 11

What is try-with-resources?

A
A syntax that automatically closes resources, added in Java 7
B
A way to try multiple catch blocks
C
A replacement for finally blocks
D
A way to suppress exceptions
12

Question 12

When should you create custom exceptions?

A
When you need to represent application-specific error conditions
B
Always, instead of using standard exceptions
C
Never, use built-in exceptions
D
Only for checked exceptions
13

Question 13

What is the output of this nested try-catch example?

java
public class Test {
    public static void main(String[] args) {
        try {
            try {
                throw new RuntimeException("Inner");
            } catch (RuntimeException e) {
                System.out.println("Inner catch: " + e.getMessage());
                throw e;
            }
        } catch (RuntimeException e) {
            System.out.println("Outer catch: " + e.getMessage());
        }
    }
}
A
Inner catch: Inner Outer catch: Inner
B
Inner catch: Inner
C
Outer catch: Inner
D
Compilation error
14

Question 14

What is exception chaining?

A
Wrapping one exception inside another to preserve the original cause
B
Catching multiple exceptions in one block
C
Throwing exceptions from finally blocks
D
Using multiple catch blocks
15

Question 15

What is the purpose of multi-catch blocks?

A
To catch multiple exception types in a single catch block, added in Java 7
B
To catch exceptions in multiple threads
C
To replace finally blocks
D
To suppress exceptions
16

Question 16

What is the result of this try-with-resources example?

java
class Resource implements AutoCloseable {
    public void close() { System.out.println("Closed"); }
}
public class Test {
    public static void main(String[] args) {
        try (Resource r = new Resource()) {
            System.out.println("Using resource");
            throw new RuntimeException("Error");
        } catch (Exception e) {
            System.out.println("Caught: " + e.getMessage());
        }
    }
}
A
Using resource Closed Caught: Error
B
Using resource Caught: Error Closed
C
Using resource Closed
D
Compilation error
17

Question 17

When should you use runtime exceptions vs checked exceptions?

A
Runtime exceptions for programming errors, checked exceptions for recoverable conditions
B
Runtime exceptions for everything
C
Checked exceptions for everything
D
It doesn't matter
18

Question 18

What is the output of this exception propagation example?

java
public class Test {
    static void method1() { throw new RuntimeException("From method1"); }
    static void method2() { method1(); }
    static void method3() { method2(); }
    public static void main(String[] args) {
        try {
            method3();
        } catch (RuntimeException e) {
            System.out.println("Caught in main: " + e.getMessage());
        }
    }
}
A
Caught in main: From method1
B
Compilation error
C
Runtime exception
D
No output
19

Question 19

What is the best practice for exception messages?

A
Include context, be descriptive, and avoid sensitive information
B
Keep them short and cryptic
C
Include stack traces in messages
D
Use generic messages for all exceptions
20

Question 20

What happens with this suppressed exception example?

java
class Resource implements AutoCloseable {
    public void close() throws Exception {
        throw new Exception("Close failed");
    }
}
public class Test {
    public static void main(String[] args) {
        try (Resource r = new Resource()) {
            throw new RuntimeException("Main error");
        } catch (Exception e) {
            System.out.println("Caught: " + e.getMessage());
            for (Throwable t : e.getSuppressed()) {
                System.out.println("Suppressed: " + t.getMessage());
            }
        }
    }
}
A
Caught: Main error Suppressed: Close failed
B
Caught: Close failed
C
Compilation error
D
Runtime exception
21

Question 21

Why should you avoid catching Exception or Throwable?

A
It catches errors and runtime exceptions that should not be caught
B
It makes code faster
C
It prevents compilation
D
It is required for all methods
22

Question 22

What is the output of this finally return example?

java
public class Test {
    static String test() {
        try {
            return "try";
        } catch (Exception e) {
            return "catch";
        } finally {
            System.out.println("finally executed");
        }
    }
    public static void main(String[] args) {
        System.out.println(test());
    }
}
A
finally executed try
B
try finally executed
C
finally executed
D
Compilation error
23

Question 23

What is the difference between Error and Exception?

A
Errors are serious system problems, exceptions are recoverable conditions
B
They are the same
C
Errors are checked, exceptions are unchecked
D
Exceptions are more serious than errors
24

Question 24

What is the result of this multi-catch example?

java
public class Test {
    public static void main(String[] args) {
        try {
            if (args.length > 0) {
                throw new IllegalArgumentException("Bad arg");
            } else {
                throw new NullPointerException("Null");
            }
        } catch (IllegalArgumentException | NullPointerException e) {
            System.out.println("Caught: " + e.getClass().getSimpleName());
        }
    }
}
A
Caught: NullPointerException
B
Caught: IllegalArgumentException
C
Compilation error
D
Runtime exception
25

Question 25

When should you declare exceptions in method signatures?

A
When throwing checked exceptions that callers should handle
B
For all exceptions
C
Never, catch everything internally
D
Only for runtime exceptions
26

Question 26

What is the output of this exception re-throwing example?

java
public class Test {
    public static void main(String[] args) {
        try {
            throw new RuntimeException("Original");
        } catch (RuntimeException e) {
            throw new IllegalStateException("Wrapped", e);
        }
    }
}
A
IllegalStateException with RuntimeException as cause
B
RuntimeException
C
Compilation error
D
No exception
27

Question 27

Why is it important to close resources properly?

A
To prevent resource leaks, memory leaks, and file handle exhaustion
B
To make code faster
C
To avoid compilation errors
D
It doesn't matter
28

Question 28

What is the result of this nested exception handling?

java
public class Test {
    public static void main(String[] args) {
        try {
            try {
                throw new Exception("Inner");
            } finally {
                System.out.println("Inner finally");
            }
        } catch (Exception e) {
            System.out.println("Caught: " + e.getMessage());
        } finally {
            System.out.println("Outer finally");
        }
    }
}
A
Inner finally Caught: Inner Outer finally
B
Caught: Inner Inner finally Outer finally
C
Inner finally Outer finally Caught: Inner
D
Compilation error
29

Question 29

What is the best practice for logging exceptions?

A
Log the full stack trace at error level for debugging, but show user-friendly messages
B
Never log exceptions
C
Log only the exception message
D
Log everything at info level
30

Question 30

What happens in this try-with-resources with exception in close?

java
class Resource implements AutoCloseable {
    public void close() throws Exception {
        throw new Exception("Close error");
    }
}
public class Test {
    public static void main(String[] args) throws Exception {
        try (Resource r = new Resource()) {
            System.out.println("Using resource");
        }
    }
}
A
Using resource Exception: Close error
B
Using resource
C
Exception: Close error
D
Compilation error
31

Question 31

When should you use checked exceptions?

A
For recoverable error conditions that callers should be aware of and handle
B
For all exceptions
C
Never, use runtime exceptions
D
Only in libraries
32

Question 32

What is the output of this exception in constructor example?

java
class TestClass {
    TestClass() throws Exception {
        throw new Exception("Constructor failed");
    }
}
public class Test {
    public static void main(String[] args) {
        try {
            new TestClass();
        } catch (Exception e) {
            System.out.println("Caught: " + e.getMessage());
        }
    }
}
A
Caught: Constructor failed
B
Compilation error
C
Runtime exception
D
No output
33

Question 33

Why should you avoid empty catch blocks?

A
They silently ignore errors, making debugging impossible
B
They cause compilation errors
C
They make code slower
D
They are required for checked exceptions
34

Question 34

What is the result of this exception precedence example?

java
public class Test {
    public static void main(String[] args) {
        try {
            throw new RuntimeException("Runtime");
        } catch (Exception e) {
            System.out.println("Caught Exception: " + e.getMessage());
        } catch (RuntimeException e) {
            System.out.println("Caught Runtime: " + e.getMessage());
        }
    }
}
A
Compilation error - unreachable code
B
Caught Exception: Runtime
C
Caught Runtime: Runtime
D
Runtime exception
35

Question 35

What is the best practice for exception handling in layered applications?

A
Translate low-level exceptions to application-specific exceptions at layer boundaries
B
Let all exceptions bubble up unchanged
C
Catch everything in the top layer
D
Never use custom exceptions
36

Question 36

What is the output of this finally with return example?

java
public class Test {
    static boolean test() {
        try {
            return true;
        } finally {
            return false;
        }
    }
    public static void main(String[] args) {
        System.out.println(test());
    }
}
A
false
B
true
C
Compilation error
D
Runtime exception
37

Question 37

When should you use try-catch in constructors?

A
When initialization can fail and you want to prevent invalid object creation
B
Never, constructors shouldn't throw exceptions
C
Always, for safety
D
Only for checked exceptions
38

Question 38

What is the result of this suppressed exception access?

java
public class Test {
    public static void main(String[] args) {
        try (java.io.StringReader r = new java.io.StringReader("test")) {
            throw new RuntimeException("Main");
        } catch (Exception e) {
            System.out.println("Main: " + e.getMessage());
            Throwable[] suppressed = e.getSuppressed();
            if (suppressed.length > 0) {
                System.out.println("Suppressed: " + suppressed[0].getMessage());
            }
        }
    }
}
A
Main: Main
B
Main: Main Suppressed: (some close message)
C
Compilation error
D
Runtime exception
39

Question 39

Why is proper exception handling important for security?

A
It prevents information leakage through stack traces and error messages
B
It makes code faster
C
It prevents compilation
D
It is not related to security
40

Question 40

Mastering exception handling means you now write code that:

A
Fails gracefully, provides meaningful error messages, ensures resource cleanup, and distinguishes between different types of errors for appropriate handling
B
Never throws exceptions
C
Catches everything with Exception
D
Ignores all errors

QUIZZES IN Java