Sample questions
Question 1Easy
What does @Binding allow?
- Store data in iCloud
- Create a new state
- A child view to read and write a value owned by a parent
- Observe a class
Answer: C. A child view to read and write a value owned by a parent
Explanation
A binding is a two-way reference to state owned elsewhere, passed with $.
Step-by-step solution
- Parent owns @State.
- Child gets $value.
- Child changes it too.
Why the other options are wrong
- A. Not related.
- B. It references existing state.
- D. It's for two-way value access.
Question 2Easy
What does Spacer() do in an HStack?
- Adds a fixed 8 pt gap
- Hides neighbouring views
- Centres its neighbours
- Expands to fill the available space, pushing other views apart
Answer: D. Expands to fill the available space, pushing other views apart
Explanation
A Spacer takes as much room as it can along the stack's axis.
Step-by-step solution
- Spacer is flexible.
- Uses extra space.
- Pushes siblings to edges.
Why the other options are wrong
- A. It is flexible.
- B. It is invisible but doesn't hide.
- C. It pushes them apart.
Question 3Easy
In SwiftUI, what is a View?
- A UIKit subclass of UIView drawn with Core Graphics
- A protocol describing a piece of UI through its body
- A storyboard file that describes a screen layout
- A wrapper around a single network request
Answer: B. A protocol describing a piece of UI through its body
Explanation
SwiftUI views are lightweight value types that conform to View and return content from body.
Step-by-step solution
- A SwiftUI View is a protocol.
- A conforming type describes its UI in the body property.
- So option B is correct.
Why the other options are wrong
- A. SwiftUI views are not UIView subclasses drawn with Core Graphics.
- C. A storyboard is a separate UIKit file, not a View.
- D. A View does not wrap a network request.
Tip: A SwiftUI view is a struct that returns other views from body.
Question 4Easy
Which SwiftUI container stacks its children on top of each other along the z-axis?
- VStack
- ZStack
- HStack
- LazyVGrid
Answer: B. ZStack
Explanation
ZStack overlaps children, with later ones on top.
Step-by-step solution
- Z axis is depth.
- ZStack overlays.
- Later children are drawn above.
Why the other options are wrong
- A. VStack arranges vertically.
- C. HStack arranges horizontally.
- D. LazyVGrid forms a grid.
Question 5Medium
What does the .task { } modifier do?
- Runs only once ever
- Blocks the main thread
- Runs async work when the view appears and cancels it when the view disappears
- Creates a new view
Answer: C. Runs async work when the view appears and cancels it when the view disappears
Explanation
.task ties an async operation to the view's lifetime.
Step-by-step solution
- Starts on appear.
- Cancels on disappear.
- Good for loading data.
Why the other options are wrong
- A. It runs each time the view appears.
- B. It's async.
- D. It attaches async work.
Question 6Medium
- Stores local view state and re-renders the view when it changes
- Shares data across the entire app for every view
- Persists the data to disk between app launches
- Creates a network session for loading content
Answer: A. Stores local view state and re-renders the view when it changes
Explanation
@State is owned by the view; changing it invalidates the body.
Step-by-step solution
- @State stores a value owned by one view.
- When the value changes, SwiftUI re-runs the view's body.
- So option A is correct.
Why the other options are wrong
- B. @State is local to a view, not shared across the whole app.
- C. @State does not save data between launches.
- D. @State does not create network sessions.
Tip: Use @State for simple local values; use other tools for shared or saved data.
Question 7Medium
Do these two produce the same result?
Text("Hi").padding().background(Color.red)
Text("Hi").background(Color.red).padding()
- No: modifier order matters; the first colours the padded area, the second only the text
- Yes, order never matters
- No: the second fails to compile
- Yes, because both use padding
Answer: A. No: modifier order matters; the first colours the padded area, the second only the text
Explanation
Each modifier returns a new view wrapping the previous one, so order changes what gets coloured.
Step-by-step solution
- padding then background: colour covers padding.
- background then padding: colour only text.
- Different looks.
Why the other options are wrong
- B. Each modifier wraps the previous view.
- C. Both compile.
- D. The background applies to a different view in each.
Question 8Medium
Which stack arranges child views horizontally in SwiftUI?
- VStack
- ZStack
- HStack
- LazyStack
Answer: C. HStack
Explanation
HStack is horizontal, VStack vertical, ZStack overlays views.
Step-by-step solution
- HStack lays out child views in a horizontal row.
- VStack stacks views vertically, and ZStack overlays them in depth.
- SwiftUI has no LazyStack; the lazy stacks are LazyHStack and LazyVStack.
- So the horizontal stack is HStack (option C).
Why the other options are wrong
- A. VStack arranges children vertically, top to bottom.
- B. ZStack layers children on top of each other, not side by side.
- D. SwiftUI has no LazyStack; the lazy variants are LazyHStack and LazyVStack.
Tip: In SwiftUI, H means horizontal, V means vertical and Z means layered (depth).
Question 9Hard
With the Observation framework, how does an @Observable class update a view?
- Every property change refreshes every view
- You must call objectWillChange.send()
- It never updates views
- The view re-renders only when properties it actually read change
Answer: D. The view re-renders only when properties it actually read change
Explanation
@Observable tracks which properties each view body reads and invalidates only affected views.
Step-by-step solution
- Tracks reads in body.
- Change in read property triggers update.
- Fewer redraws.
Why the other options are wrong
- A. Observation tracks reads.
- B. That's the old ObservableObject.
- C. It is built for updates.
Question 10Hard
A SwiftUI view is a struct recreated frequently. How does @State keep its value?
- The struct is never recreated
- State is saved to disk
- It uses a global variable
- SwiftUI stores the state outside the struct and keeps it for the view's lifetime
Answer: D. SwiftUI stores the state outside the struct and keeps it for the view's lifetime
Explanation
@State storage is managed by SwiftUI per view identity, so recreating the struct doesn't reset it.
Step-by-step solution
- View structs are cheap.
- State lives in SwiftUI storage.
- Persists across redraws.
Why the other options are wrong
- A. Views are cheap and recreated often.
- B. It is in memory.
- C. It is managed per view identity.
Practise all 14 questions free
The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.
Open this topic in the app →