MCQs on Enumerations | Swift

Explore enumerations in Swift, a powerful way to define groups of related values. Learn about defining enums, using associated values, working with raw values, and adding methods for enhanced functionality.


MCQs on Enumerations in Swift

Section 1: Defining Enumerations (6 Questions)

  1. What keyword is used to define an enumeration in Swift?
    • a) enum
    • b) enumeration
    • c) struct
    • d) case
  2. How do you declare an enumeration in Swift?
    • a) enum Direction { case north, south, east, west }
    • b) struct Direction { case north, south, east, west }
    • c) direction { enum north, south, east, west }
    • d) let enum Direction: north, south, east, west
  3. What is the default value of an enumeration case if no raw value is specified?
    • a) String representation of the case name
    • b) Integer starting from 0
    • c) nil
    • d) Boolean
  4. Can Swift enumerations store additional information alongside each case?
    • a) No, cases cannot store additional information
    • b) Yes, with associated values
    • c) Only if raw values are provided
    • d) Only for string-based enums
  5. Which of the following is a valid enumeration case declaration?
    • a) case sun = "Sunday"
    • b) case .monday
    • c) case "Tuesday"
    • d) case(wednesday)
  6. What is the primary purpose of enumerations in Swift?
    • a) To group related values into a type-safe structure
    • b) To define classes with specific methods
    • c) To replace all constant declarations
    • d) To handle only string-based constants

Section 2: Associated Values (6 Questions)

  1. What are associated values in Swift enumerations used for?
    • a) Assigning default raw values
    • b) Storing additional information with each case
    • c) Making enums act as structs
    • d) Handling integer-based cases
  2. Which of the following syntax is correct for defining associated values in an enumeration?
    • a) case result(String, Int)
    • b) case result = (String, Int)
    • c) case result[String: Int]
    • d) case result -> (String, Int)
  3. How do you retrieve associated values from an enum case?
    • a) By type casting the enum instance
    • b) Using a switch statement or if case syntax
    • c) Accessing the rawValue property
    • d) Using direct subscripting
  4. Can multiple cases in a Swift enumeration have associated values?
    • a) No, only one case can have associated values
    • b) Yes, but they must share the same type
    • c) Yes, each case can define its own type
    • d) No, associated values are limited to structs
  5. Which code snippet correctly handles associated values with a switch statement?
    • a)swiftCopy codecase let .result(name, score): print(name, score)
    • b)swiftCopy codecase .result = (name, score): print(name, score)
    • c)swiftCopy codecase result: print(result)
    • d)swiftCopy codecase .result(name: String, score: Int): print(name, score)
  6. What is the key difference between associated values and raw values?
    • a) Raw values are predefined, while associated values store dynamic data
    • b) Associated values must be integers
    • c) Raw values are used for methods, associated values are not
    • d) Raw values cannot coexist with associated values

Section 3: Raw Values (6 Questions)

  1. What are raw values in Swift enumerations?
    • a) Values automatically assigned to enum cases
    • b) Predefined constant values for each case
    • c) Additional data stored with each case
    • d) String-based identifiers for enum cases
  2. Which data types can be used for raw values in Swift?
    • a) Only integers
    • b) Strings and integers
    • c) Strings, integers, floats, and characters
    • d) Any type that conforms to Codable
  3. How do you assign raw values to an enumeration case?
    • a) case north: "N"
    • b) case north = "N"
    • c) case .north -> "N"
    • d) case north(rawValue: "N")
  4. What happens if duplicate raw values are assigned in an enumeration?
    • a) It causes a compile-time error
    • b) It overwrites previous values
    • c) The first case is prioritized
    • d) It generates unique identifiers automatically
  5. How can you access the raw value of an enum case?
    • a) Using .rawValue property
    • b) By directly casting the enum
    • c) Through the associatedValue method
    • d) Using .value property
  6. Which method allows initializing an enumeration from a raw value?
    • a) init(rawValue:)
    • b) enum(rawValue:)
    • c) fromRawValue()
    • d) rawValue(init:)

Section 4: Enumerations with Methods (6 Questions)

  1. Can Swift enumerations have methods?
    • a) Yes, instance methods can be added
    • b) No, enums are not designed for methods
    • c) Only if the enum has raw values
    • d) Only static methods are allowed
  2. Which syntax correctly defines a method within an enumeration?
    • a)swiftCopy codefunc printDetails() { print("Enum details") }
    • b)swiftCopy codeenum Example { case detail func show() { print("Detail") } }
    • c)swiftCopy codeenum Example: show() { print("Detail") }
    • d) None of the above
  3. What is the primary purpose of adding methods to enumerations?
    • a) To execute common logic related to cases
    • b) To manage associated values directly
    • c) To provide default raw values
    • d) To handle enum errors
  4. Can enum methods in Swift be static?
    • a) No, only instance methods are allowed
    • b) Yes, static methods are supported
    • c) Static methods are not recommended
    • d) Only in raw value enums
  5. How can you refer to the current case within an enum method?
    • a) Using self
    • b) Using .case
    • c) Using this
    • d) Using enum.case
  6. What is a potential use case for enums with methods?
    • a) Calculating derived values
    • b) Dynamically converting cases to strings
    • c) Simplifying switch statements
    • d) All of the above

Section 5: Miscellaneous Enumeration Topics (6 Questions)

  1. Which feature allows enums to conform to protocols in Swift?
    • a) Enums cannot conform to protocols
    • b) Extensions
    • c) Protocol inheritance
    • d) Raw values
  2. What is the default numbering for integer-based enums in Swift?
    • a) Starting at 1
    • b) Starting at 0
    • c) Randomized
    • d) Based on case declaration order
  3. Can enumerations in Swift be nested within other types?
    • a) Yes, enums can be nested in classes, structs, or other enums
    • b) No, enums must always be top-level types
    • c) Only within classes
    • d) Only within structs
  4. How can you iterate through all cases of an enum?
    • a) Conform to CaseIterable protocol
    • b) Use for case syntax
    • c) Enumerations cannot be iterated
    • d) Use rawValue directly
  5. Can enums have stored properties in Swift?
    • a) No, enums do not support stored properties
    • b) Yes, but only with raw values
    • c) Yes, but only with associated values
    • d) Yes, with explicit initializers
  6. What is a raw-value enumeration called when the values represent keys or unique identifiers?
    • a) String-key enums
    • b) Hashable enums
    • c) Codable enums
    • d) Key-value enums

Answer Key

QnoAnswer
1a) enum
2a) enum Direction { case north, south, east, west }
3b) Integer starting from 0
4b) Yes, with associated values
5a) case sun = "Sunday"
6a) To group related values into a type-safe structure
7b) Storing additional information with each case
8a) case result(String, Int)
9b) Using a switch statement or if case syntax
10c) Yes, each case can define its own type
11a) case let .result(name, score): print(name, score)
12a) Raw values are predefined, while associated values store dynamic data
13b) Predefined constant values for each case
14c) Strings, integers, floats, and characters
15b) case north = "N"
16a) It causes a compile-time error
17a) Using .rawValue property
18a) init(rawValue:)
19a) Yes, instance methods can be added
20b) enum Example { case detail; func show() { print("Detail") } }
21a) To execute common logic related to cases
22b) Yes, static methods are supported
23a) Using self
24d) All of the above
25b) Extensions
26b) Starting at 0
27a) Yes, enums can be nested in classes, structs, or other enums
28a) Conform to CaseIterable protocol
29a) No, enums do not support stored properties
30b) Hashable enums

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