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.
Option type in Rust? a) A way to represent success or failureSome value or NoneResult type in Rust? a) Success, FailureOk, ErrSome, NonePassed, FailedOption type is primarily used for: a) Returning error messagesResult is used when an operation succeeds in Rust? a) SomeOkNoneErrNone variant of the Option type indicate in Rust? a) A value is availableOption safely in Rust? a) Use unwrap()matchget()expect()Err variant in the Result type in Rust? a) Represents a successful operationResult instead of Option in Rust? a) When dealing with optional valuesOption::map() do in Rust? a) Maps a value inside Some to another valueSome and None to a default valueSome and None values into a tupleSomeResult and Option types in Rust to allow unwrapping? a) ResultableOptionableUnwrapIntoOptionResult and Option types? a) if statementsmatch expressionsfor loopstry blocksOption in Rust to handle both Some and None cases? a) Use if letwhile letmatchunwrapErr variant in a match expression in Rust? a) match result { Ok(val) => val, Err(e) => e }match result { Err(e) => e, Ok(val) => val }match result { Some(val) => val, None => error }match result { Ok(val) => val, None => error }Option using match to extract the value of Some in Rust? a) match opt { None => return, Some(val) => val }match opt { Some(val) => val, None => panic!("None found") }match opt { Some(val) => val, Err(e) => handle_error(e) }match opt { Some(val) => return val, None => default }match expression do when used with a Result type that is Ok? a) It returns the Err variantOk variantSomeOption simplify error handling? a) By allowing you to ignore errorsmatch on an Option but don’t cover both Some and None? a) A compile-time error occursNone case is assumedOption that might be None? a) unwrap()unwrap_or()matchexpect()match expression: match None { Some(x) => x, None => 0 } in Rust? a) It compiles and returns 0NoneResult for error handling? a) Always return OkOk and Err variants? Operator? operator in Rust? a) To match on resultsOption types? operator in Rust? a) It retries the operation on errorErr from a Result up the call stackOption? operator with Result in Rust? a) let x = some_function()?;let x = some_function()?; let y = other_function()?;some_function()? = x;x = some_function()?? operator encounters an Err value? a) It retries the operationOption::NoneErr value? operator in Rust? a) Only with Option typesResult? operator in Rust? a) Functions that return OptionResultAnyString? operator to be used within it in Rust? a) Result<T, E>Option<T>StringVec<T>? operator in a function that doesn’t return a Result or Option? a) It will cause a compile-time errorNone? operator with a panic! call in Rust? a) Yes, it propagates the panicpanic! cannot be used with ?Err? will suppress the panic? operator improve error handling in Rust code? a) By converting all errors to panics| Qno | Answer |
|---|---|
| 1 | b) A container for either Some value or None |
| 2 | b) Ok, Err |
| 3 | b) Representing possible absence of a value |
| 4 | b) Ok |
| 5 | c) No value is present |
| 6 | b) Use match |
| 7 | c) Represents an error or failure |
| 8 | b) When expecting a return value or success/failure |
| 9 | a) Maps a value inside Some to another value |
| 10 | d) IntoOption |
| 11 | b) match expressions |
| 12 | c) Use match |
| 13 | b) match result { Err(e) => e, Ok(val) => val } |
| 14 | b) match opt { Some(val) => val, None => panic!("None found") } |
| 15 | b) It executes code for the Ok variant |
| 16 | b) By combining error handling and value extraction in a single expression |
| 17 | a) A compile-time error occurs |
| 18 | c) match |
| 19 | a) It compiles and returns 0 |
| 20 | c) Handle both Ok and Err variants |
| 21 | b) To propagate errors upwards |
| 22 | c) It propagates Err from a Result up the call stack |
| 23 | a) let x = some_function()?; |
| 24 | c) It returns early from the function with the Err value |
| 25 | b) When dealing with functions that return Result |
| 26 | b) Functions that return Result |
| 27 | a) Result<T, E> |
| 28 | a) It will cause a compile-time error |
| 29 | b) No, panic! cannot be used with ? |
| 30 | c) By making error propagation more concise and readable |