What is the result?

Given:

public class x{
public static void main (string [] args){
String theString = “Hello World”;
System.out.println(theString.charAt(11));
}
}

What is the result?

Given:

public class x{
public static void main (string [] args){
String theString = “Hello World”;
System.out.println(theString.charAt(11));
}
}

What is the result?

A.
There is no output

B.
d is output

C.
A StringIndexOutOfBoundsException is thrown at runtime

D.
An ArrayIndexOutOfBoundsException is thrown at runtime

E.
A NullPointException is thrown at runtime

F.
A StringArrayIndexOutOfBoundsException is thrown at runtime

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” java.lang.StringIndexOutOfBoundsException: String index out of range:



Leave a Reply 2

Your email address will not be published. Required fields are marked *


Abed Abu Alhalawa

Abed Abu Alhalawa

C

James

James

The Answer is C.

the charAt(int index) method of a String object will throw a StringIndexOutOfBoundsException if the index argument of the method is less than 0 or greater than or equal to the length in characters of the String.

Note: the length() method returns the length of a String. The length instance variable equals the number of elements in an Array.