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 returns the number of characters in the string. The number of characters in the given string is 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.