Identifying and Correcting Errors

Help Questions

AP Computer Science Principles › Identifying and Correcting Errors

Questions 1 - 10
1

A programmer is debugging a program that calculates the average of three test scores. The program should output "Pass" if the average is 70 or higher, and "Fail" otherwise. However, the program is not working correctly. Which of the following is the MOST likely type of error causing this malfunction?

A logic error in the conditional statement that determines pass or fail status

A run-time error caused by division by zero when calculating the average score

An overflow error resulting from test scores that exceed the maximum integer value

A syntax error in the variable declaration statements at the beginning of the program

Explanation

A logic error is the most likely cause when a program runs without crashing but produces incorrect results. The program calculates and displays results, indicating no syntax or run-time errors, but the incorrect pass/fail determination suggests flawed conditional logic.

2

A programmer receives an error message stating "Expected ';' at line 15" when trying to run their program. What type of error is this, and what is the most appropriate first step to fix it?

This is an overflow error; the programmer should use a larger data type for variables declared near line 15

This is a run-time error; the programmer should add exception handling code around line 15 to prevent crashes

This is a logic error; the programmer should test the program with different input values to identify the problem

This is a syntax error; the programmer should add a semicolon at the end of line 15 or the previous line

Explanation

The error message indicates a missing semicolon, which is a syntax error - a violation of the programming language's rules. The compiler cannot parse the code without proper syntax, so adding the missing semicolon is the correct first step.

3

A programmer is testing a function that should return the factorial of a positive integer. The function works correctly for small inputs but crashes when calculating the factorial of 25. What type of error is this and what is the best solution?

This is a run-time error; the programmer should add input validation to reject all values greater than 20

This is a logic error; the programmer should revise the base case condition in the recursive algorithm

This is a syntax error; the programmer should check for missing parentheses in the recursive function calls

This is an overflow error; the programmer should use a data type that can handle larger numerical values

Explanation

When a factorial function works for small inputs but crashes for larger ones like 25, this indicates an overflow error. The factorial of 25 is extremely large and likely exceeds the maximum value that can be stored in the current data type.

4

A web application displays the error "Uncaught TypeError: Cannot read property 'length' of undefined" during execution. What type of error is this and what is the most likely cause?

This is a logic error caused by using the wrong method to calculate the length of an array

This is a syntax error caused by incorrect variable naming conventions in the source code

This is an overflow error caused by arrays that contain more elements than the system can handle

This is a run-time error caused by attempting to access a property of a variable that is undefined

Explanation

This error message indicates a run-time error where the program tries to access the 'length' property of an undefined variable. This happens during execution when the code attempts to use a variable that hasn't been properly initialized or assigned a value.

5

A program that processes user input crashes with an "index out of bounds" error when users enter certain types of data. What type of error is this and what is the most effective prevention strategy?

This is a syntax error; the programmer should review array declaration statements for proper formatting

This is a logic error; the programmer should redesign the algorithm to use different data structures

This is a run-time error; the programmer should add input validation and bounds checking before array access

This is an overflow error; the programmer should increase the maximum size limit for all array declarations

Explanation

An "index out of bounds" error that occurs during execution is a run-time error. The most effective prevention is adding input validation and bounds checking to ensure array indices are within valid ranges before attempting to access array elements.

6

A programmer tests a sorting function with the input list 5, 2, 8, 1, 9 and expects the output 1, 2, 5, 8, 9, but gets 1, 2, 5, 8, 8. What debugging approach would be most effective for this scenario?

Test the function with larger datasets to determine if the error is related to input size limitations

Use visualization tools to observe how array elements move during each step of the sorting process

Recompile the program with different compiler flags to ensure proper optimization of the sorting algorithm

Add error handling code to prevent the function from modifying the original input array during processing

Explanation

When a sorting function produces incorrect output (duplicating 8 instead of preserving 9), visualization tools are most effective for debugging. They allow the programmer to see exactly how elements are moved or swapped during each sorting step, revealing where the algorithm fails.

7

A program contains a recursive function that should calculate the nth Fibonacci number. The program works for small values of n but crashes with a "stack overflow" error for larger values. What type of error is this and what is the best solution?

This is an overflow error; the programmer should use floating-point data types instead of integers for calculations

This is a logic error; the programmer should verify the mathematical accuracy of the Fibonacci sequence formula

This is a syntax error; the programmer should check for proper function definition and parameter declarations

This is a run-time error; the programmer should implement an iterative solution or add memoization to reduce recursive calls

Explanation

A "stack overflow" error during execution is a run-time error caused by too many recursive function calls. The best solution is to implement an iterative approach or add memoization to reduce the number of recursive calls and prevent stack overflow.

8

A program designed to find the average of numbers in a list produces the correct result when tested with positive integers but gives wrong answers when tested with negative numbers. What is the most systematic approach to debug this issue?

Add input validation to reject all negative numbers and only process positive integer values

Create comprehensive test cases that include positive numbers, negative numbers, zero, and mixed combinations

Optimize the algorithm for better performance by using more efficient mathematical operations and data structures

Rewrite the entire program using a different programming language that handles negative numbers more reliably

Explanation

When a program works correctly for some inputs but fails for others (positive vs. negative numbers), creating comprehensive test cases is the most systematic debugging approach. This helps identify exactly what conditions cause the logic error and where the algorithm fails.

9

A programmer creates test cases with input values at the minimum, maximum, and boundary conditions to verify their program works correctly. This testing strategy is most effective for detecting which type of error?

Logic errors in boundary conditions and edge cases that may not be apparent with typical input

Overflow errors that occur when the program processes data beyond its designed capacity limits

Syntax errors related to improper variable declarations and function definition formatting

Run-time errors caused by invalid memory access patterns and improper resource management

Explanation

Testing with boundary conditions and edge cases (minimum, maximum values) is specifically designed to detect logic errors that may not be apparent with typical input values. These test cases reveal flaws in how the algorithm handles extreme or boundary situations.

10

A programmer uses a debugger to pause program execution at specific lines and examine the values of variables in memory. This debugging tool is most valuable for identifying which type of programming error?

Syntax errors that occur during the compilation phase before the program can execute

Compilation errors that prevent the debugger from loading and analyzing the program code

Overflow errors that result from exceeding the storage capacity of primitive data types

Logic errors where the program flow or variable values differ from expected behavior during execution

Explanation

Debuggers that allow pausing execution and examining variable values are most valuable for logic errors. They enable programmers to step through code line by line and verify that program flow and variable values match expected behavior, revealing where logic deviates from intentions.

Page 1 of 2