Bird
0
0

Examine the error in this code:

medium📝 Debug Q7 of 15
Java - Strings and String Handling
Examine the error in this code:
String s = "example";
s = s.toLowerCase();
s.charAt(2) = 'A';
AYou cannot assign a character to s.charAt(2) because charAt() returns a value, not a variable
BtoLowerCase() does not return a new string
CcharAt() can be used to set characters in a string
DThe code correctly changes the character at index 2
Step-by-Step Solution
Solution:
  1. Step 1: Understand charAt()

    charAt() returns a char value, not a reference that can be assigned to.
  2. Step 2: String immutability

    Strings are immutable, so individual characters cannot be changed directly.
  3. Final Answer:

    You cannot assign a character to s.charAt(2) because charAt() returns a value, not a variable -> Option A
  4. Quick Check:

    charAt() is read-only, cannot assign [OK]
Quick Trick: charAt() returns char value, not assignable [OK]
Common Mistakes:
  • Trying to assign a value to charAt()
  • Assuming toLowerCase() modifies original string
  • Confusing charAt() with setCharAt() (which does not exist)

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Java Quizzes