Multiple choice

What will be the output of given code?

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

  1. s.length() = 14s.charAt(9) = T

  2. s.length() = 15s.charAt(9) = o

  3. s.length() = 15s.charAt(9) = T

  4. s.length() = 14s.charAt(9) = o

  5. Compiler Error

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

This is wrong. Length() function returns the number of characters in the string. As we can count that the number of characters in the given string are 15. So, length() will return 15 not 14. charAt() function returns the character at the given index in the parameter. Remember that the index count starts at 0. So, charAt(0) is W, counting in this way we reach charAt(9) is o not T.  Hence, this is the correct answer.