CPA Testing, Debugging & Code Optimization 3 — Questions and Answers
Question 1: What is 'mocking' in unit testing?
- Running tests in a mock production environment
- Replacing a dependency with a fake that records how it was called (Correct answer)
- Deliberately introducing bugs to test error handling
- Skipping tests that are not yet implemented
Correct answer: Replacing a dependency with a fake that records how it was called
A mock replaces a real dependency and also verifies that specific methods were called with expected arguments during the test.
Question 2: Which type of error is detected by the compiler before the program runs?
- Runtime error
- Logic error
- Syntax error (Correct answer)
- Semantic error at execution
Correct answer: Syntax error
Syntax errors violate the grammatical rules of the programming language and are caught by the compiler or interpreter before execution begins.
Question 3: What does 'test-driven development' (TDD) require you to do first?
- Write the production code
- Write a failing test before writing code to pass it (Correct answer)
- Perform code review
- Deploy to a staging environment
Correct answer: Write a failing test before writing code to pass it
TDD follows a red-green-refactor cycle: write a failing test first, then write the minimum code to make it pass, then refactor.
Question 4: Which data structure typically provides O(1) average-case lookup and is commonly used for caching optimizations?
- Linked list
- Binary search tree
- Hash table (Correct answer)
- Stack
Correct answer: Hash table
Hash tables map keys to values using a hash function, providing O(1) average-case lookup and making them ideal for caching and memoization.
Question 5: What is 'dead code' in software?
- Code that crashes at runtime
- Code that is never executed during normal program operation (Correct answer)
- Code written in a deprecated language
- Code that has no comments
Correct answer: Code that is never executed during normal program operation
Dead code is source code that can never be reached or executed, often left over from refactoring, and should be removed to reduce maintenance burden.
Question 6: What is the purpose of an assertion in a unit test?
- To print debug output to the console
- To verify that an actual value matches the expected value (Correct answer)
- To mark a test as pending implementation
- To measure how long a function takes
Correct answer: To verify that an actual value matches the expected value
Assertions compare actual results to expected values and cause the test to fail with a clear message if they do not match.
Question 7: Which profiling approach samples the call stack at regular intervals to estimate where time is spent?
- Instrumentation profiling
- Statistical (sampling) profiling (Correct answer)
- Memory profiling
- Code coverage profiling
Correct answer: Statistical (sampling) profiling
Statistical profiling periodically samples the program's call stack, estimating hot spots without requiring code instrumentation, resulting in lower overhead.
What is 'mocking' in unit testing?