Computer Applications

What will be the value stored in the variable 'c' if the following statement is executed?
c = "COMPUTER".charAt("COMPUTER ".indexOf('P'))

  1. 3
  2. 4
  3. P
  4. 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