Debugging & Error Analysis MCQs for NSCT
Debugging and error analysis is the skill of working out why a program gives the wrong result and which line causes it. It is one of the 16 topics in the Problem Solving & Analytical Skills area, which carries 20% of the NSCT according to HEC's Areas of Competencies. In 2026 this area averaged 49.1% accuracy, and analysis-tier questions, which include debugging, averaged 44.8%, per the HEC NSCT report.
Syntax, runtime and logic errors
Every bug falls into one of three types, and questions often ask you to name the type first.
| Type | When it shows up | Python example | How you find it |
|---|---|---|---|
| Syntax error | Before the program runs, when the code is parsed | if x > 0 with no colon |
The interpreter or compiler reports it |
| Runtime error | While running, at the faulty line | 10 / 0 raises ZeroDivisionError |
The program crashes with a traceback |
| Logic error | Never reported; the output is just wrong | range(1, n) when you meant 1 to n |
Testing and tracing |
Exam angle: if the stem says the program "runs but prints the wrong answer", it is a logic error, whatever else the options suggest.
Logic errors
A logic error is valid code that does something other than what you intended. No tool can flag it, because the tool does not know your intent. Typical causes are a wrong operator (// instead of /), a wrong starting value or a wrong condition.
def find_max(a):
m = 0 # bug: should be m = a[0]
for v in a:
if v > m:
m = v
return m
This works for most lists but returns 0 for [-4, -2, -7]. Exam angle: "Which input exposes the bug?" questions reward you for testing edge cases (negatives, empty lists, zeros), not typical values.
Off-by-one errors
An off-by-one error is a boundary mistake: a loop runs one time too many or too few, or an index goes one past the end. It usually comes from < versus <=, or from forgetting that indexes run from 0 to len(a) - 1. The fencepost case is a classic: a 20 m fence with a post every 2 m needs 11 posts, not 10.
| Loop | Values produced | Count |
|---|---|---|
range(5) |
0 to 4 | 5 |
range(1, 5) |
1 to 4 | 4 |
range(1, 6) |
1 to 5 | 5 |
range(0, 10, 3) |
0, 3, 6, 9 | 4 |
Exam angle: boundary value analysis, which tests values on and just outside each edge, is the standard way to catch these.
Tracing faults
Tracing means following the code to the point where actual values first differ from expected values. Three tools do most of the work:
- Trace table: one column per variable, one row per step. It is the fastest pen-and-paper method.
- Traceback (stack trace): read it from the bottom. The last line names the exception, and the line above shows where it happened.
- Bisection: check the midpoint of the code or commit history and halve the search each time. With 1,024 stages, 10 checks are enough.
git bisectuses the same idea.
Exam angle: analysis-tier items often give a short function, an input and the wrong output, then ask which single change fixes it. Simulate each option rather than guessing.
Common traps
- Assuming no error message means no bug. Logic errors are silent.
- Reading
x == 1 or 2as "x is 1 or 2". Python treats it as(x == 1) or 2, which is always true. - Forgetting that
range(a, b)excludesb. - Removing items from a list while looping over it, which skips elements.
- Comparing floats with
==.0.1 + 0.2 == 0.3is False in binary floating point. - Using a default argument such as
bucket=[], which is shared between calls.
Worked examples
1. Understanding. A Python program misspells a variable name inside a function that only runs when the user picks option 3. What kind of error is it, and when is it reported?
Answer: a runtime error (NameError), reported only when that line runs. Python checks names when the line executes, not when the file is parsed, so the program works until option 3 is chosen.
2. Application. Marks of 50 or above pass. What does this print, and is it correct?
def count_passes(marks):
n = 0
for m in marks:
if m > 50:
n += 1
return n
print(count_passes([50, 72, 49, 90]))
Answer: it prints 2, but the correct count is 3. The mark of exactly 50 fails m > 50. Changing the test to m >= 50 fixes this off-by-one at the boundary.
3. Analysis. This function should return the index of the first negative number. Which input makes it crash, and what is the fix?
def first_negative(a):
i = 0
while a[i] >= 0:
i += 1
return i
Answer: any list with no negative number, such as [1, 2, 3]. i runs past the last index and a[3] raises IndexError. The fix is to guard the index first, while i < len(a) and a[i] >= 0, and return -1 when i == len(a). Short-circuit evaluation stops a[i] from being read once i is out of range.
Practise Debugging & Error Analysis MCQs
The quiz below has 98 questions that you can filter by tier and difficulty.
Practise: 98 questions
FAQs
How is a logic error different from a runtime error?
A runtime error stops the program with an exception, such as ZeroDivisionError or IndexError, so you know something failed and where. A logic error lets the program finish normally but produce the wrong output. You find logic errors by comparing actual and expected results on test inputs, then tracing the code to the first point where they differ.
What is the quickest way to spot an off-by-one error?
Check the boundaries first. Write down the first and last values the loop variable takes, and count them. Then test the edge inputs: an empty list, a single item, and values exactly on a limit such as 50 when the rule is "50 or above". Most off-by-one bugs show up in one of these cases.
How do I trace code quickly under time pressure?
Use a trace table with a column for each variable that changes and a row for each loop pass. Fill it in line by line instead of jumping ahead. For long loops, trace two or three passes, spot the pattern, then work out the final value. Mark output in a separate column so you do not mix it up with the variables.
Next step
Move on to Complexity & Efficiency Awareness, the next topic in HEC's order. You can also revisit Algorithms & Flow Control for loop basics, browse all Problem Solving MCQs, or test yourself under time in an NSCT mock test.