MCQs on Advanced Error Handling | Swift

Explore advanced error handling techniques in Swift, including custom error types, recoverable and non-recoverable errors, the Result type, and advanced error propagation strategies. Test your knowledge with 30 multiple-choice questions!


Custom Error Types (Questions 1-10)

  1. What is the keyword used to define a custom error type in Swift?
    a) custom
    b) enum
    c) struct
    d) error
  2. Which of the following is the correct way to define a custom error type in Swift?
    a) enum MyError: Error {}
    b) struct MyError: Error {}
    c) class MyError: Error {}
    d) error MyError {}
  3. What protocol must a custom error type conform to in Swift?
    a) Error
    b) CustomError
    c) Failure
    d) Exception
  4. Which statement about custom error types in Swift is false?
    a) Custom error types must conform to the Error protocol
    b) Custom error types can include associated values
    c) Custom error types can be enum or struct
    d) Custom error types can only be enums
  5. What is an associated value in a custom error type used for?
    a) Storing extra information related to the error
    b) Storing a description of the error
    c) Specifying the error type
    d) Formatting error messages
  6. What is the correct way to throw a custom error in Swift?
    a) throw MyError.invalidInput
    b) throw "MyError.invalidInput"
    c) throw new MyError.invalidInput()
    d) throw new MyError()
  7. Can you use custom error types in Swift with do-catch blocks?
    a) Yes
    b) No
  8. How can you make a custom error type more descriptive?
    a) By adding description property
    b) By using error codes
    c) By including associated values
    d) By adding a default message
  9. What happens when a throw statement is encountered in a function?
    a) The function stops execution and returns a value
    b) The function continues executing
    c) The program crashes
    d) An exception is raised, and control is passed to a catch block
  10. Which of the following can be an associated value for a custom error type?
    a) String
    b) Int
    c) Bool
    d) All of the above

Recoverable and Non-Recoverable Errors (Questions 11-16)

  1. What is the primary difference between recoverable and non-recoverable errors?
    a) Recoverable errors can be handled with do-catch; non-recoverable cannot
    b) Recoverable errors allow you to retry; non-recoverable do not
    c) Non-recoverable errors do not generate error messages
    d) Recoverable errors occur during runtime; non-recoverable happen during compile time
  2. Which type of error should be classified as recoverable?
    a) File not found
    b) Network timeout
    c) Division by zero
    d) Invalid input format
  3. How can you handle recoverable errors in Swift?
    a) By using try?
    b) By using try!
    c) By using do-catch with retries
    d) By using fatalError()
  4. Which of the following is a non-recoverable error?
    a) Out of memory
    b) Network connection failure
    c) File permissions error
    d) Invalid user input
  5. Can non-recoverable errors be handled by a catch block?
    a) Yes, with appropriate error handling
    b) No, they cause the program to terminate
  6. What happens if a recoverable error is not caught using do-catch?
    a) The program continues execution normally
    b) A warning is shown to the user
    c) The error is ignored
    d) The program crashes

Result Type (Result<Success, Failure>) (Questions 17-22)

  1. What does the Result type represent in Swift?
    a) Success and failure values with associated data
    b) Only success with a success message
    c) Failure only with an error message
    d) A data type that cannot be used with errors
  2. How is the Result type initialized in Swift?
    a) Result.success(value)
    b) Result.failure(error)
    c) Result(success: value, error: error)
    d) Both a and b
  3. What does Result<Success, Failure> do when using the success case?
    a) Returns the associated value of type Success
    b) Returns an error
    c) Indicates a failed operation
    d) Returns nothing
  4. Which statement about Result is false?
    a) Result can hold either a success or failure case
    b) Result is an enum type
    c) You cannot define your own success and failure types
    d) Result can be used to wrap errors and values
  5. How do you access the value from a Result type when it is successful?
    a) result.success
    b) result.successValue
    c) result.get()
    d) result.value
  6. What is the best use case for using Result?
    a) When you expect a value and want to handle success/failure explicitly
    b) For handling exceptions during runtime
    c) For managing thread execution
    d) For synchronous data processing

Advanced Error Propagation (Questions 23-30)

  1. What does error propagation allow in Swift?
    a) Passing errors from one function to another
    b) Creating custom error types
    c) Handling errors at the point of failure
    d) Ignoring errors and continuing execution
  2. Which of the following is a method for propagating errors in Swift?
    a) try!
    b) try?
    c) throw
    d) All of the above
  3. What happens when you use try! in Swift?
    a) The program throws an error if an exception occurs
    b) The function continues execution regardless of errors
    c) The error is propagated without any handling
    d) The program crashes if an error occurs
  4. What is the difference between try? and try! in Swift?
    a) try? returns an optional, while try! forces a runtime error
    b) try? will always handle errors, while try! ignores them
    c) try? causes an immediate crash, while try! handles errors
    d) There is no difference
  5. How can you handle errors from asynchronous functions in Swift?
    a) By using async try and await
    b) By using do-catch with a Task
    c) By using try? inside a closure
    d) By ignoring errors with ignoreErrors()
  6. What does the throws keyword indicate in a function declaration?
    a) The function may throw an error during execution
    b) The function must throw an error
    c) The function handles errors internally
    d) The function is guaranteed to succeed
  7. What type of error handling is recommended when dealing with multiple error conditions in a function?
    a) Nested do-catch blocks
    b) Using Result type for better clarity
    c) Returning optional values
    d) Throwing runtime errors
  8. When is it appropriate to use try? for error propagation?
    a) When you expect an error but want to handle it as nil
    b) When you expect a success and don’t care about errors
    c) When you want to crash the program on errors
    d) When the error is non-recoverable

Answer Key

QNoAnswer (Option with Text)
1b) enum
2a) enum MyError: Error {}
3a) Error
4d) Custom error types can only be enums
5a) Storing extra information related to the error
6a) throw MyError.invalidInput
7a) Yes
8c) By including associated values
9a) The function stops execution and returns a value
10d) All of the above
11b) Recoverable errors allow you to retry; non-recoverable do not
12b) Network timeout
13c) By using do-catch with retries
14a) Out of memory
15b) No, they cause the program to terminate
16a) The program continues execution normally
17a) Success and failure values with associated data
18d) Both a and b
19a) Returns the associated value of type Success
20c) You cannot define your own success and failure types
21c) result.get()
22a) When you expect a value and want to handle success/failure explicitly
23a) Passing errors from one function to another
24d) All of the above
25d) The program crashes if an error occurs
26a) try? returns an optional, while try! forces a runtime error
27b) By using do-catch with a Task
28a) The function may throw an error during execution
29b) Using Result type for better clarity
30a) When you expect an error but want to handle it as nil

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