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)
What keyword is used to define an enumeration in Swift?
a) enum
b) enumeration
c) struct
d) case
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
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
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
Which of the following is a valid enumeration case declaration?
a) case sun = "Sunday"
b) case .monday
c) case "Tuesday"
d) case(wednesday)
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)
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
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)
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
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
Which code snippet correctly handles associated values with a switch statement?
a)swiftCopy codecase let .result(name, score): print(name, score)