KnowledgeBoat Logo

Computer Applications

Give output of the following String methods:

"SUCESS".indexOf('S') + "SUCCESS".lastIndexOf('S')

  1. 0
  2. 5
  3. 6
  4. -5

Java String Handling

ICSE 2022

11 Likes

Answer

6

Reason — indexOf() returns the index of the first occurrence of the specified character within the string and lastIndexOf() returns the index of the last occurrence of the specified character within the string. Since a string begins with index 0, the given methods are evaluated as follows:

"SUCESS".indexOf('S') + "SUCCESS".lastIndexOf('S')
⇒ 0 + 6
⇒ 6

Answered By

6 Likes


Related Questions