MCQs on Object-Oriented Programming in Swift | Swift

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

  1. 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
  2. Which of the following supports inheritance in Swift?
    • A) Classes
    • B) Structs
    • C) Both classes and structs
    • D) Neither classes nor structs
  3. How are instances of structs passed in Swift?
    • A) By reference
    • B) By value
    • C) By pointer
    • D) By copy
  4. Which of these is NOT a feature of Swift structs?
    • A) Stored properties
    • B) Initializers
    • C) Inheritance
    • D) Computed properties
  5. Which keyword is used to define a class in Swift?
    • A) struct
    • B) type
    • C) class
    • D) object

2. Properties and Methods

  1. 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
  2. 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
  3. 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
  4. 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
  5. 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)

  1. 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
  2. Which keyword is used to define a convenience initializer in Swift?
    • A) init
    • B) convenient
    • C) convenience
    • D) default
  3. 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
  4. 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
  5. Can you define multiple initializers in a Swift struct?
    • A) Yes
    • B) No

4. Inheritance and Method Overriding

  1. 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
  2. In Swift, which class is the root class for all classes?
    • A) NSObject
    • B) BaseObject
    • C) Object
    • D) SwiftObject
  3. Which Swift construct cannot inherit from another?
    • A) Classes
    • B) Structs
    • C) Protocols
    • D) None of the above
  4. Which method can prevent inheritance in Swift?
    • A) final
    • B) sealed
    • C) private
    • D) restrict
  5. 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

  1. What is the default access level for properties and methods in Swift?
    • A) private
    • B) fileprivate
    • C) internal
    • D) public
  2. Which access level restricts access to the same source file?
    • A) private
    • B) fileprivate
    • C) internal
    • D) protected
  3. Which access level allows properties to be accessed from any module?
    • A) public
    • B) private
    • C) internal
    • D) fileprivate
  4. 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
  5. Which of the following access levels provides the least visibility?
    • A) open
    • B) public
    • C) fileprivate
    • D) private
  6. Can a private property in Swift be accessed within an extension of the same type?
    • A) Yes
    • B) No
  7. 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
  8. Which access control keyword is the most permissive in Swift?
    • A) public
    • B) internal
    • C) open
    • D) fileprivate
  9. 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
  10. Which keyword is necessary to explicitly expose a method outside its module?
    • A) public
    • B) open
    • C) internal
    • D) external

Answer Key

QnoAnswer (Option with Text)
1B) Classes are reference types, structs are value types
2A) Classes
3B) By value
4C) Inheritance
5C) class
6B) Properties that calculate a value
7A) To defer property initialization until it is first accessed
8A) Using the func keyword
9B) A method specific to a class or struct type
10C) mutating
11B) The primary initializer for a class
12C) convenience
13A) Call a designated initializer
14B) Using memberwise initialization
15A) Yes
16C) To redefine a method in a subclass
17A) NSObject
18B) Structs
19A) final
20B) The superclass method is called
21C) internal
22B) fileprivate
23A) public
24A) open allows inheritance, public does not
25D) private
26B) No
27B) To define how code elements are accessed
28C) open
29A) fileprivate restricts access to the same file, private restricts access to the same class or struct
30A) public

Use a Blank Sheet, Note your Answers and Finally tally with our answer at last. Give Yourself Score.

X
error: Content is protected !!
Scroll to Top