Value vs Reference Types Questions with Answers

13 practice questions on Value vs Reference Types (iOS (Swift & SwiftUI)) with answers and step-by-step solutions. Try 10 samples below, then practise all 13 free in the AptiNest app.

Sample questions

Question 1Easy

What does the === operator check in Swift?

  1. Whether two structs have equal stored properties
  2. Whether two references are the same instance
  3. Whether two optionals are both nil
  4. 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

  1. === is the identity operator.
  2. 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)
  1. nil
  2. Compile error: q is a copy
  3. 9
  4. 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

  1. P is a struct, so var q = p makes a copy.
  2. q.x = 9 changes only the copy.
  3. p.x is still 1, so print(p.x) shows 1.
  4. 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)
  1. Compile error: b is a let constant
  2. 1
  3. nil
  4. 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

  1. A creates an instance of class C with x = 1.
  2. B is assigned to point to the same object as a.
  3. b.x = 9 changes the x property of the shared object to 9.
  4. print(a.x) accesses the updated x value from the shared object.
  5. 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?

  1. A struct with an Array property
  2. String
  3. An instance of a class
  4. 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

  1. Reference types in Swift include classes and closures.
  2. Structs, enums, and collections like Array are value types.
  3. String is a value type, not a reference type.
  4. An instance of a class is a reference type.
  5. 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?

  1. Structs get a free memberwise initializer; classes don't
  2. Classes get a free memberwise initializer; structs don't
  3. Both get a memberwise initializer if all properties are var
  4. 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

  1. Structs get a free memberwise initializer by default.
  2. Classes don't get a memberwise initializer unless written.
  3. All properties in structs are initialized with memberwise init.
  4. Classes need explicit init if properties have no defaults.
  5. 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
  1. No: class properties need the mutating keyword to change
  2. Yes, but only because the class has a default initializer
  3. Yes: let fixes the reference, not the object's var properties
  4. 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

  1. C is a let constant holding a class instance, which is a reference.
  2. Let fixes which object c refers to, not the contents of that object.
  3. c.x is a var property, so changing it through c compiles fine.
  4. 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
  1. Yes, but the assignment is silently ignored at runtime
  2. Yes, because x is declared with var inside the struct
  3. No, because structs cannot have default property values
  4. 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

  1. P is declared with let, so the constant cannot be reassigned.
  2. p.x = 2 changes a stored property of a let struct instance.
  3. Swift rejects this at compile time because p is immutable.
  4. 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?

  1. Prevents any instances of the class from being created
  2. Makes all of the class's stored properties constants
  3. Stops subclassing and lets the compiler call methods directly
  4. 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

  1. final stops subclassing.
  2. No overrides can exist.
  3. 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)
  1. 0 2
  2. 0 1
  3. 5 1
  4. 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

  1. Arrays are value types but class elements are references.
  2. b = a copies the references, so b[0] is the same C.
  3. b[0].x = 5 changes it for a as well.
  4. 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. 1
  2. 5
  3. Compile error: s is captured by value
  4. 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

  1. S is a global var, and the closure f captures the variable, not a copy of its value.
  2. s.x is set to 5 before f is called.
  3. f() reads s.x when it runs, and s.x is now 5.
  4. 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 →

Related topics

See all iOS (Swift & SwiftUI) topics · All topics