Bird
0
0

You want to replace all vowels in a String input with '*' and get a new String. Which Kotlin code correctly does this while respecting String immutability?

hard📝 Application Q8 of 15
Kotlin - Data Types
You want to replace all vowels in a String input with '*' and get a new String. Which Kotlin code correctly does this while respecting String immutability?
Ainput.replace(Regex("[aeiouAEIOU]"), "*") println(input)
Bval result = input.replace(Regex("[aeiouAEIOU]"), "*")
Cinput[0] = '*' val result = input
Dinput.replaceAll("[aeiouAEIOU]", "*")
Step-by-Step Solution
Solution:
  1. Step 1: Understand how to replace characters in String immutably

    Use replace() with Regex to create a new String with vowels replaced.
  2. Step 2: Check each option for correctness

    val result = input.replace(Regex("[aeiouAEIOU]"), "*") correctly creates a new String with vowels replaced. input.replace(Regex("[aeiouAEIOU]"), "*") println(input) ignores the new String and prints original. input[0] = '*' val result = input tries to assign to String index (invalid). input.replaceAll("[aeiouAEIOU]", "*") uses non-existent replaceAll method.
  3. Final Answer:

    val result = input.replace(Regex("[aeiouAEIOU]"), "*") -> Option B
  4. Quick Check:

    Use replace with Regex to get new String [OK]
Quick Trick: Use replace with Regex to create new modified String [OK]
Common Mistakes:
MISTAKES
  • Trying to modify String in place
  • Ignoring returned new String from replace()
  • Using non-existent replaceAll method

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes