Swift Basics & Optionals Questions with Answers

14 practice questions on Swift Basics & Optionals (iOS (Swift & SwiftUI)) with answers and step-by-step solutions. Try 10 samples below, then practise all 14 free in the AptiNest app.

Sample questions

Question 1Easy

What type does Swift infer for x in let x = 3.0?

  1. Double
  2. Float
  3. Decimal
  4. CGFloat

Answer: A. Double

Explanation

Floating-point literals default to Double when there is no other type context.

Step-by-step solution

  1. X is assigned the value 3.0, which is a floating-point literal.
  2. In Swift, floating-point literals default to Double unless specified otherwise.
  3. So Swift infers x as type Double.
  4. Double is option A.
  5. Final answer: A (Double).

Why the other options are wrong

  • B. Float is not the default; Swift picks Double for a floating-point literal.
  • C. Decimal is not inferred from a plain literal; it needs an explicit type.
  • D. CGFloat is not the default; it is a separate graphics type that must be asked for explicitly.

Tip: Remember: 3.0 is Double, not Float or CGFloat in Swift.

Question 2Easy

What is the output?

let name: String? = nil
print(name ?? "Guest")
  1. Optional("Guest")
  2. Guest
  3. nil
  4. Compile error: name is never set

Answer: B. Guest

Explanation

The nil-coalescing operator ?? returns the unwrapped value if it exists and the default otherwise. name is nil, so "Guest" is printed.

Step-by-step solution

  1. Name is set to nil, which means it has no value.
  2. The ?? operator checks if name has a value; since it doesn't, it uses 'Guest'.
  3. print(name ?? "Guest") outputs 'Guest' because name is nil.
  4. The print function displays the string directly without wrapping.
  5. So the output is simply 'Guest'.

Why the other options are wrong

  • A. The ?? operator returns the plain default, so no Optional wrapper appears in the output.
  • C. nil is not printed; the ?? operator replaces nil with the default string Guest.
  • D. No compile error: name is an optional, so ?? with a default is valid.

Tip: Use ?? to safely provide a default when an optional is nil.

Question 3Easy

Which is true of a Swift switch statement?

  1. Cases fall through to the next case unless you write break
  2. It must be exhaustive and cases do not fall through by default
  3. A default case is always required, even for enums
  4. It can only switch on Int and String values, not on enums

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

Explanation

Swift requires every possible value to be handled (a default is needed only if the cases don't already cover everything). Execution stops at the end of the matched case unless fallthrough is written.

Step-by-step solution

  1. Swift switch must be exhaustive.
  2. Cases do not fall through by default.

Why the other options are wrong

  • A. That's C-like behaviour.
  • C. Default is not needed if all cases are covered.
  • D. Switch works on enums.

Tip: No fallthrough.

Question 4Easy

What happens at runtime when you force-unwrap (!) an optional that is nil?

  1. The expression quietly evaluates to zero
  2. A catchable error is thrown to the caller
  3. The statement is skipped and execution continues
  4. The program traps with a fatal error

Answer: D. The program traps with a fatal error

Explanation

Force-unwrapping nil is a runtime trap ("Unexpectedly found nil while unwrapping an Optional value"). It cannot be caught with do/catch.

Step-by-step solution

  1. Force-unwrapping a nil optional causes a fatal error at runtime.
  2. The error message is 'Unexpectedly found nil while unwrapping an Optional value'.
  3. This error terminates the program immediately and cannot be caught with do/catch.
  4. No recovery or continuation is possible after this error occurs.
  5. The program stops execution and displays the error message.
  6. The program traps with a fatal error.

Why the other options are wrong

  • A. A nil force unwrap does not give zero; it stops the program with a fatal error.
  • B. A nil force unwrap is not catchable with do/catch; it is a trap, not a thrown error.
  • C. The statement is not skipped; Swift never continues past a failed force unwrap.

Tip: Never force-unwrap optionals unless you are certain they are not nil.

Question 5Medium

What is the output?

let s = "42"
let n = Int(s)
print(n)
  1. nil
  2. Optional(42)
  3. 42
  4. Compile error: Int(s) needs a radix

Answer: B. Optional(42)

Explanation

Int(String) is a failable initializer that returns Int?. Printing an optional directly shows the Optional(...) wrapper (the compiler also warns about it).

Step-by-step solution

  1. Int(s) takes a String and returns Int?, because the string may not be a number.
  2. '42' is a valid number, so the result holds 42 inside an optional.
  3. Print shows an optional as Optional(42), so the answer is option B.

Why the other options are wrong

  • A. Nil only happens when the string is not a number, and '42' is a number.
  • C. The result is still optional, so print shows Optional(42), not 42.
  • D. Int(s) compiles; no radix argument is needed.

Tip: Int(string) returns an optional; unwrap it before using it as a plain Int.

Question 6Medium

What is the output?

enum Shape {
    case circle(Double)
    case square(Double)
}
let s = Shape.circle(2)
switch s {
case .circle(let r): print("circle", r)
case .square(let side): print("square", side)
}
  1. radius: 2.0
  2. Compile error: no default case
  3. circle
  4. circle 2.0

Answer: D. circle 2.0

Explanation

The enum case carries an associated Double. The case pattern binds it to r, and the switch is exhaustive because both cases are listed.

Step-by-step solution

  1. S is Shape.circle(2); the literal 2 is stored as the Double 2.0.
  2. The switch matches the .circle case and binds r to 2.0.
  3. print('circle', r) prints circle 2.0, since a Double shows its decimal point.
  4. So the answer is option D.

Why the other options are wrong

  • A. The printed text starts with 'circle', not 'radius:'.
  • B. The switch covers both cases, so no default is needed.
  • C. Print also prints the value of r, so the output is not just circle.

Tip: Values bound in a switch case can be printed; Double values show as 2.0.

Question 7Medium

What is the output?

let a = 7 / 2
let b = 7.0 / 2
print(a, b)
  1. 3 3.5
  2. 3.5 3.5
  3. Compile error: mixed Int and Double
  4. 3 3

Answer: A. 3 3.5

Explanation

7 / 2 uses integer division and gives 3. In 7.0 / 2 the literal 2 is inferred as Double, giving 3.5.

Step-by-step solution

  1. a = 7 / 2 uses integer division, so a = 3.
  2. b = 7.0 / 2 treats 2 as Double, so b = 3.5.
  3. print(a, b) outputs 3 and 3.5.
  4. Final answer: 3 3.5.

Why the other options are wrong

  • B. The first value is integer division 7 / 2, which is 3, so 3.5 is wrong.
  • C. Mixing Int and Double literals is fine here; the literal 2 is converted, so no compile error.
  • D. The second value is 3.5, not 3, so 3 3 is wrong.

Tip: Remember that dividing integers gives integer results, but using a Double ensures floating-point output.

Question 8Medium

Which statement about guard let is correct?

  1. The else block may fall through to the code after the guard
  2. The unwrapped constant stays in scope after the guard statement
  3. guard let can only be used at the top level of a file
  4. The unwrapped constant exists only inside the guard's else block

Answer: B. The unwrapped constant stays in scope after the guard statement

Explanation

guard let binds the value for the rest of the enclosing scope. Its else branch must exit that scope (return, throw, break, continue) so the code below can rely on the value.

Step-by-step solution

  1. Guard let unwraps an optional and binds it to a constant.
  2. The else block must exit the scope, for example with return.
  3. The bound constant stays in scope after the guard statement, so the code below can use it.
  4. So option B is correct.

Why the other options are wrong

  • A. The else block must exit (return, throw, break), so it cannot fall through.
  • C. Guard let works in functions and closures, not only at file top level.
  • D. This describes if let; the constant from guard let lives on after the guard.

Tip: Guard let is for early exit: the unwrapped value stays available for the rest of the scope.

Question 9Hard

What is the output?

let a: Int?? = .some(nil)
print(a == nil, a! == nil)
  1. true true
  2. true false
  3. false true
  4. false false

Answer: C. false true

Explanation

a is a double optional holding .some(nil): the outer layer has a value, so a == nil is false. Unwrapping one layer with a! gives an inner optional that is nil.

Step-by-step solution

  1. A is declared Int??, and .some(nil) wraps a nil Int? inside a non-nil optional.
  2. a == nil is false, because a holds .some(...) at the outer level.
  3. A! unwraps the outer level to the inner Int? value, which is nil, so a! == nil is true.
  4. Output is false true, so the answer is option C.

Why the other options are wrong

  • A. A is not nil at the outer level; it wraps a nil inside.
  • B. a == nil is false, while a! == nil is true.
  • D. A! is nil, so the second comparison is true.

Tip: With double optionals, count the levels; .some(nil) is not nil at the outer level.

Question 10Hard

What is the output?

let m = Int8.max
print(m &+ 1)
  1. Runtime crash: arithmetic overflow
  2. 128
  3. 127
  4. -128

Answer: D. -128

Explanation

&+ is the overflow (wrapping) addition operator. Int8.max is 127, and 127 &+ 1 wraps round to -128. A plain + would trap at runtime instead.

Step-by-step solution

  1. Int8.max is 127 as it's the maximum value for an 8-bit signed integer.
  2. Using &+ adds 1 to 127, which overflows and wraps around to -128.
  3. The result of 127 &+ 1 is -128 due to overflow wrapping in Int8.
  4. Print statement outputs the wrapped value -128.
  5. Final answer is -128.

Why the other options are wrong

  • A. Swift's &+ wraps around silently and does not trap, so there is no overflow crash.
  • B. 128 is outside Int8, whose largest value is 127, so the wrapped result cannot be 128.
  • C. 127 is the starting value, not the result; &+ wraps it past the maximum.

Tip: Use &+ for overflow-safe arithmetic with fixed-size types like Int8.

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