How Much Do You Know About Exception Handling? Trivia Quiz

Approved & Edited by ProProfs Editorial Team
The editorial team at ProProfs Quizzes consists of a select group of subject experts, trivia writers, and quiz masters who have authored over 10,000 quizzes taken by more than 100 million users. This team includes our in-house seasoned quiz moderators and subject matter experts. Our editorial experts, spread across the world, are rigorously trained using our comprehensive guidelines to ensure that you receive the highest quality quizzes.
Learn about Our Editorial Process
| By RiaTor
R
RiaTor
Community Contributor
Quizzes Created: 1 | Total Attempts: 1,474
Questions: 10 | Attempts: 1,474

SettingsSettingsSettings
How Much Do You Know About Exception Handling? Trivia Quiz - Quiz

Do you consider yourself a good programmer? How much do you know about exception handling? Take this trivia quiz and check your programming skills. Applications in a computer are expected to run smoothly with no downtime, but this is entirely not the case. When there is an anomaly in the application, a program is expected to have some information on exceptional handling to correct it. The quiz below is designed to check just what you know about this process. Try it out!


Questions and Answers
  • 1. 

    Public class Exceptions {public static void main(String[] args) {try {if (args.length == 0) return;System.out.println(args[0]);} finally {System.out.println("The end");}}}

    • A.

      If run with no arguments, the program will produce no output.

    • B.

      If run with no arguments, the program will print "The end".

    • C.

      The program will throw an ArrayIndexOutOfBoundsException

    • D.

      If run with one argument, the program will simply print the given argument

    • E.

      If run with one argument, the program will print the given argument followed by "The end"

    Correct Answer(s)
    B. If run with no arguments, the program will print "The end".
    E. If run with one argument, the program will print the given argument followed by "The end"
    Explanation
    (b) and (e)
    If run with no arguments, the program will print "The end". If run with one argument, the program will print the given argument followed by "The end". The
    finallyblock will always be executed, no matter how control leaves the tryblock.

    Rate this question:

  • 2. 

    Public class MyClass {public static void main(String[] args) throws A {try {f();} finally {System.out.println("Done.");} catch (A e) {throw e;}}public static void f() throws B {throw new B();}}class A extends Throwable {}class B extends A {}

    • A.

      The main()method must declare that it throws B.

    • B.

      The finally block must follow the catch block in the main()method.

    • C.

      The catch block in the main()method must declare that it catches B rather than A.

    • D.

      A single tryblock cannot be followed by both a finallyand a catchblock

    • E.

      The declaration of class A is illegal

    Correct Answer
    B. The finally block must follow the catch block in the main()method.
    Explanation
    (b)
    The only thing that is wrong with the code is the ordering of the catchand finally
    blocks. If present, the finallyblock must always appear last in a try-catch-finally
    construct.

    Rate this question:

  • 3. 

    Class A {// InterruptedException is a direct subclass of Exception.void f() throws ArithmeticException, InterruptedException {div(5, 5);}int div(int i, int j) throws ArithmeticException {return i/j;}}public class MyClass extends A {void f() /* throws [...list of exceptions...] */ {try {div(5, 0);} catch (ArithmeticException e) {return;}throw new RuntimeException("ArithmeticException was expected.");}}

    • A.

      Does not need to specify any exceptions.

    • B.

      Needs to specify that it throws ArithmeticException

    • C.

      Needs to specify that it throws InterruptedException.

    • D.

      Needs to specify that it throws RuntimeException

    • E.

      Needs to specify that it throws both ArithmeticExceptionand InterruptedException.

    Correct Answer
    A. Does not need to specify any exceptions.
    Explanation
    (a)
    Overriding methods can specify all, none, or a subset of the checked exceptions
    the overridden method declares in its throwsclause. The InterruptedExceptionis the
    only checked exception specified in the throwsclause of the overridden method.
    The overriding method f()need not specify the InterruptedExceptionfrom the
    throwsclause of the overridden method, because the exception is not thrown here.

    Rate this question:

  • 4. 

    Public class MyClass {public static void main(String[] args) {RuntimeException re = null;throw re;}}

    • A.

      The code will fail to compile because the main()method does not declare that it throws RuntimeExceptionin its declaration

    • B.

      The program will fail to compile because it cannot throw re.

    • C.

      The program will compile without error and will throw java.lang.RuntimeExceptionwhen run.

    • D.

      The program will compile without error and will throw java.lang.NullPointerExceptionwhen run

    • E.

      The program will compile without error and will run and terminate without any output

    Correct Answer
    D. The program will compile without error and will throw java.lang.NullPointerExceptionwhen run
    Explanation
    (d)
    The program will compile without error, but will throw a NullPointerExceptionwhen
    run. The throwstatement can only throw Throwableobjects. A NullPointerException
    will be thrown if the expression of the throwstatement results in a nullreference.

    Rate this question:

  • 5. 

    Which statements are true?

    • A.

      If an exception is not caught in a method, the method will terminate and normal execution will resume.

    • B.

      An overriding method must declare that it throws the same exception classes as the method it overrides

    • C.

      The main()method of a program can declare that it throws checked exceptions.

    • D.

      A method declaring that it throws a certain exception class may throw instances of any subclass of that exception class

    • E.

      Finallyblocks are executed if, and only if, an exception gets thrown while inside the corresponding tryblock

    Correct Answer(s)
    C. The main()method of a program can declare that it throws checked exceptions.
    D. A method declaring that it throws a certain exception class may throw instances of any subclass of that exception class
    Explanation
    (c) and (d)
    Normal execution will only resume if the exception is caught by the method. The
    uncaught exception will propagate up the runtime stack until some method handles it. An overriding method need only declare that it can throw a subset of the
    checked exceptions the overridden method can throw. The main()method can
    declare that it throws checked exceptions just like any other method. The finally
    block will always be executed, no matter how control leaves the tryblock.

    Rate this question:

  • 6. 

    Public class MyClass {public static void main(String[] args) {int k=0;try {int i = 5/k;} catch (ArithmeticException e) {System.out.println("1");} catch (RuntimeException e) {System.out.println("2");return;} catch (Exception e) {System.out.println("3");} finally {System.out.println("4");}System.out.println("5");}}

    • A.

      The program will only print 5.

    • B.

      The program will only print 1and 4, in that order.

    • C.

      The program will only print 1, 2, and 4, in that order

    • D.

      The program will only print 1, 4, and 5, in that order

    • E.

      The program will only print 1, 2,4, and 5, in that order.

    • F.

      The program will only print 3 and 5, in that order.

    Correct Answer
    D. The program will only print 1, 4, and 5, in that order
    Explanation
    (d) The program will only print 1,4, and 5, in that order. The expression 5/kwill throw
    anArithmeticException, since kequals 0. Control is transferred to the first catch
    block, since it is the first block that can handle arithmetic exceptions. This exception handler simply prints 1. The exception has now been caught and normal execution can resume. Before leaving the try statement, the finally block is executed.
    This block prints 4. The last statement of the main()method prints 5.

    Rate this question:

  • 7. 

    Public class MyClass {public static void main(String[] args) {try {f();} catch (InterruptedException e) {System.out.println("1");throw new RuntimeException();} catch (RuntimeException e) {System.out.println("2");return;} catch (Exception e) {System.out.println("3");} finally {System.out.println("4");}System.out.println("5");}// InterruptedException is a direct subclass of Exception.static void f() throws InterruptedException {throw new InterruptedException("Time for lunch.");}}

    • A.

      The program will print 5.

    • B.

      The program will print 1and 4, in that order.

    • C.

      The program will print 1,2, and 4, in that order.

    • D.

      The program will print 1,4, and 5, in that order.

    • E.

      The program will print 1,2, 4, and 5, in that order

    • F.

      The program will print 3and 5, in that order.

    Correct Answer
    B. The program will print 1and 4, in that order.
    Explanation
    (b)
    The program will print 1and 4, in that order. An InterruptedExceptionis handled
    in the first catchblock. Inside this block a new RuntimeExceptionis thrown. This
    exception was not thrown inside the tryblock and will not be handled by the catch
    blocks, but will be sent to the caller of the main()method. Before this happens, the
    finallyblock is executed. The code to print 5is never reached, since the RuntimeExceptionremains uncaught after the execution of the finallyblock.

    Rate this question:

  • 8. 

    Class A {void f() throws ArithmeticException {//...}}public class MyClass extends A {public static void main(String[] args) {A obj = new MyClass();try {obj.f();} catch (ArithmeticException e) {return;} catch (Exception e) {System.out.println(e);throw new RuntimeException("Something wrong here");}}// InterruptedException is a direct subclass of Exception.void f() throws InterruptedException {//...}}

    • A.

      The main()method must declare that it throws RuntimeException.

    • B.

      The overriding f()method in MyClassmust declare that it throws ArithmeticException, since the f()method in class Adeclares that it does

    • C.

      The overriding f()method in MyClassis not allowed to throw InterruptedException, since the f()method in class Adoes not throw this exception

    • D.

      The compiler will complain that the catch(ArithmeticException)block shadows the catch(Exception)block

    • E.

      You cannot throw exceptions from a catchblock

    • F.

      Nothing is wrong with the code, it will compile without errors.

    Correct Answer
    C. The overriding f()method in MyClassis not allowed to throw InterruptedException, since the f()method in class Adoes not throw this exception
    Explanation
    (c)
    The overriding f()method in MyClassis not permitted to throw the checked
    InterruptedException, since the f()method in class Adoes not throw this exception. To avoid compilation errors, either the overriding f()method must not throw an InterruptedExceptionor the overridden f()method must declare that it
    can throw an InterruptedException.

    Rate this question:

  • 9. 

    Public class MyClass {public static void main(String[] args) throws InterruptedException {try {f();System.out.println("1");} finally {System.out.println("2");}System.out.println("3");}// InterruptedException is a direct subclass of Exception.static void f() throws InterruptedException {throw new InterruptedException("Time to go home.");}}

    • A.

      The program will print 2and throw InterruptedException.

    • B.

      The program will print 1and 2, in that order.

    • C.

      The program will print 1,2, and 3, in that order.

    • D.

      The program will print 2 and 3, in that order.

    • E.

      The program will print 3 and 2, in that order.

    • F.

      The program will print 1 and 3, in that order.

    Correct Answer
    A. The program will print 2and throw InterruptedException.
    Explanation
    (a)
    The program will print 2and throw an InterruptedException. An InterruptedException
    is thrown in the tryblock. There is no catchblock to handle the exception, so it will
    be sent to the caller of the main()method, i.e., to the default exception handler. Before
    this happens, the finallyblock is executed. The code to print 3is never reached.

    Rate this question:

  • 10. 

    What is the closest common ancestor of RuntimeException, Error, IOException, and ClassNotFoundException?

    • A.

      Exception

    • B.

      Catchable

    • C.

      Option 3

    • D.

      Throwable

    Correct Answer
    D. Throwable
    Explanation
    The closest common ancestor of RuntimeException, Error, IOException, and ClassNotFoundException is Throwable. This is because all these classes are direct or indirect subclasses of Throwable. Throwable is the superclass of all exceptions and errors in Java, and it provides common methods and fields for handling and propagating exceptions and errors. Therefore, Throwable is the correct answer as it is the closest common ancestor of all the mentioned classes.

    Rate this question:

Quiz Review Timeline +

Our quizzes are rigorously reviewed, monitored and continuously updated by our expert board to maintain accuracy, relevance, and timeliness.

  • Current Version
  • Aug 22, 2023
    Quiz Edited by
    ProProfs Editorial Team
  • Jul 21, 2014
    Quiz Created by
    RiaTor
Back to Top Back to top
Advertisement
×

Wait!
Here's an interesting quiz for you.

We have other quizzes matching your interest.