Bird
0
0

You want to create a mutable set of strings and add elements conditionally. Which Kotlin code correctly does this?

hard📝 Application Q8 of 15
Kotlin - Collections Fundamentals
You want to create a mutable set of strings and add elements conditionally. Which Kotlin code correctly does this?
val fruits = mutableSetOf()
if (true) fruits.add("apple")
if (false) fruits.add("banana")
println(fruits)
A[apple, banana]
B[apple]
C[]
DCompilation error
Step-by-Step Solution
Solution:
  1. Step 1: Analyze conditional adds

    "apple" is added because the condition is true; "banana" is not added because the condition is false.
  2. Step 2: Check final set contents

    The set contains only "apple" and prints as [apple].
  3. Final Answer:

    [apple] -> Option B
  4. Quick Check:

    Conditional adds affect mutable set contents [OK]
Quick Trick: Mutable sets can be empty or grow with add() [OK]
Common Mistakes:
MISTAKES
  • Expecting banana to be added
  • Confusing set print format
  • Thinking code causes error

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes