How Much Do You Know About Exception Handling? Trivia Quiz

10 Questions | Attempts: 1227

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"

  • 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

  • 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.

  • 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

  • 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

  • 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.

  • 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.

  • 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.

  • 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.

  • 10. 
    What is the closest common ancestor of RuntimeException, Error, IOException, and ClassNotFoundException?
    • A. 

      Exception

    • B. 

      Catchable

    • C. 

      Option 3

    • D. 

      Throwable

Back to Top Back to top
×

Wait!
Here's an interesting quiz for you.

We have other quizzes matching your interest.