12.3 Program Testing and Maintenance (3)
Resources |
Revision Questions |
Computer Science
Login to see all questions
Click on a question to view the answer
1.
A programmer is debugging a Python program that calculates the factorial of a non-negative integer. The program is intended to use recursion. However, the program consistently produces incorrect results for inputs greater than 5. Identify at least three different types of errors that could be present in the code and explain how each type of error manifests in this scenario. For each error type, suggest a method for detecting it during the development process.
Here are three potential error types and methods for detection:
- Syntax Error: This occurs when the code violates the grammatical rules of the programming language. In this case, a syntax error might be present in the recursive function definition, such as incorrect indentation, missing colons, or improper use of keywords.
Detection: The Python interpreter will flag a syntax error during the compilation/parsing phase. Integrated Development Environments (IDEs) like VS Code or PyCharm will highlight syntax errors as the code is being written.
- Logic Error: This is an error in the algorithm or the way the code is structured, leading to incorrect results even if the syntax is correct. A common logic error in a factorial function is an incorrect base case (e.g., not handling the case where n=0 correctly) or an incorrect recursive step. For example, the recursive call might be using the wrong argument or not correctly multiplying the result.
Detection: Thorough testing with a variety of inputs, including edge cases (e.g., 0, 1, 5, 6, 10), is crucial. Using a debugger to step through the code and examine the values of variables at each stage can also reveal logic errors. Code reviews by other programmers are also highly effective.
- Runtime Error: This occurs during the execution of the program, often due to unexpected conditions. A runtime error could arise if the recursion depth is exceeded (leading to a stack overflow) or if the input is invalid (e.g., a negative number is passed to the function, which is not handled).
Detection: Using a try-except block to catch potential exceptions (e.g., RecursionError
, ValueError
) can help handle runtime errors gracefully. Input validation (checking that the input is a non-negative integer) before calling the factorial function can prevent invalid inputs from causing runtime errors. Also, monitoring the program's memory usage can help detect stack overflows.
2.
Consider a program that reads data from a file. Explain how the use of input validation can help avoid faults. Provide a specific example of an invalid input and describe how input validation would prevent a fault.
Input validation is crucial for avoiding faults when reading data from a file. It involves checking the data received from the file to ensure it conforms to the expected format and constraints. Without input validation, the program could crash, produce incorrect results, or even become vulnerable to security exploits.
Example of Invalid Input: Suppose the program is designed to read an integer from the file, but the file contains a string (e.g., "abc").
How Input Validation Prevents a Fault: Input validation would involve checking the type of the data read from the file. If the data is not an integer, the program would detect this invalid input and take appropriate action. This could include:
- Rejecting the input: Displaying an error message to the user and prompting them to re-enter the data.
- Using a default value: Assigning a default value to the variable if the input is invalid.
- Logging the error: Recording the error in a log file for later analysis.
By validating the input, the program avoids a potential crash (e.g., a NumberFormatException
) and ensures that it only processes valid data. This makes the program more robust and reliable.
3.
Explain the difference between a test strategy and a test plan. Provide an example to illustrate how they are related.
A test strategy is a high-level document that outlines the overall approach to testing a software product. It defines the 'what' and 'why' of testing. It sets the direction and principles for the entire testing effort. It's a broad, conceptual document.
A test plan is a more detailed document that specifies the 'how' of testing. It translates the test strategy into actionable steps. It outlines the specific activities, resources, schedule, and deliverables required to execute the test strategy. It's a detailed, practical document.
Relationship Example:
Test Strategy: The test strategy might state that the testing approach will be risk-based, focusing on areas of the software with the highest potential for defects (e.g., complex algorithms, critical functionality). It might also specify that a combination of black-box and white-box testing techniques will be used.
Test Plan: The test plan would then detail *how* the risk-based testing will be implemented. It would specify:
- Which modules will be prioritized for testing based on risk assessment.
- The specific test cases that will be developed for those modules.
- The test environment that will be used.
- The tools that will be used to automate testing.
- The schedule for testing activities.
- The roles and responsibilities of the testing team.
In this example, the test strategy provides the guiding principles, while the test plan provides the detailed roadmap for implementing those principles. The test plan is derived from and supports the test strategy.