Debugging & Error Analysis MCQs for NSCT

By the NSCT Exam Editorial Team · Updated 2026-11-24 · Facts checked against official sources on 2026-11-24

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:

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

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

Interactive quiz loading. The first 10 questions are shown below.

  1. A program runs to completion without any error message but prints the wrong total. What kind of error is this?

    1. Runtime error
    2. Logic error
    3. Syntax error
    4. Linker error
    Show answer

    Answer: B. Logic error

    A logic error lets the program run normally but produces the wrong result, because the code does something other than what the programmer intended. Syntax errors stop the program before it runs, and runtime errors crash it with a message.

  2. In Python, the line `if x > 0` is missing its colon. When is this error reported?

    1. Before the program starts running, as a SyntaxError
    2. Never; Python silently adds the colon
    3. Only when the if statement is reached, as a RuntimeError
    4. After the program ends, as a logic error
    Show answer

    Answer: A. Before the program starts running, as a SyntaxError

    Python parses the whole file before running it. A missing colon breaks the grammar, so the parser raises a SyntaxError and no line of the file runs. Syntax errors are found at parse or compile time, not at run time.

  3. Which exception does Python raise when this line runs?

    print(10 / 0)
    1. TypeError
    2. ZeroDivisionError
    3. SyntaxError
    4. ValueError
    Show answer

    Answer: B. ZeroDivisionError

    The line is valid syntax, so it parses. When it runs, dividing by zero raises ZeroDivisionError, which is a runtime error: it only appears when that line executes with a zero divisor.

  4. What is an off-by-one error?

    1. Any error that appears only on the first run of a program
    2. Using one variable where two are needed
    3. A loop or index boundary that goes one step too far or stops one step too early
    4. A rounding error in the last decimal place of a float
    Show answer

    Answer: C. A loop or index boundary that goes one step too far or stops one step too early

    An off-by-one error is a boundary mistake: a loop runs n+1 or n-1 times instead of n, or an index goes one past the end. It usually comes from mixing up < and <=, or forgetting that indexes start at 0.

  5. When a Python program crashes, what does the traceback (stack trace) show?

    1. The values of all variables in the program
    2. The chain of function calls active at the crash, with the file and line of each
    3. A suggested fix for the bug
    4. Every line the program has executed since it started
    Show answer

    Answer: B. The chain of function calls active at the crash, with the file and line of each

    A traceback lists the calls that were in progress when the exception was raised, most recent call last, with file names and line numbers. Read it from the bottom: the last line names the exception and the line just above shows where it happened.

  6. What does setting a breakpoint in a debugger do?

    1. Makes the line run twice to check for consistency
    2. Pauses execution at that line so you can inspect variables and step through the code
    3. Stops the program permanently and reports an error
    4. Deletes the line from the program
    Show answer

    Answer: B. Pauses execution at that line so you can inspect variables and step through the code

    A breakpoint tells the debugger to pause before the marked line runs. While paused you can read variable values, step into or over calls, and continue. It lets you check your expectations at a precise point without adding print statements.

  7. A student explains her code line by line to a rubber duck on her desk and spots the bug halfway through. What is this technique called?

    1. Rubber duck debugging
    2. Static analysis
    3. Pair programming
    4. Unit testing
    Show answer

    Answer: A. Rubber duck debugging

    Rubber duck debugging means explaining code aloud, line by line, to someone or something that cannot help. Forcing yourself to state what each line should do often exposes the line that does something else.

  8. A user reports that a form "sometimes" saves the wrong date. What should a developer do first?

    1. Reproduce the bug reliably with a known input
    2. Rewrite the date-handling module from scratch
    3. Add try/except around the save call to hide the error
    4. Close the report because it is not consistent
    Show answer

    Answer: A. Reproduce the bug reliably with a known input

    Debugging starts with reproducing the fault. Once you can trigger it on demand with a known input, you can test hypotheses and confirm that a fix works. Hiding the error or rewriting code blindly does not locate the cause.

  9. A feature that worked in version 1.2 stops working after an unrelated change in version 1.3. What is this kind of bug called?

    1. A syntax error
    2. A boundary value
    3. A regression
    4. A compile-time warning
    Show answer

    Answer: C. A regression

    A regression is a bug in behaviour that used to work, introduced by a later change. Regression testing re-runs earlier tests after every change so that such breakages are caught early.

  10. A function accepts ages from 18 to 60 inclusive. Which set of test inputs best follows boundary value analysis?

    1. 17, 18, 60, 61
    2. 20, 30, 40, 50
    3. 18, 19, 20, 21
    4. 0, 100, 1000, -1
    Show answer

    Answer: A. 17, 18, 60, 61

    Boundary value analysis tests values on and just outside each edge, because off-by-one errors live there. 17 and 61 should be rejected, while 18 and 60 should be accepted. Mid-range values rarely expose a wrong < or <=.

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.