Bird
0
0

Which Swift code correctly defines a struct to hold a user's name and age?

easy📝 Syntax Q3 of 15
iOS Swift - User Input and Forms
Which Swift code correctly defines a struct to hold a user's name and age?
Aclass User { var name: String; var age: Int }
Bstruct User { var name: String; var age: Int }
Cstruct User { name: String; age: Int }
Dstruct User { var name; var age }
Step-by-Step Solution
Solution:
  1. Step 1: Check struct syntax

    Swift structs require 'var' or 'let' before properties with type annotations.
  2. Step 2: Compare options

    struct User { var name: String; var age: Int } correctly uses 'var' and types. class User { var name: String; var age: Int } uses class, not struct. Options C and D miss 'var' or types.
  3. Final Answer:

    struct User { var name: String; var age: Int } -> Option B
  4. Quick Check:

    Correct struct syntax = struct User { var name: String; var age: Int } [OK]
Quick Trick: Struct properties need var/let and type [OK]
Common Mistakes:
  • Using class instead of struct
  • Omitting var/let keywords
  • Missing type annotations

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More iOS Swift Quizzes