What this quiz covers
This quiz focuses on Identifying And Correcting Errors, giving you a quick way to practice the rules, question types, and explanations that matter most for AP Computer Science Principles.
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?
AP Computer Science Principles Quiz
Practice Identifying And Correcting Errors in AP Computer Science Principles with focused quiz questions that help you check what you know, review explanations, and build confidence with test-style prompts.
This quiz focuses on Identifying And Correcting Errors, giving you a quick way to practice the rules, question types, and explanations that matter most for AP Computer Science Principles.
Try each quiz question before looking at the correct answer. Use the explanations to review missed ideas, then come back to similar questions until the pattern feels familiar.
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?
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.
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?
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.
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?
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.
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?
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.
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?
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.
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?
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.
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?
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.
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?
Explanation: 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.
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?
Explanation: 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.
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?
Explanation: 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.
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?
Explanation: 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.
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?
Explanation: 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.
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?
Explanation: 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.
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?
Explanation: 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.
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?
Explanation: 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.
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?
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.
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?
Explanation: 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.
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?
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.
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?
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.