What is the result?

Given:

public class Test {

void display(String[] arr) {

try {

System.out.print(arr[2]);

} catch(ArrayIndexOutOfBoundsException |

NullPointerException e) {

e = new Exception();

throw e;

}

}

public static void main(String[] args) throws Exception {

try {

String[] arr = {“Unix”,”Solaris”,null};

new Test().display(arr);

} catch(Exception e) {

System.err.print(e.getClass());

}

}

}

What is the result?

Given:

public class Test {

void display(String[] arr) {

try {

System.out.print(arr[2]);

} catch(ArrayIndexOutOfBoundsException |

NullPointerException e) {

e = new Exception();

throw e;

}

}

public static void main(String[] args) throws Exception {

try {

String[] arr = {“Unix”,”Solaris”,null};

new Test().display(arr);

} catch(Exception e) {

System.err.print(e.getClass());

}

}

}

What is the result?

A.
Null

B.
class java.lang.ArraylndexOutOfBoundsException

C.
class java.lang.NullPointerException

D.
class java.lang.Exception

E.
Compilation fails.

Explanation:
error: incompatible types
e = new Exception();
required: RuntimeException
found: Exception



Leave a Reply 6

Your email address will not be published. Required fields are marked *


Charlie

Charlie

Type mismatch: cannot convert from Exception to RuntimeException
&
The parameter e of a multi-catch block cannot be assigned

gelete

gelete

+1

E.

e = new Exception();
“Type mismatch: cannot convert from Exception to RuntimeException”

e = new ArrayIndexOutOfBoundsException();
With a RuntimeException the compiler says:
“The parameter e of a multi-catch block cannot be assigned”

Tim

Tim

E. If the line “e = new Exception();” is remarked, the answer would be A.

Dylan

Dylan

E, can’t re-assign multicatch parameter, and even if you could, RuntimeException reference e is not compatable with Exception object.