MCQs on Testing and Debugging | Rust

Testing and debugging are essential practices for any Rust developer. This collection of 30 multiple-choice questions (MCQs) covers writing unit tests, test organization, debugging with common Rust tools, and using cargo test and cargo bench for better code quality.


Writing Unit Tests with #[test]

  1. What is the primary purpose of the #[test] attribute in Rust?
    • A) To specify that the function is a unit test
    • B) To define a struct for test data
    • C) To compile the code for production
    • D) To benchmark a test case
  2. Which of the following is required for a unit test function in Rust?
    • A) It must return a Result
    • B) It must use the #[test] attribute
    • C) It must be public
    • D) It must be asynchronous
  3. In Rust, how do you assert that two values are equal in a unit test?
    • A) assert_eq!(a, b)
    • B) check_eq!(a, b)
    • C) equal!(a, b)
    • D) compare!(a, b)
  4. Which of these best practices is recommended when writing unit tests in Rust?
    • A) Always write unit tests for public functions only
    • B) Keep tests small and focused on one behavior
    • C) Avoid using assertions in tests
    • D) Avoid writing tests for functions that interact with databases
  5. What is the purpose of the #[ignore] attribute in a test?
    • A) To skip the test during normal test execution
    • B) To make the test always pass
    • C) To log information about the test
    • D) To mark the test as deprecated

Test Organization and Attributes

  1. Which attribute is used to mark a test as needing a specific condition before running?
    • A) #[test_if]
    • B) #[should_panic]
    • C) #[cfg(test)]
    • D) #[requires]
  2. What is the purpose of #[should_panic] in Rust unit tests?
    • A) To assert that the function returns Err
    • B) To indicate that a test should panic for success
    • C) To suppress test output
    • D) To mark the function as deprecated
  3. How can you run only a specific test function using cargo test?
    • A) cargo test --run test_name
    • B) cargo test test_name
    • C) cargo test --only test_name
    • D) cargo test --name test_name
  4. What is the role of #[cfg(test)] in Rust?
    • A) To specify code that should only be compiled during testing
    • B) To disable the test functions
    • C) To specify which environment a test should run on
    • D) To mark the test as ignored
  5. What is a common practice for grouping unit tests in Rust?
  • A) Using submodules and organizing tests into files
  • B) Always placing tests in the src directory
  • C) Writing all tests in the main module
  • D) Grouping tests inside the main.rs file

Debugging with println! and dbg!

  1. Which of the following statements is true about println! in Rust?
  • A) It is used for debugging output during execution
  • B) It is used to print formatted error messages
  • C) It outputs data in JSON format
  • D) It stops the execution of the program
  1. What does the dbg! macro in Rust do?
  • A) Prints the value of an expression and returns the value for debugging
  • B) Outputs a stack trace when a panic occurs
  • C) Logs the test results to a file
  • D) Terminates the program with a failure message
  1. What is the key difference between println! and dbg! in Rust?
  • A) dbg! prints the value of the expression and the source code location
  • B) println! is used for error messages only
  • C) dbg! is slower than println!
  • D) println! returns the printed value
  1. In Rust, what happens if you use dbg! in release builds?
  • A) It is ignored unless explicitly enabled
  • B) It causes a runtime error
  • C) It prints values but disables optimization
  • D) It reduces performance by adding debugging symbols
  1. What is the main advantage of using dbg! during debugging?
  • A) It gives a quick overview of the expression being evaluated with less code
  • B) It logs to a file for post-mortem analysis
  • C) It only outputs error messages
  • D) It is better than println! for long-running applications

Using cargo test and cargo bench

  1. What is the primary purpose of cargo test in Rust?
  • A) To run all tests in the project
  • B) To compile the project in debug mode
  • C) To benchmark performance of the code
  • D) To format the project code
  1. How can you see more detailed output from cargo test?
  • A) Use the --verbose flag
  • B) Add #[debug] in test attributes
  • C) Use cargo test --details
  • D) Use println! statements in tests
  1. What does the cargo test -- --nocapture command do?
  • A) It prevents test output from being captured, allowing it to be printed to the console
  • B) It skips tests that produce output
  • C) It disables output logging entirely
  • D) It runs tests without showing any console output
  1. How do you benchmark code performance with cargo bench in Rust?
  • A) By running cargo test --bench
  • B) By using cargo run --bench
  • C) By defining benchmark functions and using #[bench]
  • D) By using the criterion crate
  1. What is the benefit of using cargo bench in Rust development?
  • A) It helps measure and improve the performance of code
  • B) It runs unit tests more efficiently
  • C) It tracks dependencies in the project
  • D) It performs static analysis of code quality

General Testing and Debugging

  1. What is the default behavior of cargo test when a test fails?
  • A) It stops the entire testing process
  • B) It prints a summary of all tests and exits with a non-zero status
  • C) It retries the test automatically
  • D) It ignores the failed test
  1. In Rust, which command would you use to run a single test function?
  • A) cargo test --name test_name
  • B) cargo test --filter test_name
  • C) cargo test test_name
  • D) cargo run --test test_name
  1. How can you run tests for a specific module or file in Rust?
  • A) cargo test mod_name
  • B) cargo test --module mod_name
  • C) cargo test --only mod_name
  • D) cargo test -p mod_name
  1. What does the #[test] attribute in Rust mark in a test file?
  • A) A function that should be executed as a unit test
  • B) A struct that contains test data
  • C) A module for testing-related utilities
  • D) A specific environment setup for testing
  1. How can you define a custom test runner in Rust?
  • A) By using the #[test] attribute with a custom function
  • B) By implementing a custom trait for the test structure
  • C) By creating a new main.rs for the test runner
  • D) Rust does not support custom test runners
  1. What is the purpose of cargo test --release?
  • A) To run tests using the release build optimizations
  • B) To skip compiling the code before testing
  • C) To run tests with minimal console output
  • D) To test a specific release version
  1. In Rust, which attribute is used to define a test that should pass only when a specific condition is met?
  • A) #[test_if]
  • B) #[should_panic]
  • C) #[cfg(test)]
  • D) #[only_when]
  1. What is the behavior of #[should_panic] in unit tests?
  • A) The test will pass if the function panics
  • B) The test will pass if the function does not panic
  • C) The test is ignored if the function panics
  • D) The function will panic automatically during execution
  1. What does cargo test output in case of a test failure?
  • A) A detailed message of what failed
  • B) A stack trace and log file
  • C) The error message from the failed test
  • D) Nothing, it just exits silently
  1. What happens when a test is marked as #[ignore] in Rust?
  • A) The test is skipped unless specifically requested
  • B) The test is compiled but not executed
  • C) The test will fail automatically
  • D) The test is included in the test suite but marked as deprecated

Answer Table

QnoAnswer (Option with text)
1A) To specify that the function is a unit test
2B) It must use the #[test] attribute
3A) assert_eq!(a, b)
4B) Keep tests small and focused on one behavior
5A) To skip the test during normal test execution
6C) #[cfg(test)]
7B) To indicate that a test should panic for success
8B) cargo test test_name
9A) To specify code that should only be compiled during testing
10A) Using submodules and organizing tests into files
11A) It is used for debugging output during execution
12A) Prints the value of an expression and returns the value for debugging
13A) dbg! prints the value of the expression and the source code location
14A) It is ignored unless explicitly enabled
15A) It gives a quick overview of the expression being evaluated with less code
16A) To run all tests in the project
17A) Use the --verbose flag
18A) It prevents test output from being captured, allowing it to be printed to the console
19C) By defining benchmark functions and using #[bench]
20A) It helps measure and improve the performance of code
21B) It prints a summary of all tests and exits with a non-zero status
22C) cargo test test_name
23A) cargo test mod_name
24A) A function that should be executed as a unit test
25D) Rust does not support custom test runners
26A) To run tests using the release build optimizations
27C) #[cfg(test)]
28A) The test will pass if the function panics
29A) A detailed message of what failed
30A) The test is skipped unless specifically requested

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