MCQs on Error Handling | Swift

Learn to handle runtime issues effectively with Swift’s error-handling techniques. This chapter covers error types, throwing and catching errors, the do-catch statement, and error propagation with throws.


Topic 1: Error Types in Swift

  1. What protocol must a type conform to in order to represent an error in Swift?
    a) ErrorRepresentable
    b) CustomStringConvertible
    c) Error
    d) NSError
  2. Which of the following best describes an error in Swift?
    a) A runtime exception
    b) A value of a type conforming to Error
    c) A logic failure
    d) None of the above
  3. What is a common way to define custom error types in Swift?
    a) Using struct
    b) Using enum
    c) Using class
    d) All of the above
  4. How can you associate additional information with a custom error type?
    a) By adding associated values in an enum
    b) By defining properties in a struct
    c) By defining properties in a class
    d) All of the above
  5. Which built-in Swift error type is typically used in Foundation-based APIs?
    a) Error
    b) NSError
    c) CustomError
    d) None of the above

Topic 2: Throwing and Catching Errors

  1. How do you indicate that a function can throw an error?
    a) By using throws in the function signature
    b) By using throwable in the function signature
    c) By using error in the function body
    d) By using catch in the function signature
  2. Which keyword is used to throw an error in Swift?
    a) error
    b) raise
    c) throw
    d) catch
  3. What type of value can be thrown as an error in Swift?
    a) Any type
    b) Only instances of Error
    c) Only instances of NSError
    d) Only String values
  4. What happens if an error is thrown inside a function that doesn’t declare throws?
    a) The program crashes
    b) A compile-time error occurs
    c) The error is ignored
    d) It gets automatically propagated
  5. Can a throw statement return a value in Swift?
    a) Yes, always
    b) No, never
    c) Yes, if it’s a string
    d) None of the above

Topic 3: The do-catch Statement

  1. What is the purpose of a do block in Swift?
    a) To define an error type
    b) To test a block of code for errors
    c) To propagate an error
    d) None of the above
  2. Which of the following is correct syntax for a do-catch block?
    a) do { try function() } catch { print(error) }
    b)do { function() } onError { print(error) }
    c)try { function() } catch { print(error) }
    d) None of the above
  3. What happens if no catch block matches the thrown error?
    a) The error is ignored
    b) The program crashes
    c) The error propagates further
    d) None of the above
  4. Can a do block contain multiple catch blocks?
    a) Yes
    b) No
    c) Only for specific error types
    d) None of the above
  5. How can you catch only specific error types in a do-catch block?
    a) By using if let
    b) By using case statements in catch
    c) By specifying error codes
    d) All of the above

Topic 4: Error Propagation with throws

  1. How do you indicate that a function propagates errors?
    a) Add throws to the function signature
    b) Add error to the function body
    c) Use catch in the function implementation
    d) None of the above
  2. What happens to an error that propagates out of a throws function?
    a) It must be handled by the caller
    b) It is automatically ignored
    c) It causes a crash
    d) It is converted into an exception
  3. Can an initializer in Swift throw an error?
    a) Yes, by adding throws to the initializer
    b) No, initializers cannot throw errors
    c) Only in subclasses
    d) None of the above
  4. How can you call a throws function without handling the error directly?
    a) Using try!
    b) Using try?
    c) Both a and b
    d) None of the above
  5. Which of the following is correct for error propagation?
    a)func example() throws { try anotherFunction() }
    b) func example() { try anotherFunction() }
    c) func example() error { anotherFunction() }
    d) None of the above

Answers Table

QNoAnswer
1c) Error
2b) A value of a type conforming to Error
3b) Using enum
4d) All of the above
5b) NSError
6a) By using throws in the function signature
7c) throw
8b) Only instances of Error
9b) A compile-time error occurs
10b) No, never
11b) To test a block of code for errors
12a) Correct syntax as shown
13c) The error propagates further
14a) Yes
15b) By using case statements in catch
16a) Add throws to the function signature
17a) It must be handled by the caller
18a) Yes, by adding throws to the initializer
19c) Both a and b
20a) Correct syntax as shown

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