Multiple choice

String s = “Welcome To Java”; s.charAt(s.length());

What will be the result of the above code?

  1. a

  2. v

  3. compiler error : cannot pass a function as parameter

  4. compiler error : parameters not sufficient

  5. runtime error

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

This is the correct answer. S.length() wil return 15 because there are 15 characters in the String.So the code looks like this :-s.charAt(15);the index count starts at 0. In this way the last character a is at index number 14. So if we try to access a character in a string which is out of bound then a runtime exception “StringOutOfBound” will be thrown.So we can pass only till s.charAt(s.length()-1).So this is correct answer.