MCQs on Error Handling | Rust

Rust is known for its powerful error handling features, primarily through the Result and Option types, pattern matching, and the ? operator. These concepts ensure safer, more predictable error management. This set of 30 MCQs covers these critical aspects of error handling in Rust, including handling panics.


Topics:

Result and Option Types

  1. Which of the following best describes the Option type in Rust? a) A way to represent success or failure
    b) A container for either Some value or None
    c) A type used for handling exceptions
    d) A type that allows optional parameters
  2. What are the two variants of the Result type in Rust? a) Success, Failure
    b) Ok, Err
    c) Some, None
    d) Passed, Failed
  3. In Rust, the Option type is primarily used for: a) Returning error messages
    b) Representing possible absence of a value
    c) Handling panics
    d) Wrapping functions with side effects
  4. Which variant of Result is used when an operation succeeds in Rust? a) Some
    b) Ok
    c) None
    d) Err
  5. What does the None variant of the Option type indicate in Rust? a) A value is available
    b) An error has occurred
    c) No value is present
    d) A panic occurred
  6. How do you retrieve a value from an Option safely in Rust? a) Use unwrap()
    b) Use match
    c) Use get()
    d) Use expect()
  7. What is the purpose of the Err variant in the Result type in Rust? a) Represents a successful operation
    b) Represents the absence of a value
    c) Represents an error or failure
    d) Represents an empty value
  8. When should you use Result instead of Option in Rust? a) When dealing with optional values
    b) When expecting a return value or success/failure
    c) When handling panics
    d) When working with iterators
  9. What does Option::map() do in Rust? a) Maps a value inside Some to another value
    b) Maps both Some and None to a default value
    c) Combines Some and None values into a tuple
    d) Allows direct access to the value inside Some
  10. Which trait is implemented for both Result and Option types in Rust to allow unwrapping? a) Resultable
    b) Optionable
    c) Unwrap
    d) IntoOption

Pattern Matching with Result and Option

  1. Which Rust construct allows you to handle the variants of Result and Option types? a) if statements
    b) match expressions
    c) for loops
    d) try blocks
  2. How can you match on an Option in Rust to handle both Some and None cases? a) Use if let
    b) Use while let
    c) Use match
    d) Use unwrap
  3. Which of the following is correct syntax to handle an Err variant in a match expression in Rust? a) match result { Ok(val) => val, Err(e) => e }
    b) match result { Err(e) => e, Ok(val) => val }
    c) match result { Some(val) => val, None => error }
    d) match result { Ok(val) => val, None => error }
  4. How do you handle an Option using match to extract the value of Some in Rust? a) match opt { None => return, Some(val) => val }
    b) match opt { Some(val) => val, None => panic!("None found") }
    c) match opt { Some(val) => val, Err(e) => handle_error(e) }
    d) match opt { Some(val) => return val, None => default }
  5. What does the match expression do when used with a Result type that is Ok? a) It returns the Err variant
    b) It executes code for the Ok variant
    c) It causes a panic
    d) It wraps the value in Some
  6. In Rust, how does pattern matching with Option simplify error handling? a) By allowing you to ignore errors
    b) By combining error handling and value extraction in a single expression
    c) By causing runtime errors
    d) By handling invalid values automatically
  7. What happens if you match on an Option but don’t cover both Some and None? a) A compile-time error occurs
    b) The program panics
    c) The None case is assumed
    d) It causes an infinite loop
  8. Which of these Rust constructs allows safe handling of an Option that might be None? a) unwrap()
    b) unwrap_or()
    c) match
    d) expect()
  9. What is the result of this match expression: match None { Some(x) => x, None => 0 } in Rust? a) It compiles and returns 0
    b) It causes a panic
    c) It returns None
    d) It throws a runtime error
  10. What is a common pattern when matching on Result for error handling? a) Always return Ok
    b) Unwrap the result
    c) Handle both Ok and Err variants
    d) Throw an exception

Propagating Errors with the ? Operator

  1. What is the main purpose of the ? operator in Rust? a) To match on results
    b) To propagate errors upwards
    c) To unwrap Option types
    d) To handle panics
  2. Which of the following best describes the behavior of the ? operator in Rust? a) It retries the operation on error
    b) It automatically handles all types of errors
    c) It propagates Err from a Result up the call stack
    d) It wraps the result in an Option
  3. Which of these is a correct use of the ? operator with Result in Rust? a) let x = some_function()?;
    b) let x = some_function()?; let y = other_function()?;
    c) some_function()? = x;
    d) x = some_function()?
  4. What happens when the ? operator encounters an Err value? a) It retries the operation
    b) It wraps the value in Option::None
    c) It returns early from the function with the Err value
    d) It panics with an error message
  5. When should you use the ? operator in Rust? a) Only with Option types
    b) When dealing with functions that return Result
    c) For handling panics
    d) To catch all types of runtime errors
  6. Which of the following functions will work with the ? operator in Rust? a) Functions that return Option
    b) Functions that return Result
    c) Functions that return Any
    d) Functions that return String
  7. What type must a function return for the ? operator to be used within it in Rust? a) Result<T, E>
    b) Option<T>
    c) String
    d) Vec<T>
  8. What will happen if you try to use the ? operator in a function that doesn’t return a Result or Option? a) It will cause a compile-time error
    b) The program will panic
    c) The code will silently continue
    d) It will return a None
  9. Can you use the ? operator with a panic! call in Rust? a) Yes, it propagates the panic
    b) No, panic! cannot be used with ?
    c) Yes, it turns the panic into an Err
    d) No, ? will suppress the panic
  10. How does the ? operator improve error handling in Rust code? a) By converting all errors to panics
    b) By forcing developers to handle every error
    c) By making error propagation more concise and readable
    d) By suppressing all errors

Answer Key:

QnoAnswer
1b) A container for either Some value or None
2b) Ok, Err
3b) Representing possible absence of a value
4b) Ok
5c) No value is present
6b) Use match
7c) Represents an error or failure
8b) When expecting a return value or success/failure
9a) Maps a value inside Some to another value
10d) IntoOption
11b) match expressions
12c) Use match
13b) match result { Err(e) => e, Ok(val) => val }
14b) match opt { Some(val) => val, None => panic!("None found") }
15b) It executes code for the Ok variant
16b) By combining error handling and value extraction in a single expression
17a) A compile-time error occurs
18c) match
19a) It compiles and returns 0
20c) Handle both Ok and Err variants
21b) To propagate errors upwards
22c) It propagates Err from a Result up the call stack
23a) let x = some_function()?;
24c) It returns early from the function with the Err value
25b) When dealing with functions that return Result
26b) Functions that return Result
27a) Result<T, E>
28a) It will cause a compile-time error
29b) No, panic! cannot be used with ?
30c) By making error propagation more concise and readable

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