Creating References Using Inheritance Hierarchies

AP Computer Science A· difficulty 3/5

Object o = "hello";
Integer i = (Integer) o;

What happens?

  • A

    i is 0

  • B

    Compile-time error

  • C

    ClassCastException at runtime

    check_circle
  • D

    i is null

Explanation

Cast compiles (Object can be Integer in principle) but the actual object is a String, so a ClassCastException is thrown at runtime.

Want 10 more like this — adaptive to your weak spots?

Related questions