Identifying and Correcting Errors - AP Computer Science Principles

  1. A programmer creates a function to search for a specific value in a sorted list using binary search. The function sometimes returns the correct index and sometimes returns -1 even when the value exists in the list. What is the best approach to debug this inconsistent behavior?
    Answer: Add comprehensive test cases that cover edge cases like empty lists, single-element lists, and boundary values Inconsistent behavior in a search function suggests logic errors in the algorithm. Adding comprehensive test cases, especially edge cases, helps identify exactly when and why the function fails, allowing the programmer to pinpoint the specific logical flaws.
  2. 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?
    Answer: A logic error in the conditional statement that determines pass or fail status 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.
  3. 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?
    Answer: Logic errors in boundary conditions and edge cases that may not be apparent with typical input 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.
  4. 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?
    Answer: This is a syntax error; the programmer should add a semicolon at the end of line 15 or the previous line 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.
  5. A student writes a program to find the maximum value in a list of numbers. The program compiles successfully but always returns the first element instead of the actual maximum. What is the best debugging strategy to identify the source of this logic error?
    Answer: Add print statements to display the current maximum value after each comparison in the loop Adding print statements to trace the program's execution is an effective debugging technique for logic errors. This allows the programmer to see how the maximum value variable changes (or fails to change) during each iteration, revealing where the comparison logic fails.
  6. 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?
    Answer: Logic errors where the program flow or variable values differ from expected behavior during execution 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.
  7. A programmer uses hand tracing to debug a loop that should print numbers 1 through 10. The hand trace reveals that the loop prints numbers 1 through 9 and then stops. What does this technique help identify?
    Answer: Hand tracing uncovers logic errors in the loop's termination condition or boundary handling Hand tracing involves manually following the program's execution step by step. When the loop stops at 9 instead of 10, this reveals a logic error in how the loop's termination condition or boundary values are handled, not syntax, run-time, or overflow issues.
  8. A program that converts temperatures from Celsius to Fahrenheit crashes with a "division by zero" error during execution. The formula used is F = (C × 9/5) + 32. What type of error is this and what is the most likely cause?
    Answer: This is a run-time error likely caused by a programming mistake in implementing the division operation A "division by zero" error that occurs during program execution is a run-time error. Since the formula F = (C × 9/5) + 32 doesn't inherently involve division by zero, this suggests a programming error in how the division operation was implemented.
  9. A student's program displays the error message "NameError: name 'count' is not defined" when executed. What type of error is this and what is the most likely cause?
    Answer: This is a run-time error caused by trying to use a variable that hasn't been properly declared or initialized A "NameError" that occurs during execution indicates a run-time error where the program tries to use a variable named 'count' that hasn't been declared or initialized. The program compiles successfully but fails when it encounters the undefined variable during execution.
  10. A program designed to calculate student grades crashes when processing a file containing student names with special characters like accents. What type of error is this and what is the most appropriate solution?
    Answer: This is a run-time error; the programmer should implement proper character encoding and exception handling When a program crashes during execution due to unexpected input (special characters), this is a run-time error. The solution involves implementing proper character encoding to handle various characters and adding exception handling to prevent crashes.
  11. A student's program contains the line "if x = 5" instead of "if x == 5" in most programming languages. What type of error is this and what will likely happen when the program runs?
    Answer: This is a syntax error that will prevent the program from compiling or running successfully In most programming languages, using a single equals sign (=) instead of double equals (==) in a conditional statement violates syntax rules. The compiler will detect this error and prevent the program from compiling or running.
  12. A programmer adds extra output statements throughout their code to track variable values during execution. This debugging technique is most effective for identifying which type of programming error?
    Answer: Logic errors where the program runs but produces incorrect results due to flawed algorithm implementation Adding extra output statements to track variable values is most effective for logic errors. This technique allows programmers to see how data flows through the program and identify where the logic deviates from the intended behavior, even when the program runs without crashing.
  13. 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?
    Answer: Use visualization tools to observe how array elements move during each step of the sorting process 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.
  14. 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?
    Answer: This is a run-time error caused by attempting to access a property of a variable that is undefined 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.
  15. 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?
    Answer: Create comprehensive test cases that include positive numbers, negative numbers, zero, and mixed combinations 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.
  16. A program that calculates compound interest produces slightly incorrect results due to rounding errors in floating-point arithmetic. What type of error is this and what is the most appropriate approach to address it?
    Answer: This is a logic error; the programmer should verify the mathematical formula and consider precision limitations Rounding errors in floating-point arithmetic that produce slightly incorrect results represent a logic error related to precision limitations. The programmer should verify the mathematical implementation and consider using techniques like decimal data types or adjusting precision to handle rounding appropriately.
  17. 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?
    Answer: This is a run-time error; the programmer should implement an iterative solution or add memoization to reduce recursive calls 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.
  18. 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?
    Answer: This is an overflow error; the programmer should use a data type that can handle larger numerical values 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.
  19. 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?
    Answer: This is a run-time error; the programmer should add input validation and bounds checking before array access 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.

Start this quiz