Multiple choice

Consider the following code snippet. What will be assigned to the variable fourthChar, if the code is executed? String str = new String(“Java”); char fourthChar = str.charAt(4);

  1. a

  2. v

  3. throws StringIndexOutofBoundsException

  4. null characater

Reveal answer Fill a bubble to check yourself
C Correct answer
Explanation

Strings are zero-indexed. 'Java' has indices 0-3 (J=0, a=1, v=2, a=3). charAt(4) attempts to access index 4 which doesn't exist, throwing StringIndexOutOfBoundsException.