iOS Development Swift Programming Language 1 — Questions and Answers
Question 1: What keyword is used in Swift to declare a constant?
- let (Correct answer)
- var
- const
- final
Correct answer: let
In Swift, `let` declares a constant whose value cannot be changed after it is set.
Question 2: Which Swift feature allows a variable to hold either a value or nil?
- Optional (Correct answer)
- Enum
- Tuple
- Closure
Correct answer: Optional
Swift Optionals allow a variable to represent either a wrapped value or the absence of a value (nil).
Question 3: What does the `guard` statement do in Swift?
- Exits the current scope if a condition is false (Correct answer)
- Handles errors thrown by a function
- Iterates over a sequence
- Declares a deferred action
Correct answer: Exits the current scope if a condition is false
`guard` evaluates a condition and requires early exit from the current scope if the condition is false.
Question 4: Which Swift collection type stores key-value pairs?
- Dictionary (Correct answer)
- Array
- Set
- Tuple
Correct answer: Dictionary
A Swift Dictionary stores associations between keys of one type and values of another type.
Question 5: What is a Swift closure?
- A self-contained block of functionality that can be passed around (Correct answer)
- A method that initializes a class
- A type alias for a protocol
- A property observer
Correct answer: A self-contained block of functionality that can be passed around
Closures are self-contained blocks of code that can capture and store references to variables from their surrounding context.
Question 6: Which Swift keyword is used to handle thrown errors?
- do-catch (Correct answer)
- try-except
- guard-else
- if-let
Correct answer: do-catch
Swift uses `do-catch` blocks to catch and handle errors thrown by functions marked with `throws`.
What keyword is used in Swift to declare a constant?