Java Exception Handling MCQ-3

Q1) Which exception will be raised?

public class Test {
   public static void main(String[] args) {
      m1();
   }
   public static void m1() {
      m2();
   }
   public static void m2() {
      m1();
   }
}

a) StackOverflowError
b) OutOfMemoryError
c) UnknownError
d) VirtualMachineError

View Answer Answer:- a) StackOverflowError

In Java, every thread keeps a record of method calls inside the stack. In this program, the m1() method is called the m2() method and the m2() method is called m1() method therefore StackOverflowError will be raised.

Q2) Which exception will be raised?

public class Test {
   static int x = 9/0;
   public static void main(String[] args) {
      System.out.println(x);
   }
}

a) ArithmeticException
b) ExceptionInInitializerError
c) OutOfMemoryError
d) Exception will not be raised

View Answer Answer:- b) ExceptionInInitializerError

Any exception raised in the static variable initialization or static block leads to ExceptionInInitializerError.

Q3) Which exception will be raised?

public class Test {
   static int arr[] = new int[-5];
   public static void main(String[] args) {
      System.out.println(arr);
   }
}

a) ArithmeticException
b) OutOfMemoryError
c) ExceptionInInitializerError
d) Exception will not be raised

View Answer Answer:- c) ExceptionInInitializerError

Any exception raised in the static variable initialization or static block leads to ExceptionInInitializerError.

Q4) Which exception will be raised?

public class Test {
   static int arr[] = new int[5];
   static {
      System.out.println(arr[10]);
   }
   public static void main(String[] args) {
      System.out.println(arr);
   }
}

a) ArithmeticException
b) OutOfMemoryError
c) ExceptionInInitializerError
d) Exception will not be raised

View Answer Answer:- c) ExceptionInInitializerError

Any exception raised in the static variable initialization or static block leads to ExceptionInInitializerError.

Q5) Which exception will be raised?

public class Test {
   int arr[] = new int[-5];
   public static void main(String[] args) {
      System.out.println("A");
   }
}

a) ArithmeticException
b) NegativeArraySizeException
c) ExceptionInInitializerError
d) Exception will not be raised

View Answer Answer:- d) Exception will not be raised

The array is instance type but not static, therefore ExceptionInitializerError will never be raised. In the array, size is -5 which is invalid therefore it should raise a NegativeArraySizeException, but the instance variable will be initialized only when we create an object. Hence, in this program exceptions will not be raised, the program runs successfully and gives output:- “A”.

Q6) Which exception will be raised?

public class Test {
   int arr[] = new int[-5];
   public static void main(String[] args) {
      Test t1 = new Test();
      System.out.println("A");
   }
}

a) ArithmeticException
b) NegativeArraySizeException
c) ExceptionInInitializerError
d) Exception will not be raised

View Answer Answer:- b) NegativeArraySizeException

In this program we are creating an object of Test class therefore the instance variable will be initialized and NegativeArraySizeException will be raised.

Q7) Which exception will be raised?

public class Test {
   public static void main(String[] args) {
      try {
         System.out.println(9/0);
      } catch(ArithmeticException e) {
         throw new NullPointerException();
      }
   }
}

a) ArithmeticException
b) NullPointerException
c) Compile-time error
d) Compiled successfully, Exception will not be raised, but no output

View Answer Answer:- b) NullPointerException

In this program, the statement inside the try block raises ArithmeticException. The catch block handled this exception and raised the NullPointerException. Therefore JVM will raise NullPointerException, but not ArithmeticException.

Q8) Which exception will be raised?

public class Test {
   public static void main(String[] args) {
      try {
         System.out.println(9/0);
      } catch(ArithmeticException e) {
         throw new StackOverflowError();
      }
   }
}

a) ArithmeticException
b) StackOverflowError
c) OutOfMemoryError
d) VirtualMachineError

View Answer Answer:- b) StackOverflowError

The try block raised ArithmeticException, the catch block handled it and threw StackOverflowError.

Q9) Which exception will be raised?

public class Test {
   public static void main(String[] args) {
      try {
         Integer.parseInt("two");
      } catch(ArithmeticException e) {
         throw new NullPointerException();
      }
   }
}

a) ArithmeticException
b) NullPointerException
c) NumberFormatException
d) Compiled successfully, Exception will not be raised, but no output

View Answer Answer:- c) NumberFormatException

The try block raised a NumberFormatException, but the catch block is only handling ArithmeticException. Hence, NumberFormatException will be raised.

Q10) Which exception will be raised?

public class Test {
   static {
      String s = null;
      System.out.println(s.length());
   }
   public static void main(String[] args) { }
}

a) ArithmeticException
b) NegativeArraySizeException
c) ExceptionInInitializerError
d) Exception will not be raised

View Answer Answer:- c) ExceptionInInitializerError

Any exception raised in the static variable initialization or static block leads to ExceptionInInitializerError.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

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