Swift MCQ

Test your understanding of Swift’s fundamental concepts with this beginner-friendly quiz. Each question comes with an answer and a brief explanation to clarify the concept.

1. Which of the following is the correct way to declare a constant in Swift?

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

Answer:

a) let constantName = value

Explanation:

In Swift, constants are declared using the let keyword.

2. Which data type is used to store textual data in Swift?

a) char
b) text
c) String
d) str

Answer:

c) String

Explanation:

Swift uses the String data type to store textual data.

3. What does the following code print?

var number: Int? = nil
print(number ?? "No value")
a) nil
b) No value
c) 0
d) Error

Answer:

b) No value

Explanation:

The ?? operator provides a default value (“No value”) if the optional (number) contains nil.

4. Which of the following is not a valid loop in Swift?

a) for
b) while
c) repeat
d) loop

Answer:

d) loop

Explanation:

Swift has for, while, and repeat-while loops. There is no loop keyword.

5. Which Swift feature allows you to group multiple values together?

a) Class
b) Array
c) Tuple
d) Dictionary

Answer:

c) Tuple

Explanation:

A tuple allows you to group multiple values together, possibly of different types.

6. What does the following code declare?

var names: [String] = ["Anna", "Bob", "Charlie"]
a) An array of strings
b) A dictionary with string keys
c) A set of strings
d) A tuple of strings

Answer:

a) An array of strings

Explanation:

The syntax specifies an array of String type.

7. Which operator is used for string concatenation?

a) +
b) &
c) .
d) *

Answer:

a) +

Explanation:

In Swift, the + operator is used to concatenate strings.

8. How can you safely unwrap an optional in Swift?

a) Force unwrap
b) Optional binding
c) Implicit unwrap
d) Unwrap

Answer:

b) Optional binding

Explanation:

Optional binding (using if let or guard let) is a safe way to unwrap optionals without risking a runtime crash.

9. Which of the following is a valid function declaration in Swift?

a) func functionName() -> return
b) function functionName() {}
c) def functionName() {}
d) func functionName() {}

Answer:

d) func functionName() {}

Explanation:

In Swift, functions are declared using the func keyword.

10. Which keyword is used to declare a class in Swift?

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

Answer:

a) class

Explanation:

The class keyword is used to declare classes in Swift.

11. What does the ! symbol represent when used after a variable?

a) It forces the variable to be optional.
b) It casts the variable to a different type.
c) It force unwraps the optional.
d) It checks if the variable is nil.

Answer:

c) It force unwraps the optional.

Explanation:

The ! symbol, when used after an optional, forcefully unwraps it. This can lead to a runtime crash if the optional is nil.

12. Which collection is unordered and stores unique values?

a) Array
b) Set
c) Dictionary
d) List

Answer:

b) Set

Explanation:

Sets are collections of unique values that are unordered.

13. Which type alias represents a function type in Swift?

a) Type
b) Func
c) Closure
d) Method

Answer:

c) Closure

Explanation:

In Swift, closures represent blocks of self-contained code, and they can be thought of as function types.

14. Which operator checks for object identity?

a) ==
b) ===
c) !=
d) !==

Answer:

b) ===

Explanation:

The === operator checks if two object references refer to the same instance.

15. Which is a valid dictionary declaration?

a) var dictionary: [Key: Value]
b) var dictionary: Key -> Value
c) var dictionary: Key, Value
d) var dictionary: [Key, Value]

Answer:

a) var dictionary: [Key: Value]

Explanation:

Dictionaries in Swift use a [Key: Value] syntax for declaration.

16. Which keyword is used to handle exceptions in Swift?

a) catch
b) error
c) except
d) fault

Answer:

a) catch

Explanation:

Swift uses do, try, and catch for exception handling.

17. Which of the following is not a valid access control modifier in Swift?

a) private
b) protected
c) public
d) fileprivate

Answer:

b) protected

Explanation:

Swift doesn’t have a protected access level. The available ones are public, internal, fileprivate, and private.

18. Which protocol is used to check for equality?

a) Equatable
b) Comparable
c) Equal
d) Matchable

Answer:

a) Equatable

Explanation:

The Equatable protocol is used to check for equality between instances.

19. Which is the correct way to define an enumeration in Swift?

a) enum Name {case, case}
b) enumerate Name: case, case
c) enum Name: {case, case}
d) enum Name {case case1, case case2}

Answer:

d) enum Name {case case1, case case2}

Explanation:

Enumerations are defined using the enum keyword, followed by cases inside curly braces.

20. Which Swift feature ensures type safety at compile-time?

a) Dynamic typing
b) Static typing
c) Type inference
d) Type conversion

Answer:

b) Static typing

Explanation:

Swift is a statically-typed language, which means type checking is done at compile-time.

21. Which of the following denotes a multiline string?

a) “””…”””
b) ”’…”’
c) “…”
d) ‘…’

Answer:

a) “””…”””

Explanation:

In Swift, multiline strings are enclosed in triple double quotes.

22. Which of these is not a valid numeric type in Swift?

a) Int
b) Double
c) Float
d) Short

Answer:

d) Short

Explanation:

Swift doesn’t have a Short numeric type. Instead, it has Int, UInt, Double, and Float.

23. Which method is called when an instance of a class is deallocated?

a) deinit
b) dealloc
c) delete
d) destroy

Answer:

a) deinit

Explanation:

The deinit method is called just before an instance of a class is deallocated.

24. Which operator allows you to provide a default value for an optional?

a) ?.
b) !!
c) ??
d) !?

Answer:

c) ??

Explanation:

The ?? operator (nil coalescing operator) provides a default value if the optional is nil.

25. Which of these is the base protocol for most Swift standard library types?

a) Base
b) Object
c) Any
d) Element

Answer:

c) Any

Explanation:

Any can represent an instance of any type, and is the base type for most types in Swift.


I hope this quiz has helped you gauge your understanding of Swift’s fundamentals. Whether you’re just starting out or reviewing concepts, remember that practice and continuous learning are key to mastering any programming language. Happy coding!

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top