Computer Applications

Given the code :

String s = new String("abc");

Which of the following calls are invalid ?

  1. s.trim( )
  2. s.replace('a', 'A')
  3. s.substring(3)
  4. s.toUpperCase( )
  5. s.setCharAt(1, 'A')

Java String Handling

7 Likes

Answer

s.setCharAt(1, 'A')

Reason — setCharAt() is a method of StringBuffer class. It cannot be used with a String object, which is immutable.

Answered By

4 Likes


Related Questions