Explore SwiftUI Basics: 30 MCQs for iOS Developers
SwiftUI revolutionizes app development with declarative design, powerful state management, and seamless integration of MVVM architecture. These 30 MCQs help you master SwiftUI’s core concepts, views, state, and bindings.
Topic 1: Introduction to SwiftUI
What is SwiftUI? a) A framework for writing UI code in a declarative way b) A tool for designing UI components visually c) A low-level UI framework d) A framework for building UIKit-based apps
Which of the following is NOT part of SwiftUI? a) Views b) States c) Bindings d) Storyboards
What is the default container in SwiftUI for arranging views? a) VStack b) ZStack c) HStack d) List
What is the role of the @State property wrapper in SwiftUI? a) To create a binding between a view and a model b) To store data that can change during the view’s lifecycle c) To modify the layout of views d) To store constant values
Which of these elements does NOT need to be explicitly created in SwiftUI? a) Views b) Layout containers c) Declarative statements d) UIKit components
How do you declare a SwiftUI view? a) By using a class that inherits from UIView b) By using a struct that conforms to View c) By using a protocol d) By using a function with a return type of UIView
What is the main benefit of using SwiftUI for UI development? a) It allows manual layout control b) It makes UI code shorter and easier to maintain c) It uses imperative programming d) It works only for iOS apps
What is the role of the @Environment property wrapper in SwiftUI? a) To store global data b) To modify the UI appearance c) To access shared environment values like color schemes d) To bind state values to views
How does SwiftUI handle updates to the UI when the underlying data changes? a) By manually triggering view updates b) By using a declarative system to automatically refresh the view c) By writing custom code to track state changes d) By resetting the entire view every time
What’s the default layout for views in SwiftUI? a) Grid-based b) Stack-based c) Flow-based d) Fixed-size
Topic 2: Declarative UI Design
What is declarative UI design in SwiftUI? a) Defining every step of the UI logic b) Declaring how the UI should look based on the state c) Describing UI with imperative code d) Using storyboards to manage UI layout
Which of these SwiftUI structures is used to arrange views in a vertical layout? a) HStack b) VStack c) ZStack d) List
How does SwiftUI’s declarative approach simplify state management? a) By linking UI components to the state directly b) By requiring manual refresh calls for updates c) By using complex callback functions d) By abstracting away the view layer
Which view in SwiftUI allows for stacking elements on top of each other? a) VStack b) ZStack c) HStack d) Form
In SwiftUI, what happens when the state changes in a view? a) The view is manually redrawn b) The entire app is reloaded c) SwiftUI automatically re-renders the view with new data d) The view updates only if the system detects a change
How does SwiftUI handle layouts in a declarative way? a) By adjusting constraints manually b) By specifying view properties based on the state c) By using auto-layout system entirely d) By defining fixed coordinates for each view
What does a declarative UI code look like in SwiftUI? a) A set of instructions for each screen b) A collection of view objects manipulated in code c) A declaration of what the UI should be like based on data d) A series of imperative UI commands
Which view modifier in SwiftUI is used to add padding around a view? a) .frame() b) .padding() c) .border() d) .background()
What is the purpose of @Binding in SwiftUI? a) To bind an external data model to a view b) To modify a view’s appearance c) To create a local state in a view d) To establish a connection between multiple views
What is the benefit of SwiftUI’s declarative nature for developers? a) It requires less manual coding for layouts b) It reduces the need for external libraries c) It provides easy integration with UIKit d) It is difficult to maintain
Topic 3: Views, State, and Bindings
What does the @State property wrapper do in SwiftUI? a) Provides a way to pass data between views b) Stores data that can be changed and triggers view updates c) Allows views to access external data d) Binds views to external model data
What is the role of @Binding in SwiftUI? a) It allows a view to read and write values to an external state b) It modifies the view’s layout c) It stores static data d) It creates a new instance of data for the view
How do @State and @Binding differ in SwiftUI? a) @State is used for local state, @Binding is used to pass data between views b) @State is used to pass data, @Binding stores the state c) They are the same thing d) @State modifies the layout, @Binding triggers view updates
What does the @ObservedObject property wrapper do? a) Binds a view to a specific state b) Observes changes in an external object and updates the view accordingly c) Stores the current view’s state d) Initializes external model data
What is the primary purpose of @EnvironmentObject in SwiftUI? a) To pass data from parent views to child views b) To store and share data globally across multiple views c) To create local view-specific data d) To bind model data to a view
How does SwiftUI handle state changes in a view? a) It requires a manual update call b) It automatically re-renders views that depend on the changed state c) It resets all views every time the state changes d) It sends a notification to the operating system
Which property wrapper in SwiftUI is used to read values passed from the environment? a) @State b) @Binding c) @ObservedObject d) @Environment
What happens when you use @Binding for a value in SwiftUI? a) The view can only read the value b) The view can modify and update the value c) The value is stored in the view d) The value can’t be changed by the view
Which SwiftUI property wrapper allows views to receive dynamic, model-based updates? a) @State b) @Binding c) @EnvironmentObject d) @ObservedObject
In SwiftUI, what happens when a view is bound to a state variable? a) The state remains unchanged until manually updated b) The view is automatically updated when the state changes c) The state is reset every time the view updates d) The state must be explicitly changed in the view
Answers Table
Qno
Answer (Option with the text)
1
a) A framework for writing UI code in a declarative way
2
d) Storyboards
3
c) HStack
4
b) To store data that can change during the view’s lifecycle
5
d) UIKit components
6
b) By using a struct that conforms to View
7
b) It makes UI code shorter and easier to maintain
8
c) To access shared environment values like color schemes
9
b) By using a declarative system to automatically refresh the view
10
b) Stack-based
11
b) Declaring how the UI should look based on the state
12
b) VStack
13
a) By linking UI components to the state directly
14
b) ZStack
15
c) SwiftUI automatically re-renders the view with new data
16
b) By specifying view properties based on the state
17
c) A declaration of what the UI should be like based on data
18
b) .padding()
19
a) To bind an external data model to a view
20
a) It requires less manual coding for layouts
21
b) Stores data that can be changed and triggers view updates
22
a) It allows a view to read and write values to an external state
23
a) @State is used for local state, @Binding is used to pass data between views
24
b) Observes changes in an external object and updates the view accordingly
25
b) To store and share data globally across multiple views
26
b) It automatically re-renders views that depend on the changed state
27
d) @Environment
28
b) The view can modify and update the value
29
d) @ObservedObject
30
b) The view is automatically updated when the state changes