Swift Online Quiz Test

Get ready to test your Swift programming skills with our Swift Online Quiz Test! This quiz includes 25 multiple-choice questions that cover the key features and functionalities of Swift, the powerful language used primarily for developing iOS and macOS applications.

Whether you’re a beginner looking to assess your learning or an experienced developer aiming to refresh your knowledge, this quiz is an excellent opportunity to enhance your Swift programming expertise. Let’s get started and see how well you know Swift!

1. What is the purpose of the var keyword in Swift?

a) To declare a constant
b) To declare a variable
c) To declare a function
d) To declare a type

2. Which method is used to handle optional values in Swift?

a) try
b) guard
c) optional binding
d) forced unwrapping

3. What does the following Swift code print?

var a: Int? = nil
var b = a ?? 10
print(b)
a) nil
b) 10
c) 0
d) Error

4. How do you declare a constant in Swift?

a) var constantValue = 100
b) const constantValue = 100
c) let constantValue = 100
d) static constantValue = 100

5. What is a tuple in Swift?

a) A function that returns multiple values
b) An ordered, immutable list of elements
c) A closure
d) An optional type

6. Which Swift structure are you using when you write the following code?

enum CompassPoint {
    case north
    case south
    case east
    case west
}
a) Class
b) Enumeration
c) Protocol
d) Array

7. What is the primary use of protocols in Swift?

a) To manage memory manually
b) To define a blueprint of methods, properties, and other requirements
c) To check the type of variables
d) To declare constants

8. How do you define a computed property in Swift?

a) var isAtOrigin: Bool { return x == 0.0 && y == 0.0 }
b) let isAtOrigin = (x == 0.0 && y == 0.0)
c) func isAtOrigin() -> Bool { return x == 0.0 && y == 0.0 }
d) static isAtOrigin = x == 0.0 && y == 0.0

9. What is the Swift keyword to handle memory cycles when capturing self in a closure?

a) strong
b) weak
c) unowned
d) var

10. Which keyword is used for error handling in Swift?

a) throw
b) throws
c) throwable
d) error

11. How do you create an array in Swift?

a) var names: [String] = ["Anna", "Alex", "Brian", "Jack"]
b) var names = array["Anna", "Alex", "Brian", "Jack"]
c) var names = ("Anna", "Alex", "Brian", "Jack")
d) var names = { "Anna", "Alex", "Brian", "Jack" }

12. What is the use of defer in Swift?

a) To delay the execution of code until the current scope is exited
b) To define a block of code that runs in a separate thread
c) To declare variables that should be deleted after use
d) To define constants

13. How do you implement inheritance in Swift?

a) class Bicycle: Vehicle { var hasBasket = false }
b) class Bicycle inherits Vehicle { var hasBasket = false }
c) class Bicycle implements Vehicle { var hasBasket = false }
d) class Bicycle extends Vehicle { var hasBasket = false }

14. What does @IBAction do in Swift?

a) Declares a method as an action that can respond to events in your app interface.
b) Automatically saves the state of the application.
c) Creates a binding between a variable and an interface element.
d) Marks a function to run in the background.

15. What are generics used for in Swift?

a) To write flexible, reusable functions and types that can work with any type
b) To generate automatic documentation for code
c) To specify the number of elements in an array
d) To declare global constants

16. What does the dynamic keyword do in Swift?

a) It allows Swift to interact with Objective-C APIs that require the dynamic dispatch of methods.
b) It delays the type-checking of Swift code until runtime.
c) It makes the property change dynamically during runtime.
d) It forces the compiler to optimize the annotated property or method.

17. What is the purpose of optional chaining in Swift?

a) To chain multiple methods to simplify code
b) To perform several optional operations in a sequence
c) To safely access the properties, methods, and subscripts of optional values
d) To check multiple conditions in a single line

18. What is the Encodable protocol used for in Swift?

a) To ensure a class can save its data to a file
b) To allow an object to be encoded to an external representation, such as JSON
c) To check that all objects within a class are properly initialized
d) To make sure that an object can be copied

19. How do you specify that a Swift protocol should only be adoptable by class types?

a) protocol SomeProtocol: AnyObject {}
b) protocol SomeProtocol: class {}
c) protocol SomeProtocol: OnlyClass {}
d) protocol SomeProtocol: TypeClass {}

20. What is Swift's guard statement used for?

a) To exit a loop early
b) To guarantee that a value complies with a set of conditions
c) To provide a default case when all other conditions fail
d) To ensure that resources are cleaned up

21. How do you declare a dictionary in Swift?

a) var namesAndScores = ["Anna": 2, "Brian": 3, "Craig": 8]
b) var namesAndScores = (["Anna": 2, "Brian": 3, "Craig": 8])
c) var namesAndScores = dictionary["Anna": 2, "Brian": 3, "Craig": 8]
d) var namesAndScores = dict["Anna": 2, "Brian": 3, "Craig": 8]

22. What is the willSet and didSet used for in Swift?

a) To create computed properties
b) To observe and respond to changes in a property's value
c) To initialize new properties
d) To set default values for a property

23. What is the Swift String method to check if a string starts with a specific sequence of characters?

a) startsWith()
b) beginningWith()
c) hasPrefix()
d) prefix()

24. How do you make a function parameter optional in Swift?

a) func someFunction(parameter: Int? = nil)
b) func someFunction(parameter: Optional<Int> = nil)
c) func someFunction(parameter: Int = Optional.none)
d) func someFunction(parameter: Int = nil)

25. What is the use of static keyword inside a Swift class?

a) It makes a method or property belong to the type itself rather than to instances of the type.
b) It prevents the method or property from being overridden.
c) It automatically initializes the property or method when the app starts.
d) It makes the method or property thread-safe.

Comments