Revisionโ€บOCR A Levelโ€บProblem Solving & Programming
OCR A Level H446 ยท Topic 2.2

Problem Solving & Programming

60 practice questions

Practice Questions

60 questions

Explain object-oriented programming and its three key principles.

medium

What is the difference between an instance variable and a class variable?

hard

Explain what inheritance allows in OOP with an example.

medium

What is encapsulation and why is it important?

medium

What does polymorphism allow in OOP?

hard

A class Animal has a method MakeSound(). Subclasses Dog and Cat each override MakeSound() with their own version. What OOP concept does this demonstrate?

medium

In OOP, what does it mean for an attribute to be declared as PRIVATE?

easy

What is the key difference between 'inheritance' and 'composition' as ways of relating two classes?

medium

Explain the purpose of a constructor in object-oriented programming.

medium

An 'abstract class' in OOP is best described as a class that:

hard

What is a recursive subroutine and what two things must it have?

medium

Which statement about recursion and iteration is correct?

medium

Explain why a recursive subroutine must have a base case.

medium

Trace through the recursive calls made by a function Factorial(n), which returns 1 if n=0, otherwise returns n * Factorial(n-1), when called as Factorial(3).

medium

In a recursive subroutine, the 'recursive case' is the part that:

easy

Discuss one advantage and one disadvantage of using a recursive solution compared to an iterative (loop-based) solution.

medium

Which of the following problems is most naturally suited to being solved using recursion?

medium

A function Fib(n) returns n if n is 0 or 1, otherwise returns Fib(n-1) + Fib(n-2). Show the recursive calls made (and their return values) when calculating Fib(4).

hard

What is the most likely consequence of writing a recursive subroutine without a correctly defined base case?

medium

A recursive subroutine sums all the numbers from 1 to n. Describe how this could be rewritten using iteration (a loop) instead of recursion.

medium

What is the purpose of pseudocode when designing an algorithm?

easy

Describe stepwise refinement (top-down design).

medium

Which flowchart symbol represents a decision?

easy

What is modular programming?

easy

Describe the difference between a flowchart and pseudocode.

easy

Explain what an IDE provides that a plain text editor does not.

easy

What is the purpose of a version control system (VCS)?

easy

What does a compiler do differently from an interpreter at run time?

medium

Explain the difference between passing a parameter by value and passing it by reference.

medium

Explain why using meaningful identifier names (e.g. studentAge rather than x) is considered good programming practice.

easy

Explain the difference between opening a text file in 'read' mode, 'write' mode, and 'append' mode.

medium

A program needs to add a new line of text to the end of an existing log file, without deleting any of the data already stored in it. Which file mode should be used?

easy

Explain why it is important for a program to close a file after it has finished reading from or writing to it.

medium

Which programming construct would typically be used to read and process every line of a text file, one line at a time, until the end of the file is reached?

medium

Write pseudocode to open a text file called 'scores.txt' in write mode, write the value of a variable called score to it, and then close the file.

medium

A program opens an existing text file containing important data using 'write' mode (overwrite), intending only to add new data. What is the most likely consequence?

medium

Explain the difference between storing data in a simple text file using sequential access, and storing the same data using a more structured format (e.g. with fields separated by a delimiter, such as a CSV).

hard

Which file handling operation adds new data to the end of an existing file without removing its current contents?

easy

Explain why a program that attempts to open a file for reading should include error handling in case the file does not exist.

medium

In OCR exam reference pseudocode, which of the following function calls would be used to open a file so that data can be read from it?

easy

Explain the difference between white-box and black-box testing in the context of a subroutine.

medium

Which programming construct should be used when a set of statements must repeat until a condition becomes true, with the body always executing at least once?

easy

Describe how a "stepping" feature in a debugger helps a programmer find a logic error.

medium

What will the following pseudocode output? count = 0 WHILE count < 3 OUTPUT count count = count + 1 ENDWHILE

easy

A program needs to read a number from the user and ensure it is between 1 and 10 inclusive, re-asking if not. Which construct is most appropriate?

medium

Explain what is meant by "defensive design" in programming, giving one example technique.

medium

Explain what is meant by 'exception handling' in a program, and why it is useful.

medium

Which of the following is an example of input validation?

easy

Explain the difference between validation and verification when checking data entered into a system.

medium

A program asks the user for two numbers and divides the first by the second. The user enters 0 as the second number. Without exception handling, what is most likely to happen?

medium

A program must find the largest value in a list. Write pseudocode.

medium

Explain why a dictionary (key-value pair structure / hash map) might be a more appropriate choice than a list for looking up a student's grade by their student ID.

medium

A system processes customer support tickets in the order they are received โ€” the first ticket submitted is the first to be dealt with. Which data structure is most appropriate for storing these tickets?

easy

Explain how a stack could be used to implement an 'undo' feature in a text editor.

medium

A program needs to store a collection of unique usernames, where duplicates should never be allowed, and the order of items does not matter. Which data structure is most appropriate?

medium

A programmer is designing a grid-based puzzle game (e.g. a 10x10 board). Discuss whether a 2D array or a list of individual records (each storing a row and column number) would be a more appropriate data structure for representing the board.

hard

Which data structure allows efficient insertion and removal of items from both the front and the back?

medium

Explain, with an example, why the choice of data structure can have a significant impact on the efficiency of a program.

medium

Which data structure is specifically designed to store data as pairs, where each unique key maps to an associated value?

easy

A program needs to store a list of items where items are frequently inserted and removed from the middle of the collection, but direct access by index is rarely needed. Discuss whether an array or a linked list would be more appropriate, and why.

medium

Revision Notes

Programming Techniques

โญ Exam tip: Recursion vs iteration comparisons are common โ€” know that recursion uses the call stack and can overflow, while iteration is usually more memory-efficient.
Sequence, selection, iteration:The three structured-programming constructs: sequence (statements in order), selection (IF / CASE choosing between paths) and iteration (count-controlled FOR, or condition-controlled WHILE / REPEAT-UNTIL). Any algorithm can be built from these three.
Local vs global scope:A local variable exists only within the subroutine that declares it and is destroyed when the subroutine ends; a global variable is accessible throughout the program. Prefer local variables: they prevent accidental name clashes and unintended side effects, and keep modules independent and reusable.
Parameters: by value vs by reference:Passing by value sends a COPY โ€” changes inside the subroutine do not affect the original. Passing by reference sends the variable's location โ€” the subroutine can change the caller's original value. A function returns a value; a procedure performs an action without necessarily returning one.
Recursion: factorial + how the stack works
def factorial(n):
    if n == 0:            # BASE CASE โ€” stops recursion
        return 1
    return n * factorial(n - 1)   # RECURSIVE CASE

factorial(3):
  factorial(3) waits for factorial(2)
    factorial(2) waits for factorial(1)
      factorial(1) waits for factorial(0)
        factorial(0) = 1   โ† base case, unwinds
      = 1 * 1 = 1
    = 2 * 1 = 2
  = 3 * 2 = 6
Each pending call is held on the CALL STACK; with no
base case (or too deep) the stack overflows.
RecursionIteration
ReadabilityElegant for naturally recursive problems (trees, divide & conquer)Clearer for simple repetition
MemoryUses the call stack โ€” risk of stack overflowConstant extra memory
SpeedFunction-call overheadGenerally faster
TerminationNeeds a base caseNeeds a stopping condition
Modularity & the IDE:Breaking a program into subroutines/modules aids development, testing and reuse. An IDE supports this with an editor, syntax highlighting, auto-complete, a debugger (breakpoints, step, variable watch), error diagnostics and a built-in run/translate tool.
โš ๏ธ Common mistake: Every recursive routine MUST have a reachable base case that the recursive calls move towards; otherwise it recurses forever and overflows the call stack. Examiners look for you to identify the base case explicitly.

Object-Oriented Programming

โญ Exam tip: Be ready to write a class with a constructor, private attributes and methods, and to define encapsulation, inheritance and polymorphism with an example of each.
Class, object, attribute, method:A class is a template/blueprint defining attributes (data) and methods (behaviour). An object is an instance of a class with its own attribute values. The constructor initialises a new object's attributes when it is created.
Encapsulation:Bundling data and the methods that operate on it inside an object, and hiding the internal state behind private attributes accessed only through public getter/setter methods. This protects data integrity (values can be validated in setters) and lets the implementation change without breaking code that uses the object.
Inheritance:A subclass inherits the attributes and methods of a superclass (an IS-A relationship โ€” a Dog IS-A Animal), reusing code and extending it. Avoid over-deep hierarchies, which become rigid and tightly coupled.
Polymorphism:The same method call behaves differently depending on the object's class. Achieved by overriding an inherited method โ€” e.g. every Animal has speak(), but Dog.speak() returns "Woof" and Cat.speak() returns "Meow", and code can call speak() on any Animal without knowing its exact type.
OOP example
class Animal:
    def __init__(self, name):
        self.__name = name           # private (encapsulation)
    def get_name(self):              # getter
        return self.__name
    def speak(self):
        return '...'

class Dog(Animal):                   # inheritance
    def speak(self):                 # polymorphism (override)
        return 'Woof'

for a in [Dog('Rex'), Animal('Thing')]:
    print(a.get_name(), a.speak())   # same call, different result
๐Ÿ’ก "Favour composition over inheritance": a HAS-A relationship (a Car HAS-A Engine object) is often more flexible than deep inheritance, because behaviours can be swapped at run time without rewriting class hierarchies.
โš ๏ธ Common mistake: Encapsulation is more than "using a class". The key marks are for PRIVATE attributes and controlled access via methods โ€” making attributes public defeats the purpose.

Computational Methods

Problem recognition & decomposition:Recognising the features that make a problem amenable to a computational solution, then decomposing it into sub-problems and applying <strong>divide and conquer</strong> โ€” repeatedly breaking the problem into smaller parts, solving them, and combining results (as in binary search and merge sort).
MethodWhat it isExample use
BacktrackingBuild a solution incrementally; undo a choice that violates a constraint and try anotherSudoku, maze solving, the N-queens problem
HeuristicsA practical "good enough" rule that finds acceptable solutions quickly when an exact one is too slowRoute finding, heuristic antivirus detection
Data miningSearching large data sets for patterns, trends and correlationsRecommendations, fraud detection, market analysis
Performance modellingSimulating a system under load before building itSizing servers; testing networks
VisualisationPresenting data/relationships graphically to aid understandingGraphs, heat maps, dashboards
PipeliningThe output of one processing stage feeds directly into the nextCPU instruction pipeline; data processing pipelines
Abstraction in problem solving:Modelling only the relevant features of a real-world problem so it can be represented and solved computationally โ€” the same skill that underpins simulations, where assumptions simplify reality enough to compute with.
๐Ÿ’ก For "why use a heuristic?": because the exact (optimal) algorithm is intractable โ€” too slow for the input size โ€” so a heuristic trades guaranteed optimality for a good-enough answer found in acceptable time.