How Much Do You Know About Exception Handling? Trivia Quiz

Reviewed by Editorial Team
The ProProfs editorial team is comprised of experienced subject matter experts. They've collectively created over 10,000 quizzes and lessons, serving over 100 million users. Our team includes in-house content moderators and subject matter experts, as well as a global network of rigorously trained contributors. All adhere to our comprehensive editorial guidelines, ensuring the delivery of high-quality content.
Learn about Our Editorial Process
| By RiaTor
R
RiaTor
Community Contributor
Quizzes Created: 1 | Total Attempts: 1,540
| Attempts: 1,540 | Questions: 10
Please wait...
Question 1 / 10
0 %
0/100
Score 0/100
1. 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");}}

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.

Submit
Please wait...
About This Quiz
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... see moreare 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! see less

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 {}

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.

Submit
3. 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.");}}

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.

Submit
4. 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.");}}

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.

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

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.

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

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.

Submit
7. 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 {//...}}

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.

Submit
8. 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.");}}

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.

Submit
9. 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");}}}

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.

Submit
10. Which statements are true?

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.

Submit
View My Results

Quiz Review Timeline (Updated): Aug 22, 2023 +

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
Cancel
  • All
    All (10)
  • Unanswered
    Unanswered ()
  • Answered
    Answered ()
Public class MyClass {public static void main(String[] args) {int...
Public class MyClass {public static void main(String[] args) throws A...
Public class MyClass {public static void main(String[] args) throws...
Class A {// InterruptedException is a direct subclass of...
What is the closest common ancestor of RuntimeException, Error,...
Public class MyClass {public static void main(String[] args)...
Class A {void f() throws ArithmeticException {//...}}public class...
Public class MyClass {public static void main(String[] args) {try...
Public class Exceptions {public static void main(String[] args) {try...
Which statements are true?
Alert!

Advertisement