MCQs
public class MyProgram
{
public static void throwit()
{
throw new RuntimeException();
}
public static void main(String args[])
{
try
{
System.out.println("Hello world ");
throwit();
System.out.println("Done with try block ");
}
finally
{
System.out.println("Finally executing ");
}
}
}
which answer most closely indicates the behavior of the program?
Once the program throws a RuntimeException (in the throwit() method) that is not caught,
the finally block will be executed and the program will be terminated. If a method does not
handle an exception, the finally block is executed before the exception is propagated.
What is the output of this program?
class exception_handling {
public static void main(String args[]) {
try {
int a = args.length;
int b = 10 / a;
System.out.print(a);
try {
if (a == 1)
a = a / a - a;
if (a == 2) {
int c = {1};
c[8] = 9;
}
}
catch (ArrayIndexOutOfBoundException e) {
System.out.println("TypeA");
}
catch (ArithmeticException e) {
System.out.println("TypeB");
}
}
}
Note: Execution command line: $ java exception_handling one two
try without catch or finally
Output:
$ javac exception_handling.java
$ java exception_handling
Main.java:9: error: 'try' without 'catch', 'finally' or resource declarations
What will be the output of the program?
class Exc0 extends Exception { }
class Exc1 extends Exc0 { } /* Line 2 */
public class Test
{
public static void main(String args[])
{
try
{
throw new Exc1(); /* Line 9 */
}
catch (Exc0 e0) /* Line 11 */
{
System.out.println("Ex0 caught");
}
catch (Exception e)
{
System.out.println("exception caught");
}
}
}
An exception Exc1 is thrown and is caught by the catch statement on line 11. The code is
executed in this block. There is no finally block of code to execute.
What is the output of this program?
class exception_handling {
public static void main(String args[]) {
try {
int a[] = {1, 2,3 , 4, 5};
for (int i = 0; i < 5; ++i)
System.out.print(a[i]);
int x = 1/0;
}
catch(ArrayIndexOutOfBoundsException e) {
System.out.print("A");
}
catch(ArithmeticException e) {
System.out.print("B");
}
}
}
There can be more than one catch for a single try block. Here Arithmetic exception
(/ by 0) occurs instead of Array index out of bound exception, so 2nd catch block is
executed.
Output:
$ javac exception_handling.java
$ java exception_handling
12345B
Since sum is declared inside try block and we are trying to access it outside the try
block it results in error.
Output:
$ javac exception_handling.java
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
sum cannot be resolved to a variable
When array index goes out of bound then ArrayIndexOutOfBoundsException exceptio
is thrown by the system.
Output:
$ javac exception_handling.java
$ java exception_handling
123450
The inner try block does not have a catch which can tackle ArrayIndexOutOfBoundException
hence finally is executed which prints 'A' the outer try block does have catch for ArrayIndexOut
OfBoundException exception but no such exception occurs in it
hence its catch is never executed and only 'A' is printed.
Output:
$ javac exception_handling.java
$ java exception_handling
A
Finally clauses are always executed. The program will first execute the try block, printing
Hello world, and will then execute the finally block, printing Finally executing.
Option A, B, and C are incorrect based on the program logic described above. Remember
that either a catch or a finally statement must follow a try. Since the finally is present, the catch
is not required.
What is the output of this program?
class exception_handling {
static void throwexception() throws ArithmeticException {
System.out.print("0");
throw new ArithmeticException ("Exception");
}
public static void main(String args[]) {
try {
throwexception();
}
catch (ArithmeticException e) {
System.out.println("A");
}
}
}
None.
Output:
$ javac exception_handling.java
$ java exception_handling
0A
What will be the output of the program?
public class X
{
public static void main(String [] args)
{
try
{
badMethod(); /* Line 7 */
System.out.print("A");
}
catch (Exception ex) /* Line 10 */
{
System.out.print("B"); /* Line 12 */
}
finally /* Line 14 */
{
System.out.print("C"); /* Line 16 */
}
System.out.print("D"); /* Line 18 */
}
public static void badMethod()
{
throw new RuntimeException();
}
}
(1) A RuntimeException is thrown, this is a subclass of exception.
(2) The exception causes the try to complete abruptly (line 7) therefore line 8 is never executed.
(3) The exception is caught (line 10) and "B" is output (line 12)
(4) The finally block (line 14) is always executed and "C" is output (line 16).
(5) The exception was caught, so the program continues with line 18 and outputs "D".