Sample questions
Question 1Easy
What does the === operator check in Swift?
- Whether two structs have equal stored properties
- Whether two references are the same instance
- Whether two optionals are both nil
- Whether two values are equal and of the same type
Answer: B. Whether two references are the same instance
Explanation
=== is the identity operator for class instances. == checks value equality via Equatable.
Step-by-step solution
- === is the identity operator.
- It checks whether two references point to the same instance.
Why the other options are wrong
- A. == with Equatable compares values.
- C. It does not test nil.
- D. It is about class instances.
Tip: Three = same object.
Question 2Easy
What is the output?
struct P { var x = 1 }
var p = P()
var q = p
q.x = 9
print(p.x)
- nil
- Compile error: q is a copy
- 9
- 1
Answer: D. 1
Explanation
Structs are value types. var q = p makes an independent copy, so changing q.x leaves p.x as 1.
Step-by-step solution
- P is a struct, so var q = p makes a copy.
- q.x = 9 changes only the copy.
- p.x is still 1, so print(p.x) shows 1.
- So the answer is option D.
Why the other options are wrong
- A. Print runs and prints a value, not nil.
- B. Assigning a struct copies it; this compiles fine.
- C. 9 is the copy's value; p is unchanged.
Tip: Structs are value types: assignment makes a copy.
Question 3Easy
What is the output?
class C { var x = 1 }
let a = C()
let b = a
b.x = 9
print(a.x)
- Compile error: b is a let constant
- 1
- nil
- 9
Answer: D. 9
Explanation
Classes are reference types. a and b refer to the same object, so a change through b is visible through a. let only stops b from pointing at another object.
Step-by-step solution
- A creates an instance of class C with x = 1.
- B is assigned to point to the same object as a.
- b.x = 9 changes the x property of the shared object to 9.
- print(a.x) accesses the updated x value from the shared object.
- The output is 9.
Why the other options are wrong
- A. The let names hold references, so changing b.x is allowed and no compile error occurs.
- B. a.x is not still 1, because b and a share one object, so the change shows in a.
- C. nil is not the result; x is an Int property, so it prints a number.
Tip: Swift's class variables share the same object, so changes through one variable reflect in others.
Question 4Easy
Which of these is a reference type in Swift?
- A struct with an Array property
- String
- An instance of a class
- Array
Answer: C. An instance of a class
Explanation
Array, String, Dictionary, structs and enums are value types. Class instances (and closures) are reference types.
Step-by-step solution
- Reference types in Swift include classes and closures.
- Structs, enums, and collections like Array are value types.
- String is a value type, not a reference type.
- An instance of a class is a reference type.
- So the correct answer is An instance of a class.
Why the other options are wrong
- A. A struct with an Array is still a value type; copying it copies the struct.
- B. String is a value type in Swift, copied on assignment, not shared by reference.
- D. Array is a value type in Swift, so it is copied rather than shared by reference.
Tip: Remember: Classes are reference types in Swift.
Question 5Medium
Which is true about initializers for structs and classes?
- Structs get a free memberwise initializer; classes don't
- Classes get a free memberwise initializer; structs don't
- Both get a memberwise initializer if all properties are var
- Neither gets any initializer unless one is written
Answer: A. Structs get a free memberwise initializer; classes don't
Explanation
Swift synthesizes a memberwise init for structs. A class only gets a default init() when every stored property has a default value.
Step-by-step solution
- Structs get a free memberwise initializer by default.
- Classes don't get a memberwise initializer unless written.
- All properties in structs are initialized with memberwise init.
- Classes need explicit
init if properties have no defaults. - Final answer: Structs get a free memberwise initializer; classes don't.
Why the other options are wrong
- B. Classes do not get a memberwise initializer; only structs get one automatically.
- C. Classes never get memberwise initializers, even when all properties are var.
- D. Structs get a free memberwise initializer, so saying neither does is false.
Tip: Remember: structs have a free memberwise init, classes do not.
Question 6Medium
Does this code compile?
class C { var x = 1 }
let c = C()
c.x = 2
- No: class properties need the mutating keyword to change
- Yes, but only because the class has a default initializer
- Yes: let fixes the reference, not the object's var properties
- No: an object held in a let constant cannot be modified
Answer: C. Yes: let fixes the reference, not the object's var properties
Explanation
With a class, let makes the reference constant. The object it points to can still have its var properties changed.
Step-by-step solution
- C is a let constant holding a class instance, which is a reference.
- Let fixes which object c refers to, not the contents of that object.
- c.x is a var property, so changing it through c compiles fine.
- So option C is correct.
Why the other options are wrong
- A. Classes do not need mutating; that keyword is for struct and enum methods.
- B. The default initializer is not the reason; var properties on a class can change through a let reference.
- D. The object can be modified even when it is held in a let constant.
Tip: Let on a class means the reference is fixed; the object's var properties can still change.
Question 7Medium
Does this code compile?
struct P { var x = 1 }
let p = P()
p.x = 2
- Yes, but the assignment is silently ignored at runtime
- Yes, because x is declared with var inside the struct
- No, because structs cannot have default property values
- No: p is a let constant, so p.x can't change
Answer: D. No: p is a let constant, so p.x can't change
Explanation
For a value type, a let instance is fully immutable, even its var properties. Declaring p with var would fix it.
Step-by-step solution
- P is declared with let, so the constant cannot be reassigned.
- p.x = 2 changes a stored property of a let struct instance.
- Swift rejects this at compile time because p is immutable.
- So option D is correct.
Why the other options are wrong
- A. It does not compile, so there is no silent runtime ignore.
- B. The var inside the struct does not override let on the instance.
- C. Structs can have default property values; that part is fine.
Tip: A let struct is fully immutable; use var when you need to change properties.
Question 8Medium
What does marking a class final do?
- Prevents any instances of the class from being created
- Makes all of the class's stored properties constants
- Stops subclassing and lets the compiler call methods directly
- Turns the class into a value type that copies on assignment
Answer: C. Stops subclassing and lets the compiler call methods directly
Explanation
A final class cannot be subclassed. Since no override can exist, the compiler can use static dispatch for its methods.
Step-by-step solution
- final stops subclassing.
- No overrides can exist.
- So the compiler can use direct (static) calls.
Why the other options are wrong
- A. Instances can still be created.
- B. final does not make properties constants.
- D. It stays a reference type.
Tip: final = no subclass.
Question 9Hard
What is the output?
class C { var x = 0 }
var a = [C()]
var b = a
b[0].x = 5
b.append(C())
print(a[0].x, a.count)
- 0 2
- 0 1
- 5 1
- 5 2
Answer: C. 5 1
Explanation
Copying the array copies its element references, not the objects. Both arrays point at the same C instance, so b[0].x = 5 is seen through a. Appending to b only changes b.
Step-by-step solution
- Arrays are value types but class elements are references.
- b = a copies the references, so b[0] is the same C.
- b[0].x = 5 changes it for a as well.
- append only changes b.
Why the other options are wrong
- A. a[0].x becomes 5.
- B. a still has 1 element but x is 5.
- D. Appending to b does not change a's count.
Tip: Value container, reference elements.
Question 10Hard
What is the output?
struct P { var x = 1 }
var s = P()
let f = { print(s.x) }
s.x = 5
f()
- 1
- 5
- Compile error: s is captured by value
- 0
Answer: B. 5
Explanation
A closure captures the variable s itself, not a snapshot of its value. The change made before the call is visible when the closure runs.
Step-by-step solution
- S is a global var, and the closure f captures the variable, not a copy of its value.
- s.x is set to 5 before f is called.
- f() reads s.x when it runs, and s.x is now 5.
- Output is 5, so the answer is option B.
Why the other options are wrong
- A. The closure reads the variable when called, so it sees 5, not the old 1.
- C. Closures capture variables by reference; this compiles.
- D. The value 0 never appears; s.x starts at 1 and becomes 5.
Tip: Closures capture variables, not snapshots, unless you use a capture list such as [x].
Practise all 13 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 →