MCQs on Optionals | Swift

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?

  1. 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
  2. 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
  3. What is the default value of an optional that is not assigned in Swift?
    a) 0
    b) nil
    c) undefined
    d) Optional.empty
  4. 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
  5. 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

  1. Which keyword is used for optional binding in Swift?
    a) var
    b) let
    c) if
    d) Both b and c
  2. 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
  3. 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
  4. 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
  5. 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

  1. 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
  2. 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
  3. Which operator is used for force unwrapping in Swift?
    a) !
    b) ?
    c) :
    d) ??
  4. 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
  5. 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

  1. 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
  2. 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
  3. 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
  4. 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
  5. 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

  1. 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
  2. Which of these operators is used to chain optionals in Swift?
    a) .
    b) ??
    c) ?
    d) !
  3. 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
  4. What does the following code snippet output?swiftCopy codevar number: Int? = 42 print(number ?? 0) a) 42
    b) 0
    c) nil
    d) Runtime error
  5. 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
  6. 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
  7. 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
  8. 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
  9. What does this code produce?swiftCopy codelet message: String? = "Hello" print(message!) a) Hello
    b) nil
    c) Runtime error
    d) Undefined behavior
  10. 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

QnoAnswer (Option with the text)
1b) A type that can store a value or no value
2d) Both b and c
3b) nil
4d) All of the above
5d) Both b and c
6d) Both b and c
7b) Unwraps the value of optionalName
8c) Both a and b
9b) The function returns or throws an error
10a) No value
11d) Both a and b
12b) The program crashes
13a) !
14c) Both a and b
15c) Both a and b
16d) Both a and b
17a) Guest
18a) let value = optional ?? defaultValue
19b) No, never
20d) All of the above
21a) Safely accesses the property if not nil
22c) ?
23a) Yes
24a) 42
25b) if let creates local scope, guard let exits on failure
26d) All of the above
27d) Both a and b
28b) No, never
29a) Hello
30c) Both a and b

Use a Blank Sheet, Note your Answers and Finally tally with our answer at last. Give Yourself Score.

X
error: Content is protected !!
Scroll to Top