Bird
0
0

Given the code below, what will be printed?

medium📝 Predict Output Q13 of 15
iOS Swift - Lists and Data Display

Given the code below, what will be printed?

struct User: Identifiable {
    let id: Int
    let name: String
}

let users = [User(id: 1, name: "Anna"), User(id: 2, name: "Ben")]
print(users[1].id)
A1
BAnna
C2
DBen
Step-by-Step Solution
Solution:
  1. Step 1: Identify the second user in the array

    Index 1 refers to the second element, which is User(id: 2, name: "Ben").
  2. Step 2: Access the id property

    The id of this user is 2, so print(users[1].id) prints 2.
  3. Final Answer:

    2 -> Option C
  4. Quick Check:

    Second user's id = 2 [OK]
Quick Trick: Array index 1 is second element, check its id [OK]
Common Mistakes:
  • Confusing index 0 and 1
  • Printing name instead of id
  • Assuming id is name

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More iOS Swift Quizzes