Debugging, Testing & Optimization MCQs for the NSCT
Debugging, testing and optimisation are the three skills that turn code that compiles into code that is correct and fast enough. Debugging means finding a bug with breakpoints and stepping. Testing means proving behaviour with unit tests and well-chosen inputs. Optimisation means profiling first, then improving the measured hotspot. It is topic 15 of the 16 in the Programming (C++/Java/Python) area, which carries 10% of the NSCT according to HEC's Areas of Competencies. Candidates averaged 47.8% accuracy in this area in 2026, per the HEC NSCT report.
Analysis-tier questions in this area usually show buggy code and ask what goes wrong, so practise reading code for intent as well as output. Runtime errors overlap with exception handling.
Breakpoints
A breakpoint marks a line where the debugger pauses the program before running it, so you can inspect variables and the call stack: the chain of function calls that led to that line. From there you move with these commands:
| Command | What it does |
|---|---|
| Step into | Enters the function called on this line |
| Step over | Runs the whole call, then pauses on the next line |
| Step out | Finishes the current function and returns to the caller |
| Continue | Runs to the next breakpoint |
A conditional breakpoint pauses only when an expression is true, such as i == 7342. A watch keeps showing the value of an expression as you step.
It also helps to know which kind of bug you are hunting: - Syntax error: the code will not compile or parse. - Runtime error: the program crashes while running, for example dividing by zero. - Logic error: the program runs but gives the wrong answer.
Exam angle: a bug that vanishes when you add a print or a breakpoint (a "heisenbug") points to timing, usually a race condition. See concurrency.
Unit tests
A unit test automatically checks one function or class in isolation. Good tests follow Arrange-Act-Assert:
def test_is_even():
n = -4 # Arrange
result = is_even(n) # Act
assert result is True # Assert
Test-driven development (TDD) repeats a red, green, refactor cycle. Write a failing test, write just enough code to pass it, then clean up. Test doubles replace real dependencies. A stub returns canned answers, while a mock also checks how it was called. That is the Meszaros/Fowler distinction, but libraries such as Python's unittest.mock use "mock" for both.
Choosing inputs is half the skill:
| Technique | Idea | Example (valid range 1–100) |
|---|---|---|
| Equivalence partitioning | One value per class of input | −5, 50, 150 |
| Boundary value analysis | Values at and just beyond each edge | 0, 1, 100, 101 |
| Branch coverage (white-box) | Every true and false outcome taken | Depends on the code |
Test levels run from unit to integration (modules together), system and acceptance (against user requirements). Regression testing re-runs existing tests after every change.
Exam angle: 100% branch coverage implies 100% statement coverage, but not the reverse. Even full coverage does not prove the code is correct.
Profiling
A profiler measures where time or memory actually goes. Python's cProfile reports ncalls, tottime (time in the function's own code) and cumtime (including the functions it calls). Optimise the function with the largest own time, not the one at the top of the call tree.
Measure first, then optimise. Knuth's warning that "premature optimization is the root of all evil" means clear code comes first. The biggest wins usually come from better algorithms:
| Slow pattern | Faster version | Complexity |
|---|---|---|
x in some_list inside a loop |
Convert to a set first | O(n·m) → O(n + m) average |
| Naive recursive Fibonacci | Memoise with @lru_cache |
O(2ⁿ) → O(n) |
s += part on Java strings in a loop |
StringBuilder |
O(n²) → O(n) |
| Same value computed every iteration | Hoist it out of the loop | n calls → 1 |
Exam angle: Amdahl's law applies to any partial optimisation. If X is 90% of run time and you make X twice as fast, the whole program speeds up by 1 / (0.1 + 0.45) ≈ 1.82, not 2.
Common traps
- Confusing step into with step over.
- Writing a test with no assertion. It passes even when the code is wrong.
- Choosing test data that cannot fail, such as an already sorted list for a sort function.
- Comparing floats with
==in tests instead ofmath.iscloseorassertAlmostEqual. - Reading
cumtimeas the function's own cost when profiling. - Memoising an impure function, or one with list arguments, which
lru_cachecannot hash.
Worked examples
1. Understanding. In testing vocabulary, what is the difference between an error, a fault and a failure? Answer: under IEEE usage, an error is a human mistake, which causes a fault (defect) in the code. When the faulty code runs, the visible wrong behaviour is the failure. Some textbooks use "bug" and "defect" interchangeably for the fault.
2. Application. How many calls does naive recursive fib(6) make, including the first?
Answer: 25. Using calls(n) = calls(n − 1) + calls(n − 2) + 1, with calls(0) = calls(1) = 1, the counts are 3, 5, 9, 15 and 25 for n = 2 to 6.
3. Analysis. A Java binary search computes int mid = (lo + hi) / 2; and fails only on arrays with more than about a billion elements. Why?
Answer: lo + hi can exceed the largest int (2,147,483,647) and overflow to a negative number, giving a negative index. Writing lo + (hi - lo) / 2 avoids the overflow. Python integers do not overflow, so the bug does not occur there.
Practise
The quiz below has 99 questions on debugging, testing and optimisation, filterable by tier and difficulty.
Practise: 99 questions
FAQs
What is the difference between debugging and testing?
Testing shows that a bug exists, by running code with chosen inputs and comparing results with expectations. Debugging finds and removes the cause, using breakpoints, stepping, logs and reasoning. A failing test usually starts a debugging session, and the test then stays in the suite as a regression check.
What is the difference between unit and integration testing?
A unit test checks one function or class on its own, with its dependencies replaced by stubs or mocks, so it is fast and pinpoints failures. An integration test runs several real components together, such as a service and its database, to check that their interfaces fit. Most projects need many unit tests and fewer integration tests.
What is code coverage, and is 100% enough?
Coverage measures how much code your tests execute: statements, branches or paths. It shows untested code, which is useful. But 100% coverage only proves each line ran, not that its result was checked. A test without strong assertions or edge-case inputs can reach full coverage and still miss bugs.
How do I know what to optimise?
Profile the program with realistic input and look for the functions with the most own time or the most calls. Fix the algorithm first, for example a list lookup inside a loop or repeated recursive calls. Measure again to confirm the gain. Leave code that the profile shows is cheap alone.
Next step
Continue to Software Development Practices MCQs for Git, clean code and code review. Revisit the Programming MCQs hub or test yourself in an NSCT mock test.