Structs, Classes & Enums Questions with Answers

21 practice questions on Structs, Classes & Enums (Swift) with answers and step-by-step solutions. Try 10 samples below, then practise all 21 free in the AptiNest app.

Sample questions

Question 1Easy

What is the output?

struct M { static let k = 7 }
print(M.k)
  1. 0
  2. 7
  3. k
  4. Compile error

Answer: B. 7

Explanation

A static property belongs to the type, so it is read through M.k.

Step-by-step solution

  1. static = type-level.
  2. Read as M.k.
  3. 7.

Why the other options are wrong

  • A. static is accessed via the type.
  • C. It prints the value.
  • D. Static members belong to the type.

Question 2Easy

What is the output?

struct P { var x = 1 }
var a = P()
var b = a
b.x = 9
print(a.x)
  1. 9
  2. 0
  3. Compile error
  4. 1

Answer: D. 1

Explanation

Structs are copied on assignment, so changing b does not affect a.

Step-by-step solution

  1. P is a struct, so var b = a makes a copy.
  2. Changing b.x to 9 changes only the copy.
  3. a.x is still 1 (option D).

Why the other options are wrong

  • A. The copy changed, not a; a.x stays 1.
  • B. The default value of x is 1, not 0.
  • C. The code compiles because a and b are var.

Tip: Struct assignment copies the value; class assignment shares the instance.

Question 3Easy

What is the output?

enum L { case a, b }
let l = L.b
switch l {
case .a: print(1)
case .b: print(2)
}
  1. 2
  2. 1
  3. 12
  4. Compile error

Answer: A. 2

Explanation

The switch matches .b, printing 2.

Step-by-step solution

  1. l is .b.
  2. Second case runs.
  3. Prints 2.

Why the other options are wrong

  • B. l is .b.
  • C. Only one case runs.
  • D. The switch covers all cases.

Question 4Easy

Which is a value type in Swift?

  1. class
  2. closure
  3. actor reference
  4. struct

Answer: D. struct

Explanation

Structs and enums are value types, copied on assignment; classes are reference types.

Step-by-step solution

  1. Structs are value types: assignment makes a copy.
  2. Classes, actors and closures are reference-based.
  3. So struct is the value type (option D).

Why the other options are wrong

  • A. A class is a reference type.
  • B. A closure is a reference-based function value, not a value type in this sense.
  • C. An actor is a reference type that serializes access.

Tip: Use struct for plain data; use class when shared identity matters.

Question 5Medium

What is the output?

struct T { var v = 0 { didSet { print("old \(oldValue)") } } }
var t = T()
t.v = 3
  1. old 3
  2. old 0
  3. new 3
  4. Nothing is printed

Answer: B. old 0

Explanation

didSet runs after assignment, with oldValue holding the previous value (0).

Step-by-step solution

  1. v starts at 0.
  2. Set to 3.
  3. didSet prints old 0.

Why the other options are wrong

  • A. oldValue is the previous value.
  • C. The observer prints oldValue.
  • D. didSet fires after assignment.

Question 6Medium

What is the output?

struct S { var a: Int; var b = 2 }
print(S(a: 1).b)
  1. 1
  2. 2
  3. 0
  4. Compile error

Answer: B. 2

Explanation

The memberwise initialiser lets you omit properties that have default values.

Step-by-step solution

  1. b default is 2.
  2. Only a is passed.
  3. b stays 2.

Why the other options are wrong

  • A. b has the default 2.
  • C. b has the default 2.
  • D. Properties with defaults can be omitted in the memberwise init.

Question 7Medium

What is the output?

class A { var x = 0 }
let a = A()
let b = a
print(a === b)
  1. false
  2. true
  3. Compile error
  4. 0

Answer: B. true

Explanation

=== checks identity; a and b point at the same object.

Step-by-step solution

  1. b = a copies the reference.
  2. Same object.
  3. true.

Why the other options are wrong

  • A. Both constants refer to the same instance.
  • C. === compares class references.
  • D. It returns a Bool.

Question 8Medium

What is the output?

class C { var x = 1 }
let a = C()
a.x = 7
print(a.x)
  1. 1
  2. 7
  3. Compile error because a is a let
  4. 0

Answer: B. 7

Explanation

A let reference to a class only fixes the reference; the instance's var properties can still change.

Step-by-step solution

  1. A is a let reference to a class instance.
  2. The reference cannot change, but x is a var property of the instance.
  3. a.x = 7 is allowed, so print shows 7 (option B).

Why the other options are wrong

  • A. The property was changed to 7 before printing.
  • C. Let only fixes the reference; the instance's var properties can change.
  • D. The instance was set to 7, not 0.

Tip: Let on a class fixes the reference, not the object's var properties.

Question 9Hard

When must an enum be declared indirect?

  1. When it has raw values
  2. When it has more than 10 cases
  3. When it conforms to Equatable
  4. When a case holds another instance of the same enum, like a recursive tree

Answer: D. When a case holds another instance of the same enum, like a recursive tree

Explanation

indirect stores the associated value behind a pointer so recursive enums have finite size.

Step-by-step solution

  1. A case contains the enum itself.
  2. Size would be infinite.
  3. indirect fixes this.

Why the other options are wrong

  • A. Raw values don't require indirect.
  • B. Number of cases doesn't matter.
  • C. Conformance is unrelated.

Question 10Hard

What is the output?

class A { func f() -> String { "A" } }
class B: A { override func f() -> String { super.f() + "B" } }
print(B().f())
  1. BA
  2. AB
  3. B
  4. A

Answer: B. AB

Explanation

The override calls super.f() ("A") and appends "B".

Step-by-step solution

  1. B overrides f.
  2. super.f() = "A".
  3. "A" + "B".

Why the other options are wrong

  • A. super.f() runs first and gives "A".
  • C. super.f() is called.
  • D. The override appends B.

Practise all 21 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