Swift Quiz - MCQ Questions and Answers

Welcome to our Swift Quiz! This quiz includes 25 multiple-choice questions to assess your understanding of Swift, the powerful language used to develop iOS and macOS applications. The questions range from basic topics, such as syntax and data types, to more complex areas, like optionals, closures, and protocol-oriented programming. 

Whether you're new to Swift or looking to sharpen your existing skills, this quiz offers a comprehensive evaluation of your knowledge. Dive in and discover how well you know Swift—this is a great chance to challenge yourself and enhance your programming capabilities!

1. What is Swift primarily used for?

a) Server-side applications
b) Mobile and desktop apps
c) Web development
d) System programming

2. Which keyword is used to declare a constant in Swift?

a) var
b) let
c) const
d) static

3. Swift is a type-safe language. What does this mean?

a) Swift emphasizes using strings.
b) Swift ensures you are clear about the types of values your code can work with.
c) Swift requires items to be initialized as arrays.
d) Swift only works with user-defined types.

4. What is the output of this Swift code?

var fruits = ["Apple", "Banana"]
fruits.append("Cherry")
print(fruits.count)
a) 2
b) 3
c) 4
d) Error

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

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

6. Which statement is used to handle optional values in Swift?

a) guard
b) check
c) inspect
d) monitor

7. What does the following Swift code print?

let numbers = [1, 2, 3]
var sum = 0
for number in numbers {
    sum += number
}
print(sum)
a) 6
b) 3
c) 1
d) 0

8. What is an unwrapped optional in Swift?

a) A method to check for nil values
b) A variable that might not contain a value
c) A value extracted from an optional, assumed to not be nil
d) A constant that cannot be nil

9. What does the != operator do in Swift?

a) Assigns a value if not equal
b) Compares two values to determine if they are not equal
c) Checks if a variable is not nil
d) None of the above

10. How do you define a custom structure in Swift?

struct Person {
    var name: String
    var age: Int
}
a) Class definition
b) Function definition
c) Structure definition
d) Enumeration definition

11. What is the role of the protocol in Swift?

a) A set of methods and properties that suit a particular task or piece of functionality
b) A method to send data over the internet
c) A way to define optional properties
d) None of the above

12. What will the following Swift code output?

func multiply(_ a: Int, _ b: Int) -> Int {
    return a * b
}
print(multiply(2, 3))
a) 5
b) 6
c) Error
d) None of the above

13. What is a computed property in Swift?

a) A property that logs calculations
b) A stored property with a setter and getter
c) A property that directly calculates its value
d) An inherited property

14. How do you create an instance of a class in Swift?

class Vehicle {
    var type = ""
}
let car = Vehicle()
a) Error, missing constructor
b) Correct instantiation
c) Error, incorrect syntax
d) None of the above

15. What is the use of the dynamic keyword in Swift?

a) To declare variables that change types
b) To interact with Objective-C APIs and dynamic libraries
c) To enable dynamic type casting
d) To declare constants

16. What does the following Swift code snippet demonstrate?

var name: String?
name = "John"
print(name!)
a) Optional binding
b) Forced unwrapping of an optional
c) Error handling
d) Implicit unwrapping

17. In Swift, how do you ensure a piece of code is only executed once, regardless of how many times it is called?

a) Using the static keyword
b) Using the dispatch_once function
c) Using a global variable
d) Using the once token

18. How does Swift handle memory management for classes?

a) Manual reference counting
b) Automatic reference counting (ARC)
c) Garbage collection
d) Swift does not handle memory management

19. What will the following Swift code print?

let numbers = Set([1, 2, 3, 2])
print(numbers.count)
a) 3
b) 4
c) 2
d) Error

20. What is the significance of weak keyword in Swift?

a) It declares a variable that cannot be nil
b) It prevents retain cycles by creating a weak reference
c) It is used to define strong types
d) It enhances variable performance

21. How do you handle errors in Swift?

enum PrinterError: Error {
    case outOfPaper, noToner, onFire
}
func send(job: Int, toPrinter printerName: String) throws -> String {
    if printerName == "Never Has Toner" {
        throw PrinterError.noToner
    }
    return "Job sent"
}
do {
    let printerResponse = try send(job: 1040, toPrinter: "Never Has Toner")
    print(printerResponse)
} catch {
        print("Printer error: \(error)")
}
a) By using try and catch
b) By using on and off
c) By using validate and invalidate
d) By using success and failure

22. What will the following Swift code output?

var i = 1
repeat {
    i *= 2
} while i < 100
print(i)
a) 128
b) 64
c) 100
d) Error

23. How is a tuple used in Swift?

a) To manage external libraries
b) To group multiple values into a single compound value
c) To declare constants only
d) To implement error handling

24. What does the following Swift code demonstrate?

let names = ["Anna", "Alex", "Brian", "Jack"]
for name in names {
    print("Hello, \(name)!")
}
a) Array iteration using a for-in loop
b) Creation of a new array
c) Use of a while loop
d) Error handling with arrays

25. How do you declare a variable in Swift that can hold a nil value?

a) Using the nil keyword
b) By declaring it as const
c) By declaring it as an optional
d) By initializing it with zero

Comments