Computer Applications
The output of the statement "CONCENTRATION".indexOf('T') is:
- 9
- 7
- 6
- (-1)
Answer
6
Reason — The method indexOf(char ch)
in Java returns the index of the first occurrence of the specified character ch
in the string. If the character is not found, it returns -1
.
Consider the string:
"CONCENTRATION"
Let’s count the characters by their index (Java uses zero-based indexing):
C | O | N | C | E | N | T | R | A | T | I | O | N |
---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 |
The first occurrence of the character 'T' is at index 6.
Thus, the output of the statement "CONCENTRATION".indexOf('T') is 6.
Related Questions
Consider the below picture and choose the correct statement from the following:
- Polygon is the object and the pictures are classes
- Both polygon and the pictures are classes
- Polygon is the class and the pictures are objects
- Both polygon and the pictures are objects
int x = 98; char ch = (char)x; what is the value in ch?
- b
- A
- B
- 97
The access specifier that gives least accessibility is:
- package
- public
- protected
- private
The output of the statement "talent".compareTo("genius") is:
- 11
- –11
- 0
- 13