Sample questions
Question 1Easy
What do classes and actors have in common?
- Both are reference types
- Both protect their state from data races
- Both are copied on assignment
- Both support inheritance
Answer: A. Both are reference types
Explanation
Actors are reference types like classes, but unlike classes they can't be subclassed and they isolate their mutable state.
Step-by-step solution
- Classes and actors are both reference types, so assignment shares one instance.
- Only actors guard their state against data races, and only classes support inheritance.
- Marked answer: option A.
Why the other options are wrong
- B. Plain classes do not protect their state; only actors do.
- C. Both are reference types, so they are not copied on assignment.
- D. Actors cannot inherit, so only classes support inheritance.
Tip: Reference types share one instance on assignment; value types are copied.
Question 2Easy
- A task that is too slow to finish before a timeout
- Two tasks downloading the same data from a server
- Two arrays being sorted at the same time on one thread
- Unsynchronised access to shared memory with a write
Answer: D. Unsynchronised access to shared memory with a write
Explanation
Unsynchronised concurrent access with at least one write gives unpredictable results. Actors and Sendable checking exist to prevent this.
Step-by-step solution
- Data race happens when multiple threads access shared memory without synchronisation.
- At least one thread must perform a write operation on the shared memory.
- Unsynchronised writes lead to unpredictable and incorrect results in programs.
- This is why data races are considered a critical bug in concurrent programming.
- Option D describes unsynchronised access with a write, which matches the definition of a data race.
- Final answer: Unsynchronised access to shared memory with a write.
Why the other options are wrong
- A. A slow task is a performance issue, not a data race; a race needs unsynchronised shared writes.
- B. Two downloads of the same data are not a race by themselves; there is no shared write here.
- C. Sorting two arrays on one thread cannot race, since only one thread touches them.
Tip: Remember: Data races involve unsynchronized reads/writes to shared memory in concurrent programs.
Question 3Easy
From outside an actor, how do you call one of its isolated methods, such as await bank.deposit(100)?
- Directly, exactly like a normal class method
- With try!, because actor calls can always fail
- With await, because the call may wait for its turn
- Only by wrapping it in DispatchQueue.main.async
Answer: C. With await, because the call may wait for its turn
Explanation
Calls into an actor from outside are asynchronous: the caller may have to wait until the actor is free, so the call is marked with await.
Step-by-step solution
- Await tells Swift to wait for the actor's turn to process the call.
- Actor methods are asynchronous, so await ensures the call waits properly.
- Using await is required for any method call into an actor from outside.
- This makes sure the caller doesn't proceed until the actor is free.
- The correct way is to use await before the method call.
Why the other options are wrong
- A. Actor methods cannot be called directly from outside, so the call must be awaited.
- B. try! is not needed; actor calls do not fail by nature and need await, not try!.
- D. DispatchQueue is not needed; an actor is isolated by Swift itself.
Tip: Always use await when calling an actor's method from outside.
Question 4Easy
What is an actor in Swift?
- A reference type that isolates its mutable state
- A protocol for objects that respond to UI events
- A value type that is copied whenever it is passed
- A thread that is created for each async function
Answer: A. A reference type that isolates its mutable state
Explanation
An actor is like a class whose mutable state can only be touched by one task at a time, which prevents data races.
Step-by-step solution
- An actor in Swift is a reference type that isolates its mutable state.
- It ensures only one task can access its state at a time.
- This isolation prevents data races and thread safety issues.
- Actors are used to manage concurrency safely in Swift.
- The correct answer matches the definition of an actor in Swift.
- Final answer: A (A reference type that isolates its mutable state).
Why the other options are wrong
- B. An actor is not a protocol for UI events; it is a type that guards its mutable state.
- C. An actor is a reference type, so passing it shares the same instance instead of copying it.
- D. Actors do not create a thread per async function; tasks run on a shared pool.
Tip: Remember, actors in Swift are reference types used for safe concurrency.
Question 5Medium
Does this code compile?
actor Counter { var n = 0 }
func show(_ c: Counter) {
print(c.n)
}
- No: outside reads of actor state need await
- No: actors cannot have stored var properties
- Yes: reading a property is always synchronous
- Yes, but the read may return a stale value
Answer: A. No: outside reads of actor state need await
Explanation
Access to actor-isolated state from outside the actor is asynchronous, because it may need to wait its turn. It would compile as await c.n in an async context.
Step-by-step solution
- Reading actor properties from outside needs
await keyword. - Code lacks
await when accessing c.n, causing compilation error. - Actor state is isolated, so external reads must be asynchronous.
- Missing
await makes the code invalid in Swift concurrency model. - Thus, the code does not compile as written.
Why the other options are wrong
- B. Actors can have stored var properties; Counter's n is one.
- C. Reading a property from outside an actor is not synchronous; it needs await.
- D. It does not compile at all, so no value is read and none can be stale.
Tip: Always use await when reading from actors outside their context.
Question 6Medium
Inside one of an actor's own methods, how does it read its own stored properties?
- Directly and synchronously, without await
- Only through a nonisolated getter
- Only after locking the actor manually
- Only with await, like any outside caller
Answer: A. Directly and synchronously, without await
Explanation
Code already running on the actor is isolated to it, so its own state can be used synchronously.
Step-by-step solution
- Actor methods run in isolation, so they can access their own properties directly.
- No await needed because the code is already inside the actor's context.
- Properties are read synchronously without external calls.
- This allows direct and immediate access to stored values.
- Final answer: Directly and synchronously, without await.
Why the other options are wrong
- B. Inside the actor, its own stored properties need no getter; they are read directly.
- C. No manual lock is used; the actor already serialises access, so locking is not needed.
- D. await is not needed inside the actor's own methods; outside callers need it.
Tip: Inside an actor's method, read properties directly without await.
Question 7Medium
Which statement about actors is true?
- Actors do not support inheritance
- Actors cannot conform to protocols
- Actors are value types like structs
- Actors cannot have methods, only properties
Answer: A. Actors do not support inheritance
Explanation
Actors are reference types that can have methods, properties, initializers and protocol conformances, but they cannot inherit from other actors.
Step-by-step solution
- Actors can conform to protocols and can have methods and properties.
- Actors are reference types and cannot inherit from another class or actor.
- Marked answer: option A, actors do not support inheritance.
Why the other options are wrong
- B. Actors can conform to protocols, just as classes and structs can.
- C. Actors are reference types, not value types like structs.
- D. Actors can have methods as well as properties.
Tip: Actors are reference types with no inheritance; they can have methods and conform to protocols.
Question 8Medium
What kind of actor is MainActor?
- A class that holds a reference to the app delegate
- A local actor created for each view
- A global actor tied to the main thread
- An actor that runs work on a background queue
Answer: C. A global actor tied to the main thread
Explanation
MainActor is a global actor: any type, function or property marked @MainActor shares its single, main-thread executor.
Step-by-step solution
- MainActor runs on the main thread by default.
- It is shared across all code marked with @MainActor.
- This makes it a global actor, not per-view or background.
- So MainActor is tied to the main thread globally.
- Final answer: A global actor tied to the main thread.
Why the other options are wrong
- A. MainActor is not an app delegate class; it is a global actor for main-thread code.
- B. MainActor is global, not created for each view, so this is wrong.
- D. It runs on the main thread, not on a background queue, so this is the opposite.
Tip: Use @MainActor for UI-related code to ensure it runs on the main thread.
Question 9Hard
What is actor reentrancy?
- Every call to an actor creates a fresh copy of its state
- An actor can only process one method call in its whole lifetime
- At an await inside an actor method, other calls can run on the actor
- An actor calling one of its own methods recursively causes deadlock
Answer: C. At an await inside an actor method, other calls can run on the actor
Explanation
While an actor method is suspended at an await, the actor may run other queued work. So assumptions about state made before the await must be rechecked after it.
Step-by-step solution
- An actor runs one call at a time, but an await inside one of its methods suspends that method.
- While it is suspended, other calls can run on the actor, so its state may change before the method resumes.
- Marked answer: option C.
Why the other options are wrong
- A. Actor state is shared and protected, not copied on every call.
- B. An actor handles many method calls over its lifetime; it is not limited to one.
- D. Calling its own methods does not deadlock; reentrancy is about other calls running while an await is suspended.
Tip: After every await in an actor, re-check any state you read before it.
Question 10Hard
A class ViewModel is marked @MainActor. What follows?
- It can no longer be used from SwiftUI views
- Only its initializer runs on the main thread
- It becomes a struct with value semantics
- Its members become main-actor isolated
Answer: D. Its members become main-actor isolated
Explanation
A global actor attribute on a type applies to all its members, so callers from other actors must await them. This suits UI-facing view models.
Step-by-step solution
- @MainActor is a global actor that runs code on the main thread.
- Marking a class with it puts all its properties and methods under main-actor isolation.
- Marked answer: option D.
Why the other options are wrong
- A. A main-actor ViewModel can still be used from SwiftUI views; that is the usual pattern.
- B. The whole class is isolated, not only its initializer.
- C. @MainActor does not turn a class into a struct or change its value semantics.
Tip: Mark a ViewModel @MainActor to keep its members on the main thread.
Practise all 12 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 →