Java Exception Handling MCQ-2

Q1) What are the different methods that can be used to get the exception message?

a) printStackTrace()
b) toString()
c) getMessage()
d) All of these

View Answer Answer:- d) All of these

We can use printStackTrace(), toString(), and getMessage() to get the exception message. See More:- Different Ways to Get Exception Message in Java

Q2) The below exception message is given by?

Exception in thread "main" java.lang.ArithmeticException: / by zero
at Test.main(Test.java:3)

a) JDH (Java Default Handler) or JVM
b) printStackTrace()
c) toString()
d) getMessage()

View Answer Answer:- a) JDH (Java Default Handler) or JVM

Q3) Find the output of the below program?

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

a) / by zero
b) java.lang.ArithmeticException: / by zero
c) java.lang.ArithmeticException: / by zero; at Test.main(Test.java:4)
d) None of these

View Answer Answer:- b) java.lang.ArithmeticException: / by zero

The toString() method display the exception in the given format:- Exception name: <reason-of-the-exception>

Q4) Which exception class can be used to handle all types of exceptions?

a) Exception
b) Error
c) RuntimeException
d) Anyone from these options

View Answer Answer:- a) Exception

To catch all exceptions in Java we should use the “Exception” class. See More:- How to catch All Exceptions in Java

Q5) Which exception will be raised?

int arr[] = new int[5];
System.out.println(arr[20]);

a) ArithmeticException
b) NegativeArraySizeException
c) ArrayIndexOutOfBoundsException
d) Compile time error

View Answer Answer:- c) ArrayIndexOutOfBoundsException

The array contains only 5 elements but we are trying to access the 20th element, hence it raises ArrayIndexOutOfBoundsException.

Q6) Which exception will be raised?

int arr[] = new int[5];
System.out.println(arr[-2]);

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

View Answer Answer:- c) ArrayIndexOutOfBoundsException

The array contains 5 elements and their index is from 0 to 4 but we are trying to access the -2th element which is invalid, hence it raises ArrayIndexOutOfBoundsException.

Q7) Which exception will be raised?

int arr[] = new int[-5];
System.out.println(arr[1]);

a) ArithmeticException
b) NegativeArraySizeException
c) ArrayIndexOutOfBoundsException
d) Compile-time error

View Answer Answer:- b) NegativeArraySizeException

The array size must be a valid positive integer number.

Q8) Which exception will be raised?

Object obj = new Object();
String str = (String)obj;

a) ClassCastException
b) ArithmeticException
c) NoClassDefFoundError
d) Exception will not be raised

View Answer Answer:- a) ClassCastException

Parent class object can’t convert to the child class object directly. In this case we will get:- Exception in thread “main” java.lang.ClassCastException: class java.lang.Object cannot be cast to class java.lang.String (java.lang.Object and java.lang.String are in module java.base of loader ‘bootstrap’)

Q9) Which exception will be raised?

String str = new String("KnowProgram");
Object obj = (Object)str;

a) ArithmeticException
b) NoClassDefFoundError
c) ClassCastException
d) Exception will not be raised

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

Child class objects can be converted to the parent class object type therefore exceptions will not be raised in this program.

Q10) Which exception will be raised?

Object obj = new String("KnowProgram");
String str = (String)obj;

a) ClassCastException
b) ArithmeticException
c) NoClassDefFoundError
d) Exception will not be raised

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

The object is of parent type but internally it holds the child type object, but not the parent type type object. Therefore it can be converted to the child type. Hence, exceptions will not be raised.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or 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 *