Menu
Topics Index
...
`

Test Exceptions


If you need explanation Read this topic

Test Exceptions
What will be the output of the following program?
public class TestExceptions {
    public static void main(String[] args) {
        for (int i = 0; i < 3; i++) {
            try {
                int a[] = new int[i];
                a[2] = a[i - 1] / i * a[1] / 0;
            } catch (Exception e) {
                System.out.println(e);
            }
        }
    }
}

A. java.lang.ArithmeticException: / by zero
java.lang.ArithmeticException: / by zero
java.lang.ArithmeticException: / by zero
B. java.lang.ArrayIndexOutOfBoundsException: 0
java.lang.ArrayIndexOutOfBoundsException: 1
java.lang.ArrayIndexOutOfBoundsException: 2
C. java.lang.ArrayIndexOutOfBoundsException: -1
java.lang.ArrayIndexOutOfBoundsException: 1
java.lang.ArithmeticException: / by zero
D. Some other output or Compilation Error
Topic: Difference Between Error and Exception in Java

If you need explanation Read this topic

User comments below. All of them might not be correct.

Answer is c, because in try block, first exception will occur when executing a[i-1] because for the first time i =0 i.e;a[-1] which is ArrayIndexOutofBounsException:-1.Then exception will raise at a[1] because while declaring array its size is given as 0.Then lastly Arithmetic Exception / by zero

Posted by Sandeep Ega    2013-10-25 04:33:51


Congratulations Sandeep Ega. You are this dose winner. We will send you the link using which you can claim your recharge.

Posted by Merit Campus    2013-10-28 09:19:31


© meritcampus 2019

All Rights Reserved.

Open In App