Basics & Optionals Questions with Answers

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

Sample questions

Question 1Easy

What is the output?

let n = Int("42")
print(n)
  1. 42
  2. nil
  3. Optional(42)
  4. Compile error

Answer: C. Optional(42)

Explanation

Int("42") is failable, so it returns Int?. Printing it shows Optional(42).

Step-by-step solution

  1. Int(String) can fail.
  2. So the type is Int?.
  3. print shows Optional(42).

Why the other options are wrong

  • A. Int(String) returns an optional, and print shows Optional(42).
  • B. "42" is a valid number.
  • D. Int(String) is a failable initialiser.

Question 2Easy

What happens here?

var x = 3
x = 3.5
  1. x becomes 3.5
  2. x becomes 3
  3. Compile error: x was inferred as Int and cannot hold a Double
  4. Runtime crash

Answer: C. Compile error: x was inferred as Int and cannot hold a Double

Explanation

Swift is statically typed: var x = 3 infers Int, so a Double value is rejected.

Step-by-step solution

  1. 3 is an Int literal.
  2. x has type Int.
  3. 3.5 is a Double, so it fails.

Why the other options are wrong

  • A. x was inferred as Int.
  • B. Swift never truncates silently.
  • D. The mistake is caught at compile time.

Tip: Write var x = 3.0 or var x: Double = 3 to get a Double.

Question 3Easy

What is the output?

let t = (1, "x")
print(t.1)
  1. 1
  2. (1, "x")
  3. Compile error
  4. x

Answer: D. x

Explanation

Tuple members are accessed by zero-based position, so .1 is the second member.

Step-by-step solution

  1. t = (1, "x").
  2. .0 is 1, .1 is "x".
  3. Prints x.

Why the other options are wrong

  • A. Tuple elements are zero-indexed, so .1 is the second.
  • B. t.1 selects one element.
  • C. Tuple elements can be accessed by index.

Question 4Easy

What is the output?

print(Int("abc") == nil)
  1. false
  2. 0
  3. Compile error
  4. true

Answer: D. true

Explanation

"abc" is not a number so the failable initialiser returns nil.

Step-by-step solution

  1. Parsing fails.
  2. Result is nil.
  3. nil == nil is true.

Why the other options are wrong

  • A. "abc" cannot be parsed.
  • B. Failure gives nil, not 0.
  • C. Optionals can be compared to nil.

Question 5Medium

What is the output?

print(7 / 2, 7 % 2, 7.0 / 2)
  1. 3.5 1 3.5
  2. 3 1 3
  3. 3.5 1.0 3.5
  4. 3 1 3.5

Answer: D. 3 1 3.5

Explanation

Int division truncates (3) and % gives the remainder (1). A Double operand gives 3.5.

Step-by-step solution

  1. 7 / 2 = 3.
  2. 7 % 2 = 1.
  3. 7.0 / 2 = 3.5.

Why the other options are wrong

  • A. Integer division truncates.
  • B. 7.0 / 2 is Double division.
  • C. Int operands remain Int.

Question 6Medium

What is the output?

if let n = Int("12"), n > 10 {
    print("big")
} else {
    print("small")
}
  1. small
  2. big
  3. 12
  4. Compile error

Answer: B. big

Explanation

The optional binding succeeds with 12 and then 12 > 10 is true.

Step-by-step solution

  1. Int("12") = 12.
  2. 12 > 10.
  3. Prints big.

Why the other options are wrong

  • A. 12 > 10 is true.
  • C. The branch prints a word.
  • D. Conditions can be combined with commas.

Question 7Medium

In Swift 5.7 and later, what does if let name { ... } mean?

  1. It checks name is not a String
  2. Shorthand for if let name = name, unwrapping an existing optional
  3. Compile error: the right side is required
  4. It force-unwraps name

Answer: B. Shorthand for if let name = name, unwrapping an existing optional

Explanation

The shorthand binds a new non-optional constant with the same name if the optional has a value.

Step-by-step solution

  1. Optional binding.
  2. Name repeated implicitly.
  3. Same as if let name = name.

Why the other options are wrong

  • A. It unwraps, it doesn't check types.
  • C. The shorthand is allowed.
  • D. Binding never traps.

Question 8Medium

Given let user: User?, what is the type of user?.address?.city if city is a String?

  1. String
  2. String??
  3. Optional<User>
  4. String?

Answer: D. String?

Explanation

Optional chaining returns an optional of the final property's type; it never nests optionals.

Step-by-step solution

  1. Any link may be nil.
  2. Result is nil then.
  3. Type String?.

Why the other options are wrong

  • A. Optional chaining always yields an optional.
  • B. Optional chaining flattens into one level.
  • C. It is the type of the final property.

Question 9Hard

What is the output?

let x: Int? = 5
switch x {
case .some(let v) where v > 3: print("big")
case .some: print("small")
case .none: print("nil")
}
  1. small
  2. big
  3. nil
  4. Compile error

Answer: B. big

Explanation

An Optional is an enum with .some and .none. The first case matches since 5 > 3.

Step-by-step solution

  1. x = .some(5).
  2. where v > 3 is true.
  3. Prints big.

Why the other options are wrong

  • A. The first case matches since 5 > 3.
  • C. x holds a value.
  • D. Optionals can be matched with .some/.none.

Question 10Hard

What is the output?

print(Int8.max &+ 1)
  1. 128
  2. Crash
  3. -128
  4. 127

Answer: C. -128

Explanation

&+ is the overflow operator: 127 wraps to -128 for an 8-bit signed integer. Plain + would crash.

Step-by-step solution

  1. Int8.max = 127.
  2. 127 &+ 1 overflows.
  3. Wraps to -128.

Why the other options are wrong

  • A. Int8 cannot hold 128.
  • B. &+ is the wrapping operator, so it does not trap.
  • D. It wraps around.

Tip: Use &+ to wrap; use addingReportingOverflow to detect overflow.

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