Master the core concepts of object-oriented programming in Swift with these 30 MCQs. Learn about classes, structs, initializers, inheritance, method overriding, and access control in an engaging way.
1. Classes and Structs: Definitions and Differences
What is the primary difference between a class and a struct in Swift?
A) Classes are value types, structs are reference types
B) Classes are reference types, structs are value types
C) Classes are immutable, structs are mutable
D) Structs support inheritance, classes do not
Which of the following supports inheritance in Swift?
A) Classes
B) Structs
C) Both classes and structs
D) Neither classes nor structs
How are instances of structs passed in Swift?
A) By reference
B) By value
C) By pointer
D) By copy
Which of these is NOT a feature of Swift structs?
A) Stored properties
B) Initializers
C) Inheritance
D) Computed properties
Which keyword is used to define a class in Swift?
A) struct
B) type
C) class
D) object
2. Properties and Methods
What are “computed properties” in Swift?
A) Properties with default values
B) Properties that calculate a value
C) Immutable properties
D) Properties defined in structs only
What is the purpose of the lazy keyword in Swift properties?
A) To defer property initialization until it is first accessed
B) To make the property immutable
C) To initialize a property at compile time
D) To mark a property as optional
How are “methods” defined in Swift?
A) Using the func keyword
B) Using the method keyword
C) Using the def keyword
D) Using the init keyword
What is a “type method” in Swift?
A) A method that operates on instance variables
B) A method specific to a class or struct type
C) A method used for debugging
D) A method that overrides another method
Which of the following is used to update a property within a method in Swift?
A) modify
B) set
C) mutating
D) update
3. Initializers (Designated and Convenience)
What is a “designated initializer” in Swift?
A) An initializer that initializes only some properties
B) The primary initializer for a class
C) An initializer with default values
D) An initializer defined in a struct
Which keyword is used to define a convenience initializer in Swift?
A) init
B) convenient
C) convenience
D) default
What must a convenience initializer in a class do?
A) Call a designated initializer
B) Initialize all properties directly
C) Return a default value
D) Call another convenience initializer
In a struct, how are properties initialized by default?
A) Using a designated initializer
B) Using memberwise initialization
C) Using a convenience initializer
D) Using a default initializer
Can you define multiple initializers in a Swift struct?
A) Yes
B) No
4. Inheritance and Method Overriding
What is the purpose of the override keyword in Swift?
A) To indicate a property is optional
B) To mark a method as final
C) To redefine a method in a subclass
D) To stop inheritance
In Swift, which class is the root class for all classes?
A) NSObject
B) BaseObject
C) Object
D) SwiftObject
Which Swift construct cannot inherit from another?
A) Classes
B) Structs
C) Protocols
D) None of the above
Which method can prevent inheritance in Swift?
A) final
B) sealed
C) private
D) restrict
What is the default behavior if a subclass does not override a superclass method in Swift?
A) A runtime error occurs
B) The superclass method is called
C) The method cannot be accessed
D) The subclass method is used
5. Access Control: private, fileprivate, internal, public, open
What is the default access level for properties and methods in Swift?
A) private
B) fileprivate
C) internal
D) public
Which access level restricts access to the same source file?
A) private
B) fileprivate
C) internal
D) protected
Which access level allows properties to be accessed from any module?
A) public
B) private
C) internal
D) fileprivate
How does open differ from public in Swift?
A) open allows inheritance, public does not
B) public allows inheritance, open does not
C) Both are the same
D) open restricts usage to the same module
Which of the following access levels provides the least visibility?
A) open
B) public
C) fileprivate
D) private
Can a private property in Swift be accessed within an extension of the same type?
A) Yes
B) No
What is the purpose of access control in Swift?
A) To reduce memory usage
B) To define how code elements are accessed
C) To manage program flow
D) To restrict variable data types
Which access control keyword is the most permissive in Swift?
A) public
B) internal
C) open
D) fileprivate
How is fileprivate different from private in Swift?
A) fileprivate restricts access to the same file, private restricts access to the same class or struct
B) They are identical
C) fileprivate is less restrictive than open
D) fileprivate can only be used with structs
Which keyword is necessary to explicitly expose a method outside its module?
A) public
B) open
C) internal
D) external
Answer Key
Qno
Answer (Option with Text)
1
B) Classes are reference types, structs are value types
2
A) Classes
3
B) By value
4
C) Inheritance
5
C) class
6
B) Properties that calculate a value
7
A) To defer property initialization until it is first accessed
8
A) Using the func keyword
9
B) A method specific to a class or struct type
10
C) mutating
11
B) The primary initializer for a class
12
C) convenience
13
A) Call a designated initializer
14
B) Using memberwise initialization
15
A) Yes
16
C) To redefine a method in a subclass
17
A) NSObject
18
B) Structs
19
A) final
20
B) The superclass method is called
21
C) internal
22
B) fileprivate
23
A) public
24
A) open allows inheritance, public does not
25
D) private
26
B) No
27
B) To define how code elements are accessed
28
C) open
29
A) fileprivate restricts access to the same file, private restricts access to the same class or struct