Bird
0
0

What will be the output of the following Swift code?

medium📝 Predict Output Q13 of 15
Swift - Collections
What will be the output of the following Swift code?
var fruits = ["Apple", "Banana", "Cherry"]
fruits.append("Date")
fruits.remove(at: 1)
print(fruits)
A["Banana", "Cherry", "Date"]
B["Apple", "Banana", "Cherry", "Date"]
C["Apple", "Cherry", "Date"]
D["Apple", "Date"]
Step-by-Step Solution
Solution:
  1. Step 1: Append "Date" to the fruits array

    After append, fruits = ["Apple", "Banana", "Cherry", "Date"].
  2. Step 2: Remove element at index 1

    Index 1 is "Banana", so removing it results in ["Apple", "Cherry", "Date"].
  3. Final Answer:

    ["Apple", "Cherry", "Date"] -> Option C
  4. Quick Check:

    append + remove(at: 1) = ["Apple", "Cherry", "Date"] [OK]
Quick Trick: Append adds end; remove(at:) deletes by index [OK]
Common Mistakes:
  • Removing wrong index
  • Forgetting append adds element
  • Confusing remove(at:) with removeLast()

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes