Bird
0
0

Given this code:

hard📝 Application Q8 of 15
Java - Strings and String Handling
Given this code:
String s = "hello";
s = s.toUpperCase();
System.out.println(s);

What does this demonstrate about strings in Java?
AStrings are immutable; methods return new strings
BtoUpperCase() modifies the original string
CStrings are mutable and change in place
DtoUpperCase() throws an exception on literals
Step-by-Step Solution
Solution:
  1. Step 1: Recognize string immutability

    Original string "hello" is not changed; toUpperCase() returns a new string.
  2. Step 2: Understand assignment to s

    s now points to the new uppercase string, demonstrating immutability.
  3. Final Answer:

    Strings are immutable; methods return new strings -> Option A
  4. Quick Check:

    String methods return new objects [OK]
Quick Trick: String methods return new strings; originals stay unchanged [OK]
Common Mistakes:
  • Assuming strings change in place
  • Expecting toUpperCase() to modify original string
  • Confusing immutability with mutability

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Java Quizzes