Multiple choice

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

What will be the output of the given code?

  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 the correct answer. Length() function return 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.charAt() function return 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.So this is the correct answer.