Bird
0
0

Given the string input = "apple,banana,orange", which code snippet correctly replaces all commas with semicolons and converts the result to uppercase?

hard📝 Application Q15 of 15
Java - Strings and String Handling
Given the string input = "apple,banana,orange", which code snippet correctly replaces all commas with semicolons and converts the result to uppercase?
Ainput.replaceFirst(",", ";").toUpperCase();
Binput.toUpperCase().replaceFirst(",", ";");
Cinput.replace(",", ";").toUpper();
Dinput.replaceAll(",", ";").toUpperCase();
Step-by-Step Solution
Solution:
  1. Step 1: Understand replace() vs replaceAll()

    Both replace() and replaceAll() can replace characters, but replaceAll() uses regex and is safer for multiple replacements.
  2. Step 2: Check method chaining and correctness

    A and B use replaceFirst() which only replaces the first comma. C uses invalid toUpper().
  3. Step 3: Choose best practice

    Using replaceAll() with chaining to toUpperCase() is correct and clear.
  4. Final Answer:

    input.replaceAll(",", ";").toUpperCase(); -> Option D
  5. Quick Check:

    Replace commas then uppercase = replaceAll + toUpperCase() [OK]
Quick Trick: Chain replaceAll() then toUpperCase() for clean transformation [OK]
Common Mistakes:
  • Using toUpper() instead of toUpperCase()
  • Using replaceFirst() which only replaces the first occurrence
  • Confusing replace() and replaceAll() usage

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Java Quizzes