Multiple choice

What is the output of the given program code?

public class ScopeTest
{
int z;
public static void main(String[] args)
{
String theString = "Hello World";
System.out.println(theString.charAt(11));
}
}

  1. Blank output screen

  2. StringIndexOutofBound exception

  3. ArrayIndexOutofBound exception

  4. ‘\0’ will be printed

  5. Compilation fails

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

There are only 11 characters in the string hello world. The code theString.charAt(11) retrieves the 12th character, which does not exist. A StringIndexOutOfBoundsException is thrown. Exception in thread main is java.lang. StringIndexOutOfBoundsException. So, this answer is correct.