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 a parameter

  4. Compiler Error : parameters are 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 bounds then a runtime exception “StringOutOfBound” will be thrown. So we can pass only until s.charAt(s.length()-1).  So this is a correct answer.