Multiple Choice Questions 5

  1. Which of the following is not an advantage of Java exception handling.?

  2. . Java separates exception handling from normal processing tasks.
    Exception handling improves performance.
    Exception handling makes it possible for the caller's caller to handle the exception.
    Exception handling simplifies programming because the error-reporting and error-handling code can may be placed at the catch block.

  3. What is wrong in the following program?

  4. class TestRationalWithException
    { public static void main (String[] args)
    { Rational r1 = new Rational(4,2);
    Rational r2 = new Rational(2,3);
    Rational r3 = new Rational(0,1);
    try
    { System.out.println(r1 + " + " + r2 + " = " + r1.add(r2));
    System.out.println(r1 + " - " + r2 + " = " + r1.subtract(r2));
    System.out.println(r1 + " * " + r2 + " = " + r1.multiply(r2));
    System.out.println(r1 + " + " + r2 + " = " + r1.add(r2));
    }
    }

    You cannot have a try block without a catch block.
    You cannot have a try block without a catch block or a finally block.
    A method call that does not claim exceptions cannot be placed inside a try block.
    Nothing is wrong.

  5. What is displayed on the console when running the following program?

  6. class Test
    { public static void main (String[] args)
    { try
    { System.out.println("Welcome to Java");
    }
    finally
    { System.out.println("End of the block"); }
    }
    }

    Welcome to Java
    Welcome to Java followed by End of the block in the next line
    End of the block
    None of the above

  7. What is displayed on the console when running the following program?

  8. class Test
    { public static void main (String[] args)
    { try
    { System.out.println("Welcome to Java");
    return;
    }
    finally
    { System.out.println("End of the block"); }
    }
    }

    Welcome to Java
    Welcome to Java followed by End of the block in the next line
    End of the block
    None of the above

  9. What is displayed on the console when running the following program?

  10. class Test
    { public static void main(String[] args)
    { try
    { System.out.println("Welcome to Java");
    int i = 0;
    int y = 2/i;
    System.out.println("Welcome to HTML");
    }
    finally
    { System.out.println("End of the block"); }
    }

    Welcome to Java
    Welcome to Java followed by End of the block in the next line
    The program displays three lines: Welcome to Java, Welcome to HTML, End of the block
    None of the above

  11. What is displayed on the console when running the following program?

  12. class Test
    { public static void main(String[] args)
    { try
    { System.out.println("Welcome to Java");
    int i = 0;
    double y = 2.0/i;
    System.out.println("Welcome to HTML");
    }
    finally
    { System.out.println("End of the block"); }
    }
    }

    Welcome to Java
    Welcome to Java followed by End of the block in the next line
    The program displays three lines: Welcome to Java, Welcome to HTML, End of the block
    None of the above

  13. What is displayed on the console when running the following program?

  14. class Test
    { public static void main(String[] args)
    { try
    { System.out.println("Welcome to Java");
    int i = 0;
    int y = 2/i;
    System.out.println("Welcome to Java");
    }
    catch (RuntimeException e)
    { System.out.println("Welcome to Java"); }
    finally
    { System.out.println("End of the block"); }
    }

    The program displays Welcome to Java three times followed by End of the block.
    The program displays Welcome to Java two times followed by End of the block.
    The program displays Welcome to Java three times.
    The program displays Welcome to Java two times.

  15. What is displayed on the console when running the following program?

  16. class Test
    { public static void main(String[] args)
    { try
    { System.out.println("Welcome to Java");
    int i = 0;
    int y = 2/i;
    System.out.println("Welcome to Java");
    }
    catch (RuntimeException e)
    { System.out.println("Welcome to Java"); }
    finally
    { System.out.println("End of the block"); }
    System.out.println("End of the block");
    }
    }

    The program displays Welcome to Java three times followed by End of the block.
    The program displays Welcome to Java two times followed by End of the block.
    The program displays Welcome to Java two times followed by End of the block two times.
    You cannot catch RuntimeException errors.

  17. What is displayed on the console when running the following program?

  18. class Test
    { public static void main(String[] args)
    { try
    { System.out.println("Welcome to Java");
    int i = 0;
    int y = 2/i;
    System.out.println("Welcome to Java");
    }
    finally
    { System.out.println("End of the block"); }
    System.out.println("End of the block");
    }
    }

    The program displays Welcome to Java three times followed by End of the block.
    The program displays Welcome to Java two times followed by End of the block.
    The program displays Welcome to Java two times followed by End of the block two times.
    The program displays Welcome to Java and End of the block, then terminates because of an unhandled exception.

  19. What is displayed on the console when running the following program?

  20. class Test
    { public static void main(String[] args)
    { try
    { Rational r1 = new Rational(3, 4);
    Rational r2 = new Rational(0, 1);
    Rational x = r1.divide(r2);
    int i = 0;
    int y = 2/i;
    }
    catch (Exception e)
    { System.out.println("Rational operation error "); }
    catch (RuntimeException e)
    { System.out.println("Integer operation error"); }
    }
    }

    The program displays Rational operation error.
    The program displays Integer operation error.
    The program displays Rational operation error followed by Integer operation error.
    The program has a compilation error.

Visited times since 7 March 1999.