MCQs
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
Execution command line is "$ java exception_ handling one two" hence there are tro
input making args.length = 2, hence "c[8] = 9" in second try block is executing which
throws ArrayIndexOutOfBoundException which is caught by catch of nested try block.
Hence 0TypeB is printed
Output:
$ javac exception_handling.java
$ java exception_handling
0TypeB
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 commandline : $ java exception_handling one
Execution command line is "$ java exception_handling one" hence there is only single
string that is in args array, making its length 1, hence "a = a/ a - a" in second try block
is executing which throws arithmeticexception which is caught by catch of firts try block
as the nested try block does not have a catch block which can detect ArithmeticException.
Hence 0TypeA is printed
Output:
$ javac exception_handling.java
$ java exception_handling
0TypeA
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
None.
Output:
$ javac exception_handling.java
$ java exception_handling
B
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
None.
Output:
$ javac exception_handling.java
$ java exception_handling
TypeB
If a method is capable of causing an exception that it does not handle. It must
new is used to create instance of an exception. All of java's built in run-time
exceptions have two constructors : one with no parameters and one that
takes a string parameter.
None.
None.
None.