Sail E0 Webinar

MCQs

Total Questions : 10
Question 1.


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


  1.    TypeA
  2.    TypeB
  3.    0TypeA
  4.    0TypeB
 Discuss Question
Answer: Option C. -> 0TypeA

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


Question 2.


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


  1.    TypeA
  2.    TypeB
  3.    0TypeA
  4.    0TypeB
 Discuss Question
Answer: Option C. -> 0TypeA

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


Question 3.


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");
}
}
}
  1.    A
  2.    0
  3.    0A
  4.    Exception
 Discuss Question
Answer: Option C. -> 0A

None.
Output:
$ javac exception_handling.java
$ java exception_handling
0A


Question 4.


What is the output of this program?


class exception_handling {
public static void main(String args[]) {
try {
throw new NullPointerException ("Hello");
System.out.print("A");
}
catch(ArithmeticException e) {
System.out.print("B");
}
}
}
  1.    A
  2.    B
  3.    Hello
  4.    Runtime Error
 Discuss Question
Answer: Option B. -> B

None.
Output:
$ javac exception_handling.java
$ java exception_handling
B


Question 5.


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


  1.    TypeA
  2.    TypeB
  3.    0TypeA
  4.    0TypeB
 Discuss Question
Answer: Option B. -> TypeB

None.
Output:
$ javac exception_handling.java
$ java exception_handling
TypeB


Question 6.

Which of these keywords is used to by the calling function to guard against the 

exception that is thrown by called function?


  1.    try
  2.    throw
  3.    throws
  4.    catch
 Discuss Question
Answer: Option C. -> throws

If a method is capable of causing an exception that it does not handle. It must 

Question 7.

Which of these operator is used to generate an instance of an exception than can 

be thrown by using throw?


  1.    new
  2.    malloc
  3.    alloc
  4.    thrown
 Discuss Question
Answer: Option A. -> new

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.



Question 8.

Which of these class is related to all the exceptions that are explicitly thrown?


  1.    Error
  2.    Exception
  3.    Throwable
  4.    Throw
 Discuss Question
Answer: Option C. -> Throwable

None.


Question 9.

Which of these keywords is used to generate an exception explicitly?


  1.    try
  2.    finally
  3.    throw
  4.    catch
 Discuss Question
Answer: Option C. -> throw

None.


Question 10.

Which of these handles the exception when no catch is used?


  1.    Default handler
  2.    finally
  3.    throw handler
  4.    Java run time system
 Discuss Question
Answer: Option A. -> Default handler

None.


Latest Videos

Latest Test Papers