Debugging, Testing & Optimization MCQs for the NSCT

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

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

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

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

  1. What is a breakpoint in a debugger?

    1. A line that the compiler skips
    2. A marked line where execution pauses so you can inspect the program's state
    3. The point where a program crashes
    4. A comment telling other developers where a bug is
    Show answer

    Answer: B. A marked line where execution pauses so you can inspect the program's state

    When execution reaches a breakpoint, the debugger suspends the program before running that line. You can then inspect variables and the call stack and step through the code. It does not change what the program does.

  2. Execution is paused on a line that calls calculate(x). Which debugger command moves into the body of calculate?

    1. Step over
    2. Step out
    3. Step into
    4. Continue
    Show answer

    Answer: C. Step into

    Step into enters the called function and pauses at its first line. Step over runs the whole call and pauses on the next line of the current function. Step out finishes the current function and returns to its caller. Continue runs to the next breakpoint.

  3. A loop runs 10,000 times, and a bug appears only when i == 7342. Which debugger feature is most useful?

    1. An ordinary breakpoint on the loop line
    2. Step over through every iteration
    3. A conditional breakpoint with the condition i == 7342
    4. Restarting the program in release mode
    Show answer

    Answer: C. A conditional breakpoint with the condition i == 7342

    A conditional breakpoint pauses only when its expression is true, so you reach the failing iteration directly. An ordinary breakpoint would stop 7,342 times first, and stepping through every iteration is impractical.

  4. A program compiles and runs without crashing, but it prints the wrong total. What kind of error is this?

    1. Logic error
    2. Syntax error
    3. Compile-time type error
    4. Runtime exception
    Show answer

    Answer: A. Logic error

    A logic error is a mistake in the algorithm, such as a wrong formula or loop bound. The program is valid and runs to completion but gives incorrect output. Syntax and type errors stop compilation, and a runtime exception stops the program while it is running.

  5. What is a unit test?

    1. A test of the whole system by end users
    2. A test that measures how fast the program runs
    3. A manual check of the user interface
    4. An automated test that checks one small piece of code, such as a function, in isolation
    Show answer

    Answer: D. An automated test that checks one small piece of code, such as a function, in isolation

    Unit tests exercise a single function, method or class with chosen inputs and assert the expected results. They are fast and automated, and dependencies are often replaced by test doubles. End-user and performance testing are other levels and types.

  6. What is the order of the test-driven development (TDD) cycle?

    1. Write a failing test, write code to pass it, then refactor
    2. Write the code, then write tests, then refactor
    3. Refactor, write a test, then write the code
    4. Write all tests for the project first, then all the code
    Show answer

    Answer: A. Write a failing test, write code to pass it, then refactor

    TDD repeats a short loop known as red, green, refactor. Write a small test that fails (red), write the minimum code to make it pass (green), then clean up the code with the test as a safety net. Each loop adds one small behaviour.

  7. In unit testing, how does a mock differ from a stub (in the sense used by Meszaros and Fowler)?

    1. A stub verifies calls; a mock only returns canned answers
    2. A mock also verifies how it was called; a stub only returns canned answers
    3. A mock is a real database; a stub is an in-memory copy
    4. They are identical; the terms are interchangeable in every textbook
    Show answer

    Answer: B. A mock also verifies how it was called; a stub only returns canned answers

    Both are test doubles that replace real dependencies. A stub supplies fixed responses so the code under test can run. A mock is set up with expectations, and the test fails if the expected calls (for example, send_email called once) did not happen. Some libraries, such as Python's unittest.mock, use 'mock' loosely for both.

  8. After a bug fix, the team re-runs the existing test suite to make sure nothing that used to work has broken. What is this called?

    1. Acceptance testing
    2. Regression testing
    3. Smoke testing
    4. Load testing
    Show answer

    Answer: B. Regression testing

    Regression testing checks that changes have not broken previously working behaviour. An automated unit-test suite makes it cheap to run after every change. Acceptance testing checks requirements with the customer, and load testing checks behaviour under heavy use.

  9. What distinguishes white-box testing from black-box testing?

    1. White-box tests are designed from the code's internal structure; black-box tests only from the specification
    2. White-box testing is done by users; black-box testing by developers
    3. White-box tests are automated; black-box tests are always manual
    4. White-box testing checks the user interface; black-box testing checks the database
    Show answer

    Answer: A. White-box tests are designed from the code's internal structure; black-box tests only from the specification

    White-box (structural) testing uses knowledge of the code, for example to cover every branch. Black-box (functional) testing chooses inputs and expected outputs from the requirements without looking inside, for example with equivalence partitioning and boundary values. Either can be manual or automated.

  10. What is a profiler used for?

    1. Finding syntax errors
    2. Formatting code to a style guide
    3. Generating unit tests automatically
    4. Measuring where a program spends its time or memory
    Show answer

    Answer: D. Measuring where a program spends its time or memory

    A profiler records how often each function is called and how long it takes (or how much memory it uses). This shows the real hotspots, so you optimise based on measurement rather than guessing. Examples include Python's cProfile and Java's VisualVM.

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.