Bird
0
0

What will this Swift code print?

medium📝 query result Q5 of 15
Swift - Loops
What will this Swift code print?
let fruits = ["apple", "banana", "cherry", "date"]
for fruit in fruits where fruit.contains("a") {
    print(fruit)
}
Abanana cherry
Bcherry date
Capple cherry date
Dapple banana date
Step-by-Step Solution
Solution:
  1. Step 1: Understand the where condition

    The condition fruit.contains("a") filters fruits containing letter 'a'.
  2. Step 2: Check which fruits contain 'a'

    "apple", "banana", and "date" contain 'a'; "cherry" does not.
  3. Final Answer:

    apple banana date -> Option D
  4. Quick Check:

    Filter fruits containing 'a' = apple, banana, date [OK]
Quick Trick: Use contains in where to filter strings [OK]
Common Mistakes:
  • Including fruits without 'a'
  • Ignoring where clause
  • Confusing contains with startsWith

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes