State two components found within the CPU.
Main CPU components
Arithmetic Logic Unit (ALU)
Control Unit (CU)
Registers (inside the CPU)
Registers – very small, fast memory locations inside the CPU
Accumulator (ACC)
Program Counter (PC)
Instruction Register (IR)
Memory Address Register (MAR)
Memory Data Register (MDR / MBR)
Consider the program below. What will be printed and why ?
7
10
State one difference between a static data structure and a dynamic data structure.
A static data structure has a fixed size that cannot change during program execution.
A dynamic data structure can change its size while the program is running.
O(log n)
State three file opening modes used when working with sequential files in Python.
"r" (read mode) – opens a file for reading only.
"w" (write mode) – opens a file for writing; existing content is overwritten or a new file is created.
"a" (append mode) – opens a file for writing and adds new data to the end of the file without deleting existing content.
Describe the fetch phase of the fetch-decode-execute cycle.
During the fetch phase, the CPU retrieves the next instruction from main memory. The address of the next instruction is held in the Program Counter (PC) and is copied to the Memory Address Register (MAR). The instruction stored at that memory address is then fetched from memory and placed into the Memory Data Register (MDR), after which it is copied into the Instruction Register (IR). The Program Counter is then incremented so that it points to the next instruction to be executed.
State the output
String name = "MacKenty"; String sub = name.substring(0, 3);
"Mac"
queue is a data structure that follows the First In, First Out (FIFO) principle, where the first element added to the queue is the first one removed.
Construct code for sequential search
def linear_search(arr, target):
for index in range(len(arr)):
if arr[index] == target:
return index
return -1
State what the function read() returns when it reaches end-of-file (EOF).
When the read() function reaches end-of-file (EOF), it returns an empty string ("").
State the decimal equivalent of the binary number 1100101.
101
State 4 common debugging techniques
Trace Tables
Print Statements
Breakpoint Debugging
Step-by-Step Execution (Stepping)
The following operations are performed on an empty stack:
State the final contents of the stack .
D
B
A
Construct code for bubble sort
for i from 0 to n - 1:
for j from 0 to n - i - 2:
if array[j] > array[j + 1]:
swap array[j] and array[j + 1]
A text file called words.txt already exists.
Question:
Construct a Python program that:
Opens the file words.txt in append mode.
Writes the word Hello into the file on a new line.
Closes the file after writing.
with open("words.txt", "a") as file:
file.write("Hello\n")
Construct a truth table for the logic expression (A NAND B) NOR C.
0
0
0
0
0
0
1
0
Explain how exception handling improves both program reliability and user experience. Use one example to support your answer.
Exception handling improves reliability by preventing a program from crashing when an error occurs. It also improves user experience by providing meaningful error messages instead of abrupt termination. For example, handling a ValueError when converting user input to an integer allows the program to prompt the user to re-enter valid data instead of stopping execution.
Given the following list of numbers:
Question: Find the maximum and minimum values in the list.
max_value = numbers[0]
min_value = numbers[0]
for num in numbers:
if num > max_value:
max_value = num
if num < min_value:
min_value = num
print("Maximum value:", max_value)
print("Minimum value:", min_value)
Construct code for binary search
def binary_search(arr, target):
low = 0
high = len(arr) - 1
while low <= high:
mid = (low + high) // 2
if arr[mid] == target:
return mid
elif arr[mid] < target:
low = mid + 1
else:
high = mid - 1
return -1
Explain what is meant by bytes and buffer size when reading and writing files, and state one advantage of reading a file in chunks instead of reading it all at once.
A byte is the smallest unit of data storage used when reading from or writing to a file.
Different characters may use different numbers of bytes depending on the encoding (for example, "A" uses 1 byte in ASCII, while some UTF-8 characters use multiple bytes).
The buffer size determines how much data is transferred between the file and memory at one time.
Reading a file in chunks (for example using read(n)) is more efficient because it reduces memory usage, especially when working with very large files.
Deduce a simplification of the Boolean expression using the
idempotent law: A AND A AND B AND B
A AND B
Construct a Python statement that extracts the domain name from the email
student2025@nis.edu.kz
email = "student2025@nis.edu.kz"
domain = email[email.index("@")+1:]
print(domain)
A two-dimensional list stores students’ scores, where:
each row represents a student
each column represents a subject
Question:Calculate the average score for each student.
student_averages = []
for student in scores:
average = sum(student) / len(student)
student_averages.append(average)
print("Average score for each student:")
for i in range(len(student_averages)):
print(student_averages[i])
Construct code for selection sort
for i from 0 to n - 1:
min_index = i
for j from i + 1 to n - 1:
if array[j] < array[min_index]: min_index = j
swap array[i] and array[min_index]
A text file called students.txt already exists.
Each line in the file contains information about one student in the following format:
Example:
Question:
Construct a Python program that:
Opens the file students.txt in read mode.
Reads the file line by line.
Counts how many students study Computer Science.
count = 0
with open("students.txt", "r") as file:
for line in file:
parts = line.split()
subject = parts[1]
if subject == "ComputerScience":
count += 1
print("Number of students studying Computer Science:", count)