String s = "hi";
char c = s.charAt(2);What happens when this code runs?
- Acheck_circle
StringIndexOutOfBoundsException is thrown
- 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.