Java OCA OCP Practice Question 1050

Question

Which exception will the following throw?

Object obj = new Integer(3); 
String str = (String) obj; 
System.out.println(str); 
  • A. ArrayIndexOutOfBoundsException
  • B. ClassCastException
  • C. IllegalArgumentException
  • D. NumberFormatException
  • E. None of the above.


B.

Note

The second line tries to cast an Integer to a String.

Since String does not extend Integer, this is not allowed and a ClassCastException is thrown.




PreviousNext

Related