Swift’s optionals provide a robust way to handle the absence of values. Explore concepts like optional binding, force unwrapping, and the nil-coalescing operator with these 30 carefully crafted MCQs.
Topic 1: What Are Optionals?
What is an optional in Swift? a) A type that represents a collection of values b) A type that can store a value or no value c) A data structure for optional execution d) A Swift-specific function type
Which syntax correctly declares an optional string in Swift? a) var name: String b) var name: Optional<String> c) var name: String? d) Both b and c
What is the default value of an optional that is not assigned in Swift? a) 0 b) nil c) undefined d) Optional.empty
How can you check if an optional has a value? a) Using if optional == nil b) Using if let or guard let c) Using if optional != nil d) All of the above
Which of the following is a valid assignment to an optional integer? a) var number: Int = 10 b) var number: Int? = 10 c) var number: Int? = nil d) Both b and c
Topic 2: Optional Binding: if let and guard let
Which keyword is used for optional binding in Swift? a) var b) let c) if d) Both b and c
What does the following code do?swiftCopy codeif let name = optionalName { print(name) } a) Declares a new constant named optionalName b) Unwraps the value of optionalName if it’s not nil c) Checks if optionalName is nil d) Throws a runtime error
Why would you use guard let instead of if let? a) For early exit from a function b) To avoid nested code c) Both a and b d) Neither a nor b
What happens if an optional is nil in a guard let statement? a) The program continues b) The function returns or throws an error c) A runtime error occurs d) Nothing happens
What does this code output?swiftCopy codevar optionalNumber: Int? = nil if let number = optionalNumber { print(number) } else { print("No value") } a) No value b) 0 c) nil d) Runtime error
Topic 3: Force Unwrapping and Implicitly Unwrapped Optionals
What is force unwrapping in Swift? a) Accessing an optional’s value without checking for nil b) Converting an optional to a non-optional type c) Declaring an optional with ! d) Both a and b
What happens if you force unwrap a nil optional? a) The program continues b) The program crashes c) Returns a default value d) Throws an error
Which operator is used for force unwrapping in Swift? a) ! b) ? c) : d) ??
What is an implicitly unwrapped optional? a) An optional that doesn’t need explicit unwrapping b) An optional declared with ! c) Both a and b d) None of the above
When should you avoid using implicitly unwrapped optionals? a) When the value can be nil b) When safety is critical c) Both a and b d) Never
Topic 4: The Nil-Coalescing Operator
What is the purpose of the nil-coalescing operator (??) in Swift? a) To unwrap an optional safely b) To provide a default value if an optional is nil c) To compare two optionals d) Both a and b
What does the following code output?swiftCopy codelet name: String? = nil let defaultName = name ?? "Guest" print(defaultName) a) Guest b) nil c) “” d) Runtime error
Which of the following is a valid usage of the nil-coalescing operator? a) let value = optional ?? defaultValue b) let value = optional != nil ?? defaultValue c) let value = optional! ?? defaultValue d) let value = optional ?? !defaultValue
Can you use the nil-coalescing operator with non-optional types? a) Yes, always b) No, never c) Only if the second operand is optional d) Only with optional chaining
How does the nil-coalescing operator improve code readability? a) Reduces unwrapping complexity b) Provides a concise fallback mechanism c) Avoids using multiple if-else statements d) All of the above
Mixed Questions
What does optional?.property do in Swift? a) Safely accesses the property if optional is not nil b) Throws an error if optional is nil c) Unwraps the optional forcibly d) Declares a new optional property
Which of these operators is used to chain optionals in Swift? a) . b) ?? c) ? d) !
Can a non-optional type be assigned to an optional variable? a) Yes b) No c) Only with explicit conversion d) Only in special cases
What does the following code snippet output?swiftCopy codevar number: Int? = 42 print(number ?? 0) a) 42 b) 0 c) nil d) Runtime error
What is the difference between if let and guard let? a) if let unwraps, guard let checks for nil b) if let creates local scope, guard let exits on failure c) Both are identical d) None of the above
Why is optional handling crucial in Swift? a) To avoid runtime crashes b) To handle null values safely c) To provide type safety d) All of the above
What’s a common use case for force unwrapping? a) Debugging b) When you’re certain the optional has a value c) When optionals aren’t required d) Both a and b
Can an optional type store multiple nil values? a) Yes, always b) No, never c) It depends on the type d) None of the above
What does this code produce?swiftCopy codelet message: String? = "Hello" print(message!) a) Hello b) nil c) Runtime error d) Undefined behavior
When should you avoid using force unwrapping? a) In production code b) When working with user input c) Both a and b d) Never
Answers Table
Qno
Answer (Option with the text)
1
b) A type that can store a value or no value
2
d) Both b and c
3
b) nil
4
d) All of the above
5
d) Both b and c
6
d) Both b and c
7
b) Unwraps the value of optionalName
8
c) Both a and b
9
b) The function returns or throws an error
10
a) No value
11
d) Both a and b
12
b) The program crashes
13
a) !
14
c) Both a and b
15
c) Both a and b
16
d) Both a and b
17
a) Guest
18
a) let value = optional ?? defaultValue
19
b) No, never
20
d) All of the above
21
a) Safely accesses the property if not nil
22
c) ?
23
a) Yes
24
a) 42
25
b) if let creates local scope, guard let exits on failure