SwiftUI Basics Questions with Answers

14 practice questions on SwiftUI Basics (Swift) with answers and step-by-step solutions. Try 10 samples below, then practise all 14 free in the AptiNest app.

Sample questions

Question 1Easy

What does @Binding allow?

  1. Store data in iCloud
  2. Create a new state
  3. A child view to read and write a value owned by a parent
  4. 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

  1. Parent owns @State.
  2. Child gets $value.
  3. 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?

  1. Adds a fixed 8 pt gap
  2. Hides neighbouring views
  3. Centres its neighbours
  4. 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

  1. Spacer is flexible.
  2. Uses extra space.
  3. 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?

  1. A UIKit subclass of UIView drawn with Core Graphics
  2. A protocol describing a piece of UI through its body
  3. A storyboard file that describes a screen layout
  4. 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

  1. A SwiftUI View is a protocol.
  2. A conforming type describes its UI in the body property.
  3. 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?

  1. VStack
  2. ZStack
  3. HStack
  4. LazyVGrid

Answer: B. ZStack

Explanation

ZStack overlaps children, with later ones on top.

Step-by-step solution

  1. Z axis is depth.
  2. ZStack overlays.
  3. 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?

  1. Runs only once ever
  2. Blocks the main thread
  3. Runs async work when the view appears and cancels it when the view disappears
  4. 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

  1. Starts on appear.
  2. Cancels on disappear.
  3. 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

What does @State do?

  1. Stores local view state and re-renders the view when it changes
  2. Shares data across the entire app for every view
  3. Persists the data to disk between app launches
  4. 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

  1. @State stores a value owned by one view.
  2. When the value changes, SwiftUI re-runs the view's body.
  3. 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()
  1. No: modifier order matters; the first colours the padded area, the second only the text
  2. Yes, order never matters
  3. No: the second fails to compile
  4. 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

  1. padding then background: colour covers padding.
  2. background then padding: colour only text.
  3. 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?

  1. VStack
  2. ZStack
  3. HStack
  4. LazyStack

Answer: C. HStack

Explanation

HStack is horizontal, VStack vertical, ZStack overlays views.

Step-by-step solution

  1. HStack lays out child views in a horizontal row.
  2. VStack stacks views vertically, and ZStack overlays them in depth.
  3. SwiftUI has no LazyStack; the lazy stacks are LazyHStack and LazyVStack.
  4. 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?

  1. Every property change refreshes every view
  2. You must call objectWillChange.send()
  3. It never updates views
  4. 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

  1. Tracks reads in body.
  2. Change in read property triggers update.
  3. 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?

  1. The struct is never recreated
  2. State is saved to disk
  3. It uses a global variable
  4. 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

  1. View structs are cheap.
  2. State lives in SwiftUI storage.
  3. 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 →

Related topics

See all Swift topics · All topics