Java Multiple Choice Questions & Answers on Finally & Built in Exceptions for Freshers

1. Which of these clause will be executed even if no exceptions are found?

a) throws
b) finally
c) throw
d) catch
Answer: b

Explanation: finally keyword is used to define a set of instructions that will be executed irrespective of the exception found or not.
2. A single try block must be followed by which of these?

a) finally
b) catch
c) finally & catch
d) None of the mentioned
Answer: c

Explanation: try block can be followed by any of finally or catch block, try block checks for exceptions and work is performed by finally and catch block as per the exception.
3. Which of these packages contain all the Java’s built in exceptions?

a) java.io
b) java.util
c) java.lang
d) java.net
Answer: c
4. Which of these exceptions handles the divide by zero error?

a) ArithmeticException
b) MathException
c) IllegalAccessException
d) IllegarException
Answer: a
5. Which of these exceptions will occur if we try to access the index of an array beyond its length?

a) ArithmeticException
b) ArrayException
c) ArrayIndexException
d) ArrayIndexOutOfBoundsException
Answer: d

Explanation: ArrayIndexOutOfBoundsException is a built in exception that is caused when we try to access an index location which is beyond the length of an array.
6. What is the output of this program?
  1.     class exception_handling {
  2.         public static void main(String args[]) {
  3.             try {
  4.                 int a = args.length;
  5.                 int b = 10 / a;
  6.                 System.out.print(a);
  7.             }
  8.             catch (ArithmeticException e) {
  9.                     System.out.println("1");
  10.             }
  11.         }
  12.     }
a) 0
b) 1
c) Compilation Error
d) Runtime Error

Note : Execution command line : $ java exception_handling
Answer: b
7. What is the output of this program?
  1.     class exception_handling {
  2.         public static void main(String args[]) {
  3.             try {
  4.                 throw new NullPointerException ("Hello");
  5.             }
  6.             catch(ArithmeticException e) {
  7.          System.out.print("B");         
  8.             }
  9.         }
  10.     }
a) A
b) B
c) Compilation Error
d) Runtime Error
Answer: d

Explanation: try block is throwing NullPointerException but the catch block is used to counter Arithmetic Exception. Hence NullPointerException occurs since no catch is there which can handle it, runtime error occurs.
8. What is the output of this program?
  1.     class exception_handling {
  2.             static void throwexception() throws ArithmeticException {        
  3.                 System.out.print("0");
  4.                 throw new ArithmeticException ("Exception");
  5.             }
  6.             public static void main(String args[]) {
  7.             try {
  8.                 throwexception();
  9.             }
  10.             catch (ArithmeticException e) {
  11.                     System.out.println("A");
  12.             }
  13.         }
  14.     }
a) A
b) 0
c) 0A
d) Exception
Answer: c
9. What is the output of this program?
  1. class exception_handling 
  2.         {
  3.             public static void main(String args[])
  4.             {
  5.                 try 
  6.                 {
  7.                     int a = 1;
  8.                     int b = 10 / a;
  9.                     try 
  10.                     {
  11.                          if (a == 1)
  12.                              a = a / a - a;
  13.                          if (a == 2) 
  14.                          {
  15.                              int c[] = {1};
  16.                              c[8] = 9;
  17.                          }
  18.                     }
  19.                     finally 
  20.                     {
  21.                         System.out.print("A");
  22.                     }
  23.                 }
  24.                 catch (Exception e) 
  25.                 {
  26.                         System.out.println("B");
  27.                 }
  28.             }
  29.         }
a) A
b) B
c) AB
d) BA
Answer:a

Explanation: 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 ArrayIndexOutOfBoundException exception but no such exception occurs in it hence its catch is never executed and only ‘A’ is printed.
10. What is the output of this program?
  1.     class exception_handling {
  2.         public static void main(String args[]) {
  3.             try {
  4.                 int a = args.length;
  5.                 int b = 10 / a;
  6.                 System.out.print(a);
  7.                 try {
  8.                      if (a == 1)
  9.                          a = a / a - a;
  10.                      if (a == 2) {
  11.                          int []c = {1};
  12.                          c[8] = 9;
  13.                      }
  14.                 }
  15.                 catch (ArrayIndexOutOfBoundException e) {
  16.                     System.out.println("TypeA");
  17.             }
  18.             catch (ArithmeticException e) {
  19.                     System.out.println("TypeB");
  20.             }
  21.         }
  22.     }
a) TypeA
b) TypeB
c) Compilation Error
d) Runtime Error

Note: Execution command line: $ java exception_handling one two
Answer: c

Explanation: try without catch or finally

Related

Multiple Choice Questions 8154259127745271009

Post a Comment

emo-but-icon

item