MCQ Single Best Answer Easy

QGive the output of the following string methods: "MISSISSIPPI".indexOf('S') + "MISSISSIPPI".lastIndexOf('I')

ID: #22221 Introduction in String Java 75 views
Question Info
#22221Q ID
EasyDifficulty
Introduction in String JavaTopic

Choose the Best Option

Click any option to instantly check if you're correct.

  • A 10
  • B 12
  • C 20
  • D 11
Correct Answer: Option B

Explanation

Let's break down the two string method operations in Java for the string "MISSISSIPPI":

1. "MISSISSIPPI".indexOf('S')

The indexOf('S') method returns the index of the first occurrence of the character 'S' in the string "MISSISSIPPI".

  • The string "MISSISSIPPI" has the following indices:

    
    M  I  S  S  I  S  S  I  P  P  I
    0  1  2  3  4  5  6  7  8  9 10
    
    
  • The first occurrence of 'S' is at index 2.

So, "MISSISSIPPI".indexOf('S') returns 2.

 

2. "MISSISSIPPI".lastIndexOf('I')

The lastIndexOf('I') method returns the index of the last occurrence of the character 'I' in the string "MISSISSIPPI".

  • From the same index mapping, the last occurrence of 'I' is at index 10.

So, "MISSISSIPPI".lastIndexOf('I') returns 10.

Addition Operation

Now, adding these two results together:


indexOf('S') + lastIndexOf('I')
= 2 + 10
= 12

So, the correct answer is:

12

Share This Question

Challenge a friend or share with your study group.