60 practice questions
Explain object-oriented programming and its three key principles.
mediumWhat is the difference between an instance variable and a class variable?
hardExplain what inheritance allows in OOP with an example.
mediumWhat is encapsulation and why is it important?
mediumWhat does polymorphism allow in OOP?
hardA class Animal has a method MakeSound(). Subclasses Dog and Cat each override MakeSound() with their own version. What OOP concept does this demonstrate?
mediumIn OOP, what does it mean for an attribute to be declared as PRIVATE?
easyWhat is the key difference between 'inheritance' and 'composition' as ways of relating two classes?
mediumExplain the purpose of a constructor in object-oriented programming.
mediumAn 'abstract class' in OOP is best described as a class that:
hardWhat is a recursive subroutine and what two things must it have?
mediumWhich statement about recursion and iteration is correct?
mediumExplain why a recursive subroutine must have a base case.
mediumTrace 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).
mediumIn a recursive subroutine, the 'recursive case' is the part that:
easyDiscuss one advantage and one disadvantage of using a recursive solution compared to an iterative (loop-based) solution.
mediumWhich of the following problems is most naturally suited to being solved using recursion?
mediumA 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).
hardWhat is the most likely consequence of writing a recursive subroutine without a correctly defined base case?
mediumA recursive subroutine sums all the numbers from 1 to n. Describe how this could be rewritten using iteration (a loop) instead of recursion.
mediumWhat is the purpose of pseudocode when designing an algorithm?
easyDescribe stepwise refinement (top-down design).
mediumWhich flowchart symbol represents a decision?
easyWhat is modular programming?
easyDescribe the difference between a flowchart and pseudocode.
easyExplain what an IDE provides that a plain text editor does not.
easyWhat is the purpose of a version control system (VCS)?
easyWhat does a compiler do differently from an interpreter at run time?
mediumExplain the difference between passing a parameter by value and passing it by reference.
mediumExplain why using meaningful identifier names (e.g. studentAge rather than x) is considered good programming practice.
easyExplain the difference between opening a text file in 'read' mode, 'write' mode, and 'append' mode.
mediumA 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?
easyExplain why it is important for a program to close a file after it has finished reading from or writing to it.
mediumWhich 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?
mediumWrite 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.
mediumA 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?
mediumExplain 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).
hardWhich file handling operation adds new data to the end of an existing file without removing its current contents?
easyExplain why a program that attempts to open a file for reading should include error handling in case the file does not exist.
mediumIn 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?
easyExplain the difference between white-box and black-box testing in the context of a subroutine.
mediumWhich 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?
easyDescribe how a "stepping" feature in a debugger helps a programmer find a logic error.
mediumWhat will the following pseudocode output? count = 0 WHILE count < 3 OUTPUT count count = count + 1 ENDWHILE
easyA 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?
mediumExplain what is meant by "defensive design" in programming, giving one example technique.
mediumExplain what is meant by 'exception handling' in a program, and why it is useful.
mediumWhich of the following is an example of input validation?
easyExplain the difference between validation and verification when checking data entered into a system.
mediumA 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?
mediumA program must find the largest value in a list. Write pseudocode.
mediumExplain 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.
mediumA 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?
easyExplain how a stack could be used to implement an 'undo' feature in a text editor.
mediumA 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?
mediumA 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.
hardWhich data structure allows efficient insertion and removal of items from both the front and the back?
mediumExplain, with an example, why the choice of data structure can have a significant impact on the efficiency of a program.
mediumWhich data structure is specifically designed to store data as pairs, where each unique key maps to an associated value?
easyA 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.
mediumdef 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.| Recursion | Iteration | |
|---|---|---|
| Readability | Elegant for naturally recursive problems (trees, divide & conquer) | Clearer for simple repetition |
| Memory | Uses the call stack โ risk of stack overflow | Constant extra memory |
| Speed | Function-call overhead | Generally faster |
| Termination | Needs a base case | Needs a stopping condition |
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| Method | What it is | Example use |
|---|---|---|
| Backtracking | Build a solution incrementally; undo a choice that violates a constraint and try another | Sudoku, maze solving, the N-queens problem |
| Heuristics | A practical "good enough" rule that finds acceptable solutions quickly when an exact one is too slow | Route finding, heuristic antivirus detection |
| Data mining | Searching large data sets for patterns, trends and correlations | Recommendations, fraud detection, market analysis |
| Performance modelling | Simulating a system under load before building it | Sizing servers; testing networks |
| Visualisation | Presenting data/relationships graphically to aid understanding | Graphs, heat maps, dashboards |
| Pipelining | The output of one processing stage feeds directly into the next | CPU instruction pipeline; data processing pipelines |