Memory & Control Flow Questions with Answers

20 practice questions on Memory & Control Flow (Swift) with answers and step-by-step solutions. Try 10 samples below, then practise all 20 free in the AptiNest app.

Sample questions

Question 1Easy

What is the output?

for i in stride(from: 10, to: 0, by: -3) { print(i, terminator: ",") }
  1. 10,7,4,1,
  2. 10,7,4,1,0,
  3. 10,7,4,
  4. 7,4,1,

Answer: A. 10,7,4,1,

Explanation

stride(from:to:by:) excludes the end, so 10, 7, 4, 1 are produced.

Step-by-step solution

  1. 10, then 7.
  2. Then 4 and 1.
  3. Next -2 is below 0.

Why the other options are wrong

  • B. to excludes the end value.
  • C. 1 is still above 0.
  • D. Start at 10.

Question 2Easy

What is the output?

repeat { print("x") } while false
  1. Nothing
  2. xx
  3. x
  4. Compile error

Answer: C. x

Explanation

repeat-while tests the condition after the body, so the body runs once.

Step-by-step solution

  1. Body runs.
  2. Condition false.
  3. Loop ends.

Why the other options are wrong

  • A. repeat-while runs once before the check.
  • B. It stops after one pass.
  • D. Valid syntax.

Question 3Easy

How does Swift manage memory for class instances?

  1. Automatic Reference Counting (ARC)
  2. A tracing garbage collector
  3. Manual malloc and free
  4. Copying all objects at each assignment

Answer: A. Automatic Reference Counting (ARC)

Explanation

ARC frees an instance when its strong reference count reaches zero.

Step-by-step solution

  1. Swift does not use a garbage collector.
  2. ARC counts strong references and frees an object when the count reaches zero.
  3. So Swift uses ARC (option A).

Why the other options are wrong

  • B. Swift does not run a tracing garbage collector.
  • C. Malloc and free are C-style manual memory calls, not Swift's model.
  • D. Copying every object on assignment is not how Swift handles class instances.

Tip: ARC frees objects automatically; strong reference cycles still leak.

Question 4Easy

What is the output?

for i in 1...3 { print(i, terminator: "") }
  1. 12
  2. 1 2 3
  3. 3
  4. 123

Answer: D. 123

Explanation

1...3 includes 3 and the empty terminator keeps the output on one line.

Step-by-step solution

  1. 1, 2, 3.
  2. No newlines.
  3. 123.

Why the other options are wrong

  • A. The closed range includes 3.
  • B. terminator is empty.
  • C. All values print.

Question 5Medium

What is the output?

let n = 15
switch n {
case 0..<10: print("a")
case 10...20: print("b")
default: print("c")
}
  1. a
  2. c
  3. bc
  4. b

Answer: D. b

Explanation

15 lies in 10...20 so only "b" prints.

Step-by-step solution

  1. 15 is not in 0..<10.
  2. 15 is in 10...20.
  3. b.

Why the other options are wrong

  • A. 15 is outside 0..<10.
  • B. 15 matches 10...20.
  • C. Swift cases do not fall through.

Question 6Medium

Two class instances hold strong references to each other. What happens to their memory?

  1. ARC frees them when the function exits
  2. The garbage collector breaks the cycle
  3. They leak: ARC never frees them because each keeps the other alive
  4. The program crashes

Answer: C. They leak: ARC never frees them because each keeps the other alive

Explanation

ARC counts strong references only; a cycle keeps counts above zero. Make one side weak or unowned.

Step-by-step solution

  1. A holds B, B holds A.
  2. Counts never reach zero.
  3. Use weak on one side.

Why the other options are wrong

  • A. Strong references keep both alive.
  • B. Swift has no tracing garbage collector.
  • D. No crash; memory just leaks.

Question 7Medium

What is the output?

switch (1, 0) {
case (_, 0): print("axis")
default: print("no")
}
  1. no
  2. 1
  3. axis
  4. Compile error

Answer: C. axis

Explanation

_ matches any value, and the second component is 0, so the first case matches.

Step-by-step solution

  1. Tuple (1, 0).
  2. Pattern (_, 0) matches.
  3. axis.

Why the other options are wrong

  • A. The tuple matches (_, 0).
  • B. The pattern prints a word.
  • D. Tuple patterns are valid.

Question 8Medium

What is the output?

for i in 1..<4 { print(i, terminator: "") }
  1. 1234
  2. 234
  3. 123
  4. 12

Answer: C. 123

Explanation

The half-open range 1..<4 excludes 4.

Step-by-step solution

  1. 1..<4 is a half-open range: 1, 2, 3.
  2. Print with terminator "" puts the values on one line.
  3. The output is 123 (option C).

Why the other options are wrong

  • A. The range excludes 4, so 4 is not printed.
  • B. 1 is in the range and is printed.
  • D. 3 is in the range and is printed.

Tip: a..<b stops before b; a...b includes b.

Question 9Hard

What does the switch statement in Swift require?

  1. An explicit break statement after every single case
  2. It must be exhaustive, and cases do not fall through by default
  3. A default case in absolutely all situations
  4. Only integer values to match against the cases

Answer: B. It must be exhaustive, and cases do not fall through by default

Explanation

Swift switch statements must cover all values and exit after the matching case.

Step-by-step solution

  1. Swift switch must cover every possible value, or include default.
  2. Cases do not fall through by default; control leaves after a matching case.
  3. So option B is correct.

Why the other options are wrong

  • A. No break is needed after each case.
  • C. Default is needed only when the cases do not cover every value.
  • D. Switch can match strings, tuples, enums and other types.

Tip: Use fallthrough only when you really want the next case to run.

Question 10Hard

What is the output?

class P { deinit { print("P gone") } }
var p: P? = P()
weak var w = p
p = nil
print(w == nil)
  1. P gone, then true
  2. true only
  3. P gone, then false
  4. false

Answer: A. P gone, then true

Explanation

A weak reference does not keep the object alive; setting p to nil frees it and w becomes nil.

Step-by-step solution

  1. Only p holds P strongly.
  2. p = nil frees P.
  3. w is nil.

Why the other options are wrong

  • B. deinit runs when p = nil.
  • C. weak is zeroed on release.
  • D. weak does not keep it alive.

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