Computer Applications
What will be the value stored in the variable 'c' if the following statement is executed?
c = "COMPUTER".charAt("COMPUTER ".indexOf('P'))
- 3
- 4
- P
- M
Java String Handling
32 Likes
Answer
P
Reason — charAt(int index) returns a character from the given index of the string and indexOf(char ch) returns the index of first occurrence of a character in the string.
Since P is at 3rd position so indexOf('P') will return 3 and charAt(3) function will return the character at the third index i.e., 'P'. So, the given statement is solved as follows:
c = "COMPUTER".charAt("COMPUTER ".indexOf('P'))
⇒ c = "COMPUTER".charAt(3)
⇒ c = 'P'
Answered By
17 Likes
Related Questions
A string internally creates:
- An integer array
- A numeric array
- A character array
- A double type array
If a string contains 12 characters, what will be the index of the last character?
- 12
- 11
- 10
- 0
Which of the following functions is used to remove leading and trailing white spaces from the string?
- trim( )
- trail( )
- truncate( )
- slice( )
What will be the output of the following program snippet?
s1 = "COMPUTER";
s2 = "computer";
System.out.println(s1.equals(s2));- True
- False
- 0
- 1