String Methods

AP Computer Science A· difficulty 3/5

String s = "abcdef";
System.out.println(s.substring(1, 7));

What happens?

  • A

    Prints bcdef

  • B

    StringIndexOutOfBoundsException

    check_circle
  • C

    Prints abcdef

  • D

    Prints abcd

Explanation

substring(begin, end) requires end <= length(). Length is 6, so end=7 throws StringIndexOutOfBoundsException.

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

Related questions