Casting and Ranges of Variables

AP Computer Science A· difficulty 3/5

Object o = "hello";
int len = o.length();

What is wrong?

  • A

    o cannot reference a String

  • B

    Nothing is wrong

  • C

    Strings have no length method

  • D

    Object has no length(); cast o to String first

    check_circle

Explanation

The compile-time type of o is Object, which does not declare length(). Casting ((String) o).length() resolves the call.

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

Related questions