Practice 50 Handling errors with Result and option Coding Questions, TechnoVlogs

Practice 50 Handling errors with Result and option Coding Questions


Q1. Write a Rust program that demonstrates handling an error using Result when attempting to divide two numbers.  
Input:  
Numerator: 10, Denominator: 0  
Expected Output:  
Error: Division by zero

Q2. Write a Rust program that handles an Option with a None value using unwrap_or().  
Input:  
Option: None, Default value: 100  
Expected Output:  
Default value: 100

Q3. Write a Rust program that demonstrates using Result::map() to apply a function to a value inside an Ok variant.  
Input:  
Result: Ok(10), Function: multiply by 5  
Expected Output:  
Mapped value: 50

Q4. Write a Rust program to demonstrate how to use Option::map() to double the value inside an Option.  
Input:  
Option: Some(4)  
Expected Output:  
Doubled value: 8

Q5. Write a Rust program to handle an error using unwrap_or_else() when reading from a file that does not exist.  
Input:  
File path: "nonexistent_file.txt"  
Expected Output:  
Error: Could not read the file

Q6. Write a Rust program that handles Option using and_then() to apply a function only if a value is present.  
Input:  
Option: Some(3), Function: multiply by 2  
Expected Output:  
Transformed value: 6

Q7. Write a Rust program to use Result::and_then() to chain operations in case of an Ok variant.  
Input:  
Result: Ok(5), Function: add 10  
Expected Output:  
Final value: Ok(15)

Q8. Write a Rust program to use unwrap() on an Option that contains Some.  
Input:  
Option: Some(20)  
Expected Output:  
Unwrapped value: 20

Q9. Write a Rust program to use Result::map_or_else() to map or handle the error based on the result.  
Input:  
Result: Err("Connection failed"), Error: "Retrying"  
Expected Output:  
Error: Retrying

Q10. Write a Rust program to handle a None variant with unwrap_or_default().  
Input:  
Option: None  
Expected Output:  
Default value: 0

Q11. Write a Rust program to return a Result type for a function that divides two integers and handles errors.  
Input:  
Numerator: 10, Denominator: 0  
Expected Output:  
Error: Cannot divide by zero

Q12. Write a Rust program that demonstrates how to handle an error using unwrap_or() when accessing an Option with None.  
Input:  
Option: None, Default: 50  
Expected Output:  
Default value: 50

Q13. Write a Rust program to handle errors using Result and print the error message when the operation fails.  
Input:  
Result: Err("Invalid input")  
Expected Output:  
Error: Invalid input

Q14. Write a Rust program to use Option::filter() to check if a value inside an Option meets a certain condition.  
Input:  
Option: Some(10), Condition: value is greater than 5  
Expected Output:  
Option passed the filter: Some(10)

Q15. Write a Rust program to demonstrate the use of Result::unwrap_or_else() to handle errors when reading a file.  
Input:  
File path: "invalid_path.txt"  
Expected Output:  
Error: Could not open the file

Q16. Write a Rust program to use Option::ok_or() to convert an Option into a Result.  
Input:  
Option: None, Error: "No value found"  
Expected Output:  
Error: No value found

Q17. Write a Rust program to demonstrate Result::map() to transform the value inside an Ok variant.  
Input:  
Result: Ok(6), Operation: square the value  
Expected Output:  
Squared value: 36

Q18. Write a Rust program to handle an Option with Some and perform a calculation.  
Input:  
Option: Some(7), Function: add 3  
Expected Output:  
Result: Some(10)

Q19. Write a Rust program that uses Result::unwrap_or() to provide a fallback value for a failed operation.  
Input:  
Result: Err("File not found"), Fallback: "No data"  
Expected Output:  
Fallback value: No data

Q20. Write a Rust program to demonstrate using Option::and_then() to chain transformations.  
Input:  
Option: Some(2), Function: multiply by 4  
Expected Output:  
Transformed value: Some(8)

Q21. Write a Rust program to handle multiple errors with Result using match statements.  
Input:  
Result: Err("Invalid format")  
Expected Output:  
Error: Invalid format

Q22. Write a Rust program to demonstrate the use of unwrap_or_else() on an Option.  
Input:  
Option: None, Default: "No value"  
Expected Output:  
No value

Q23. Write a Rust program to handle division by zero using Result with proper error handling.  
Input:  
Numerator: 10, Denominator: 0  
Expected Output:  
Error: Cannot divide by zero

Q24. Write a Rust program to demonstrate Option::unwrap_or() with a fallback value for None.  
Input:  
Option: None, Default value: 42  
Expected Output:  
Default value: 42

Q25. Write a Rust program to demonstrate Option::is_some() to check if an Option contains a value.  
Input:  
Option: Some(25)  
Expected Output:  
Option contains a value: true

Q26. Write a Rust program to use Result::or_else() to handle the error case in a Result type.  
Input:  
Result: Err("File error"), Function: retry  
Expected Output:  
Error: Retry

Q27. Write a Rust program to handle a successful Result type and print its value.  
Input:  
Result: Ok(100)  
Expected Output:  
Success: 100

Q28. Write a Rust program that uses Option::or_else() to return a default value when Option is None.  
Input:  
Option: None, Default: 50  
Expected Output:  
Returned value: 50

Q29. Write a Rust program that uses Result::unwrap_or() to provide a default value for an error.  
Input:  
Result: Err("Invalid operation"), Default: 0  
Expected Output:  
Default value: 0

Q30. Write a Rust program to return a Result type and handle a successful outcome.  
Input:  
Result: Ok(200)  
Expected Output:  
Success: 200

Q31. Write a Rust program to check if an Option is None and return a custom message.  
Input:  
Option: None  
Expected Output:  
Option is None: true

Q32. Write a Rust program to use Option::and_then() to transform the value inside an Option.  
Input:  
Option: Some(10), Function: subtract 2  
Expected Output:  
Transformed value: Some(8)

Q33. Write a Rust program to handle an error using unwrap_or() in case of a missing value in an Option.  
Input:  
Option: None, Default: 100  
Expected Output:  
Fallback value: 100

Q34. Write a Rust program to demonstrate Option::map_or() to apply a function or return a default value.  
Input:  
Option: Some(8), Function: square the value  
Expected Output:  
Mapped value: 64

Q35. Write a Rust program to handle an Option with None and apply map_or_else() for default behavior.  
Input:  
Option: None, Default function: return 100  
Expected Output:  
Returned value: 100

Q36. Write a Rust program to demonstrate Result::map() to transform a successful result.  
Input:  
Result: Ok(15), Operation: multiply by 2  
Expected Output:  
Transformed result: Ok(30)

Q37. Write a Rust program to handle multiple error cases with Result and print appropriate messages.  
Input:  
Result: Err("Failed to connect")  
Expected Output:  
Error: Failed to connect

Q38. Write a Rust program to handle an invalid file path using Result and return an error message.  
Input:  
File path: "invalid_path.txt"  
Expected Output:  
Error: Invalid file path

Q39. Write a Rust program to return an Option type, checking if a number is greater than 5.  
Input:  
Number: 6  
Expected Output:  
Option: Some(6)

Q40. Write a Rust program to handle an error using Result::unwrap_or_else() when an operation fails.  
Input:  
Result: Err("Not enough memory"), Default: "Try again later"  
Expected Output:  
Error handled: Try again later

Q41. Write a Rust program to use Option::map() to add 5 to a number inside an Option.  
Input:  
Option: Some(5)  
Expected Output:  
Mapped value: Some(10)

Q42. Write a Rust program to check if a value inside an Option is Some or None using pattern matching.  
Input:  
Option: Some(20)  
Expected Output:  
Value: Some(20)

Q43. Write a Rust program to handle a Result and print the value if it is Ok, otherwise print an error message.  
Input:  
Result: Ok(42)  
Expected Output:  
Success: 42

Q44. Write a Rust program to demonstrate error handling using Err and provide a fallback value.  
Input:  
Result: Err("Timeout error"), Fallback: "Retrying"  
Expected Output:  
Error: Retrying

Q45. Write a Rust program to use unwrap_or_else() on a Result to handle errors in a specific way.  
Input:  
Result: Err("Failed operation"), Default message: "Operation failed"  
Expected Output:  
Operation failed

Q46. Write a Rust program that demonstrates handling an error by returning a custom error message.  
Input:  
Result: Err("Bad input")  
Expected Output:  
Bad input

Q47. Write a Rust program that uses Option::unwrap() on a value that is Some to safely unwrap it.  
Input:  
Option: Some(30)  
Expected Output:  
Unwrapped value: 30

Q48. Write a Rust program to demonstrate using Option::or_else() to return an alternative value.  
Input:  
Option: None, Alternative: Some(100)  
Expected Output:  
Returned value: Some(100)

Q49. Write a Rust program to demonstrate error handling using Option::or() to return an alternative value.  
Input:  
Option: None, Alternative: Some(50)  
Expected Output:  
Alternative value: Some(50)

Q50. Write a Rust program to handle the division of two numbers using Result and print an appropriate message for error handling.  
Input:  
Numerator: 15, Denominator: 3  
Expected Output:  
Success: 5

Share on Social Media