Problem Solving Using Programming MCQs for NSCT

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

Problem solving using programming means turning a worded problem into short, correct code and predicting exactly what a given piece of code will do. 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 application-tier questions averaged 45.1%, per the HEC NSCT report.

From problem to code

Most short programs are built from a few standard patterns. Spotting which one a task needs is half the work.

Pattern What it does Typical setup
Accumulator Builds a running total or product total = 0 before the loop, total += x inside
Counter Counts items that meet a condition count += 1 inside an if
Flag Records whether something happened found = False, set to True in the loop
Running maximum Tracks the best value so far best = a[0], then compare each item
Digit loop Processes a number digit by digit n % 10 for the last digit, n //= 10 to drop it

Stepwise refinement turns a large task into these pieces: state the task in one line, split it into steps, and refine each step until it is a few lines of code. Exam angle: "Which code solves the task?" items usually differ in one detail, such as > against >= or a count against a sum, so check each option on a small input.

Code tracing

Code tracing (a dry run) means executing code by hand and recording each variable's value in a trace table.

a, b = 0, 1
for _ in range(4):
    a, b = b, a + b
print(a)
Pass a b
start 0 1
1 1 1
2 1 2
3 2 3
4 3 5

It prints 3. In the tuple assignment both right-hand values are computed before either variable changes. Exam angle: writing a = b and b = a + b on separate lines gives a different result, and examiners like that contrast.

Output prediction

Output prediction questions test exact language behaviour. The same expression can give different results in different languages:

Expression Python 3 C, C++, Java (int)
7 / 2 3.5 3
-7 // 2 (Python), -7 / 2 (C, Java) -4 (floors) -3 (truncates)
-7 % 2 1 -1
2 ** 3 ** 2 512 (right-associative) no ** operator

Other favourites are slicing (s[::-1] reverses), list aliasing (y = x shares one list), recursion order (a print after the recursive call runs on the way back), and JavaScript's default sort(), which compares numbers as strings. Exam angle: read the exact print format too: end=" ", commas and brackets all change which option matches.

Common traps

Worked examples

1. Understanding. A task asks how many students scored above 80. Which pattern fits best?

Answer: a counter. Set count = 0 before the loop and add 1 inside if mark > 80. An accumulator would add the marks themselves, which answers a different question.

2. Application. What does this print?

x = 1
for i in range(1, 5):
    x = x * i if i % 2 else x + i
print(x)

Answer: 13. Odd i multiplies and even i adds: i = 1 gives 1 × 1 = 1, i = 2 gives 1 + 2 = 3, i = 3 gives 3 × 3 = 9, and i = 4 gives 9 + 4 = 13.

3. Analysis. This code should return the largest of three numbers. What does big3(7, 2, 3) return, and why?

def big3(a, b, c):
    if a > b and a > c:
        big = a
    if b > a and b > c:
        big = b
    else:
        big = c
    return big

Answer: 3, which is wrong. The first if sets big = 7, but the second if is a separate statement. Its test is false, so its else runs and overwrites big with c. Changing the second if to elif makes the three branches exclusive and returns 7.

Practise Problem Solving Using Programming 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. When you dry-run code by hand, what is a trace table used for?

    1. Recording the value of each variable after every step
    2. Storing the final output only
    3. Listing the syntax errors in the code
    4. Measuring how long each line takes to run
    Show answer

    Answer: A. Recording the value of each variable after every step

    A trace table has one column per variable (and often one for output) and one row per step. Filling it in line by line shows exactly how values change, which is the most reliable way to predict output or spot a fault.

  2. Which Python operator gives the remainder after integer division?

    1. //
    2. /
    3. **
    4. %
    Show answer

    Answer: D. %

    % is the modulo (remainder) operator, so 17 % 5 is 2. // is floor division (17 // 5 is 3), / is true division and ** is exponentiation. Modulo is used for even/odd tests, digit extraction and wrap-around indexes.

  3. You need to keep asking for a password until the user types the correct one. Which loop fits best?

    1. No loop; one if statement
    2. A while loop
    3. A for loop over range(10)
    4. A for loop over the password's characters
    Show answer

    Answer: B. A while loop

    A while loop repeats as long as a condition holds, so it suits cases where the number of repetitions is not known in advance. A for loop over range(10) caps the attempts at a fixed count, which is a different requirement.

  4. In the accumulator pattern, where is the accumulator variable (for example total = 0) usually initialised?

    1. Once, before the loop starts
    2. Inside the loop, at the start of each pass
    3. After the loop ends
    4. It never needs initialising
    Show answer

    Answer: A. Once, before the loop starts

    An accumulator collects a running result such as a sum, count or product. It is set once before the loop and updated on each pass. Resetting it inside the loop throws away the earlier values.

  5. In Python, when is b evaluated in the expression a and b?

    1. Never, because and only looks at a
    2. Only when a is falsy
    3. Always, before a
    4. Only when a is truthy
    Show answer

    Answer: D. Only when a is truthy

    and short-circuits: if a is falsy the result is already known to be falsy, so b is skipped. This is why a guard such as len(s) > 0 and s[0] == 'x' is safe on an empty string. or does the reverse and skips b when a is truthy.

  6. A global variable x is 10. A function then runs x = 5 inside its body, with no global statement. What is the global x after the call?

    x = 10
    def f():
        x = 5
    f()
    print(x)
    1. None
    2. 5
    3. It raises NameError
    4. 10
    Show answer

    Answer: D. 10

    Assigning to a name inside a function creates a local variable by default, so the global x is untouched. To change the global you need a global x statement, though returning a value is usually cleaner.

  7. How do you get the last element of a non-empty Python list a?

    1. a[last]
    2. a[0]
    3. a[-1]
    4. a[len(a)]
    Show answer

    Answer: C. a[-1]

    Negative indexes count from the end, so a[-1] is the last element and a[-2] the one before it. a[len(a)] is one past the end and raises IndexError, which is a classic off-by-one.

  8. What is the difference between return and print inside a function?

    1. return sends a value back to the caller; print only displays text
    2. They are interchangeable
    3. return also prints the value to the screen
    4. print sends a value back; return only displays it
    Show answer

    Answer: A. return sends a value back to the caller; print only displays text

    return ends the function and gives a value to the code that called it, which can store or reuse it. print writes text to the screen and the function still returns None. Output questions often hinge on a function that prints instead of returning.

  9. In a search loop, a variable found = False is set to True when the target is seen. What is this variable called?

    1. An accumulator
    2. A flag
    3. A constant
    4. A loop counter
    Show answer

    Answer: B. A flag

    A flag is a Boolean that records whether a condition has happened. After the loop you test the flag to decide what to report. An accumulator, by contrast, builds a running value such as a sum.

  10. Which two parts must every correct recursive function have?

    1. A loop and a global variable
    2. Two recursive calls and no base case
    3. A print statement and a return statement
    4. A base case, and a recursive call that moves towards it
    Show answer

    Answer: D. A base case, and a recursive call that moves towards it

    The base case stops the recursion, and each recursive call must make the problem smaller so that the base case is eventually reached. Missing either part leads to infinite recursion and, in Python, a RecursionError.

FAQs

How do I get faster at output prediction?

Trace on paper with a table instead of in your head, and practise on short snippets daily. Learn the handful of language rules that examiners reuse: range bounds, floor division, slicing, mutable lists, default arguments and recursion order. Once those are automatic, most questions become two or three minutes of careful tracing.

Which language should I practise in?

Our questions mainly use Python, with some JavaScript, because both read like pseudocode. The skills of tracing, choosing loops and spotting boundary errors carry over to C++ and Java. Learn the few rules that differ, especially integer division and the modulo of negative numbers, so a change of language does not catch you out.

How is this topic different from the Programming area?

This topic tests problem solving through code: tracing logic, predicting output and picking the right pattern for a task. Programming is a separate area in HEC's list, with its own topics on language concepts. The two overlap, so practising both helps; see the Programming MCQs.

What should I do when a trace gets long?

Trace the first two or three passes fully, then look for the pattern, such as a value doubling or a sum of the first n numbers. Use that pattern to jump to the final pass, then check the last step by hand. This avoids arithmetic slips across ten or more passes.

Next step

Continue with Data-Driven Problem Solving, the next topic in HEC's order. You can also review Complexity & Efficiency Awareness, browse all Problem Solving MCQs, or try an NSCT mock test.