Top 50 Java Exception Handling Quiz

Java Exception Handling Quiz

Hello guys!

You’ve been learning a lot about Java, and now it’s time to test yourself with our Exception Handling Quiz. This quiz is a great way to check your knowledge and skills in handling exceptions in Java. If you’re new to this, don’t worry—this quiz will help you understand these important concepts better.

We’ve already covered many Java quizzes before, so if you haven’t checked them out, be sure to test your skills in those too. This quiz focuses on exception handling, which is crucial for writing robust and error-free code. Let’s see how well you know it!

1 / 50

Which exception is thrown when a method receives an argument formatted differently than expected?

2 / 50

What will be the output of the following code?

public class Test {

    public static void main(String[] args) {

        try {

            testMethod();

        } catch (Exception e) {

            System.out.println(“Exception caught in main”);

        }

    }

    public static void testMethod() throws Exception {

        try {

            throw new Exception(“Exception in testMethod”);

        } catch (Exception e) {

            System.out.println(“Exception caught in testMethod”);

            throw e;

        } finally {

            System.out.println(“Finally block in testMethod”);

        }

    }

}

3 / 50

What is the use of the throw keyword in Java?

4 / 50

Which of the following statements is true about checked exceptions?

5 / 50

What will happen if the exception is thrown in both the try and finally blocks?

6 / 50

Can a try block be followed directly by a finally block without a catch block?

7 / 50

Can a catch block catch multiple exceptions in Java 7 and later?

8 / 50

Which of the following exceptions is a checked exception?

9 / 50

Consider the following code. What will be the output?

public class Test {

    public static void main(String[] args) {

        try {

            return;

        } finally {

            System.out.println(“Finally block executed”);

        }

    }

}

10 / 50

What is the output of the following code snippet?

public class Test {

    public static void main(String[] args) {

        try {

            method1();

        } catch (Exception e) {

            System.out.println(“Exception caught in main: ” + e.getMessage());

        }

    }

    public static void method1() throws Exception {

        try {

            method2();

        } finally {

            System.out.println(“Finally block in method1”);

        }

    }

    public static void method2() throws Exception {

        throw new Exception(“Exception in method2”);

    }

}

11 / 50

What is the correct way to rethrow an exception?

12 / 50

What will happen if an exception is thrown inside the finally block?

13 / 50

What is the primary difference between throw and throws in Java?

14 / 50

What is the output of the following code snippet?

public class Test {

    public static void main(String[] args) {

        try {

            int a = 10 / 0;

        } finally {

            System.out.println(“Finally block executed”);

        }

    }

}

15 / 50

Which method is used to print the stack trace of an exception?

16 / 50

What happens if an exception is thrown in a method and there is no catch block to handle it?

17 / 50

What is the output of the following code snippet?

public class Test {

    public static void main(String[] args) {

        try {

            return;

        } finally {

            System.out.println(“Finally block executed”);

        }

    }

}

18 / 50

Which of the following keywords is used to indicate that a method might throw an exception?

19 / 50

Can a finally block be used without a try block?

20 / 50

Which exception is thrown when an application attempts to use null in a case where an object is required?

21 / 50

Which statement about the catch block is true?

22 / 50

Which of the following best describes the hierarchy of exception handling classes in Java?

23 / 50

What is the purpose of the try-with-resources statement in Java?

24 / 50

Which keyword is used to manually throw an exception in Java?

25 / 50

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

26 / 50

What is the output of the following code snippet?

public class Test {

    public static void main(String[] args) {

        try {

            int a = 10 / 0;

        } catch (ArithmeticException e) {

            System.out.println(“Arithmetic Exception caught”);

        } finally {

            System.out.println(“Finally block executed”);

        }

    }

}

27 / 50

What will be the output of the following code?

public class Test {

    public static void main(String[] args) {

        try {

            throw new NullPointerException(“Custom message”);

        } catch (NullPointerException e) {

            System.out.println(e.getMessage());

        }

    }

}

28 / 50

What will happen if a catch block handles an exception and then throws a new exception?

29 / 50

What happens if an exception is thrown and not caught in a catch block inside a method?

30 / 50

Consider the following code. What will be the output?

public class Test {

    public static void main(String[] args) {

        try {

            String s = null;

            s.length();

        } catch (NullPointerException e) {

            System.out.println(“NullPointerException caught”);

        } finally {

            System.out.println(“Finally block executed”);

        }

    }

}

31 / 50

What is the primary use of the throws keyword?

32 / 50

What will be the output of the following code?

public class Test {

    public static void main(String[] args) {

        try {

            int a = 10 / 0;

        } catch (NullPointerException e) {

            System.out.println(“NullPointerException caught”);

        } catch (Exception e) {

            System.out.println(“Exception caught”);

        }

    }

}

33 / 50

What will be the output of the following code snippet?

public class Test {

    public static void main(String[] args) {

        try {

            int result = divide(10, 0);

            System.out.println(result);

        } catch (ArithmeticException e) {

            System.out.println(“Arithmetic Exception caught”);

        } finally {

            System.out.println(“Finally block executed”);

        }

    }

    public static int divide(int a, int b) {

        return a / b;

    }

}

34 / 50

What happens if a return statement is encountered in a try block and a finally block is also present?

35 / 50

Which of the following is not a type of exception in Java?

36 / 50

What is the output of the following code snippet?

public class Test {

    public static void main(String[] args) {

        try {

            int[] arr = new int[5];

            arr[5] = 10;

        } catch (ArrayIndexOutOfBoundsException | NullPointerException e) {

            System.out.println(“Exception caught”);

        }

    }

}

37 / 50

What is the purpose of the try block in Java?

38 / 50

What is the output of the following code snippet?

public class Test {

    public static void main(String[] args) {

        try {

            throw new Exception(“Custom Exception”);

        } catch (Exception e) {

            System.out.println(“Exception caught: ” + e);

        } finally {

            System.out.println(“Finally block executed”);

        }

    }

}

39 / 50

What will be the output of the following code snippet?

public class Test {

    public static void main(String[] args) {

        try {

            method1();

        } catch (Exception e) {

            System.out.println(“Exception caught in main”);

        }

    }

    public static void method1() throws Exception {

        try {

            method2();

        } finally {

            System.out.println(“Finally block in method1”);

        }

    }

    public static void method2() throws Exception {

        throw new Exception(“Exception in method2”);

    }

}

40 / 50

Can a catch block be written to handle multiple exception types in Java?

41 / 50

What is the correct way to define a custom exception in Java?

42 / 50

What is the output of the following code snippet?

public class Test {

    public static void main(String[] args) {

        try (AutoCloseableResource resource = new AutoCloseableResource()) {

            System.out.println(“Try block”);

            throw new RuntimeException(“Exception in try block”);

        } catch (Exception e) {

            System.out.println(“Catch block: ” + e.getMessage());

        } finally {

            System.out.println(“Finally block”);

        }

    }

}

class AutoCloseableResource implements AutoCloseable {

    @Override

    public void close() {

        System.out.println(“Resource closed”);

    }

}

43 / 50

What will happen if an exception is not caught in a try block?

44 / 50

Which of the following statements is true about the finally block?

45 / 50

Which method from the Throwable class is used to get the cause of an exception?

46 / 50

Which of the following is true about the StackOverflowError?

47 / 50

Which of the following is true about Error in Java?

48 / 50

Can the throws clause be used with multiple exceptions in a method declaration?

49 / 50

Which of the following is true about exceptions in Java?

50 / 50

Consider the following code. What will be the output?

public class Test {

    public static void main(String[] args) {

        try {

            int[] arr = new int[5];

            arr[5] = 10;

        } catch (ArrayIndexOutOfBoundsException e) {

            System.out.println(“Array Index Out Of Bounds Exception caught”);

        } finally {

            System.out.println(“Finally block executed”);

        }

    }

}

Your score is

The average score is 0%

Leave a Reply