String Methods

AP Computer Science A· difficulty 3/5

String s = "hi";
char c = s.charAt(2);

What happens when this code runs?

  • A

    StringIndexOutOfBoundsException is thrown

    check_circle
  • B

    It prints 'i'

  • C

    It prints null

  • D

    It assigns ' ' to c

Explanation

Valid indices are 0 to length() - 1; index 2 is out of range for "hi", so a StringIndexOutOfBoundsException is thrown.

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

Related questions