Swift Online Quiz

Welcome to our Swift Online Quiz, crafted to assess and enhance your understanding of Swift, Apple's powerful and intuitive programming language designed for iOS, macOS, watchOS, and tvOS app development. This quiz covers a broad spectrum of Swift's features, from basic syntax and control flow to advanced topics such as closures and protocols. Whether you are preparing for a job interview or just want to test your Swift knowledge, this quiz offers a variety of questions to challenge you.

1. What is the primary purpose of the Optional type in Swift?

a) To provide a method for looping through collections
b) To handle the absence of a value
c) To declare constants
d) To manage memory explicitly

2. How do you declare a constant in Swift?

a) var constantName = value
b) let constantName = value
c) const constantName = value
d) static constantName = value

3. What is the output of the following Swift code?

var numbers = [1, 2, 3]
a) 3
b) 4
c) Error
d) 1

4. In Swift, what keyword is used to define a new class?

a) class
b) new
c) struct
d) object

5. What does the following Swift code snippet output?

let firstPart = "Hello, "
let secondPart = "world!"
print(firstPart + secondPart)
a) Hello, world!
b) Hello,
c) world!
d) Compilation error

6. Which method in Swift is automatically called when an instance of a class is created?

a) init()
b) start()
c) new()
d) class()

7. What is the output of the following Swift code?

var score = 5
score *= 3
a) 15
b) 8
c) 5
d) 3

8. How do you define a dictionary in Swift with keys of type String and values of type Int?

a) var dict: [String: Int] = [:]
b) var dict = [String, Int]()
c) var dict = Dictionary<String, Int>()
d) var dict: Dictionary<String, Int> = []

9. What will the following Swift code output?

func addNumbers(a: Int, b: Int) -> Int {
    return a + b
print(addNumbers(a: 5, b: 3))
a) 8
b) 5
c) 3
d) Error

10. What is the use of the weak keyword in Swift?

a) To declare a variable that does not participate in reference counting
b) To prevent strong reference cycles in closures
c) To indicate a lower priority variable
d) To create a weak reference to an instance that does not prevent it from being deallocated

11. What keyword is used to define protocols in Swift?

a) protocol
b) interface
c) delegate
d) blueprint

12. How can you handle errors in Swift?

a) try...catch
b) do...catch
c) try...except
d) error...catch

13. What does the final keyword do when used in a class declaration in Swift?

a) Prevents the class from being subclassed
b) Indicates that the class is complete
c) Ensures that the class cannot be modified
d) None of the above

14. What is the output of the following Swift code?

let numbers = [10, 20, 30, 40, 50]
let filteredNumbers = numbers.filter { $0 < 40 }
a) 3
b) 4
c) 5
d) 2

15. What is the output of the following Swift code?

var number: Int? = nil
var numberString = number?.description ?? "None"
a) nil
b) "None"
c) "0"
d) ""

16. In Swift, how do you declare a variable that might store nil at some point during its lifecycle?

a) var variable: Optional
b) var variable: Any?
c) var variable: String!
d) var variable: Int?

17. What is the correct way to define an enumeration in Swift?

a) enum Directions { case north, south, east, west }
b) enum Directions -> { north, south, east, west }
c) enum Directions: { north, south, east, west }
d) Directions enum { north, south, east, west }

18. What is the purpose of the guard statement in Swift?

a) To guarantee that a value meets certain conditions
b) To provide a more readable form of an if statement
c) To execute a block of code as long as a condition is true
d) To transfer control out of a scope if one or more conditions aren’t met

19. What does the following Swift code output?

var numbers = Set([1, 2, 3])
a) 3
b) 4
c) 5
d) 2

20. How do you perform type casting in Swift?

a) Using the cast keyword
b) Using the typecast operator
c) Using the as keyword
d) Using the convert method

21. What is the use of the override keyword in Swift?

a) To change the implementation of a base class method in a subclass
b) To prevent a method from being overridden
c) To mark a method as requiring an override in a subclass
d) To denote that a variable is being re-declared

22. What is the output of the following Swift code?

func multiply(n1: Int, n2: Int) -> Int {
    return n1 * n2
print(multiply(n1: 3, n2: 4))
a) 7
b) 12
c) 0
d) 1

23. What does the dynamic keyword do when used with a property in Swift?

a) Makes the property non-static
b) Allows the property to bypass Swift’s type system
c) Enables the property to be observed for changes at runtime
d) None of the above

24. What is the purpose of the @escaping annotation in a function parameter in Swift?

a) It indicates that the parameter will be stored after the function returns
b) It guarantees that the parameter will not be modified
c) It prevents the parameter from being captured by closures
d) It forces the parameter to be evaluated immediately

25. How can you ensure that a piece of code blocks is executed only once in Swift, regardless of how many times a function is called?

a) Using the static keyword
b) Using the once block
c) Using the dispatch_once function
d) Using the singleton pattern