Marked Activity PROGRAMMING IN PYTHON
Exercise 1 — Programming Languages and Python Basics
A. Distinguish between procedural, object-oriented, and scripting languages. Give one
example of each. (3 marks)
B. Explain the difference between statically typed and dynamically typed languages. State
where Python belongs. (3 marks)
C. Explain what an interpreter does in Python. Why is Python considered platform-
independent? (4 marks)
D. What is a Python virtual environment, and why is it important? Mention two
advantages. (2 marks)
E. Explain the difference between a syntax error and a runtime error with examples. (2
marks)
F. Explain the purpose of try, except, else, and finally. (2 marks)
G. Explain the difference between recursion and iteration. Give one advantage and one
disadvantage of recursion. (4 marks)
H. Write a recursive function that computes the sum of the first n natural numbers. (3 marks)
I. Why is recursion sometimes discouraged in Python? Explain the concept of maximum
recursion depth. (3 marks)
Exercise 2 — Strings, Conditions, and Functions
A. Define a string in Python and list two common string operations. (2 marks)
B. Write a Python function reverse_string(s) that returns the reverse of a given string without
using built-in reverse functions. (4 marks)
C. Write a function count_vowels(s) that returns the number of vowels in a string (both
uppercase and lowercase). (4 marks)
Exercise 3 — Lists, Tuples, and Sets
A. Explain the difference between a list, a tuple, and a set in Python. Give one example use-
case for each. (4 marks)
B. Given the list:
values = [4, 7, 2, 7, 9, 2, 5, 4]
Write Python code to:
1. Remove duplicate values while preserving order
2. Display the maximum and minimum values (6 marks)
3. Use dict or counter to compute the frequency of each element. (3 marks)
Exercise 4 — Loops and Comprehensions
A. Explain the difference between a for loop and a while loop. Give one situation where each
is preferred. (4 marks)
B. Using a loop, write a program that prints all numbers between 1 and 50 that are divisible by
both 3 and 5. (4 marks)
C. Rewrite part (B) using a list comprehension. (2 marks)
Exercise 5 — Dictionaries and Data Processing
A. Explain what a dictionary is and how it differs from a list. (3 marks)
B. Write a Python program that counts how many times each word appears in the sentence:
"Artificial intelligence is transforming intelligence systems" (7 marks)
Exercise 6 — File Handling and Exceptions
Part 1
A. Explain the purpose of file modes 'r', 'w', 'a', and 'r+'. (4 marks)
B. Write a Python program that reads a file named [Link] containing one score per line and
prints the average score. (4 marks)
C. Modify the program to handle:
File not found errors
Invalid (non-numeric) entries (2 marks)
Part 2
You are analyzing students’ grades stored in a CSV file:
Name,Quiz,Midterm,Final
Alice,7,18,40
Bob,9,15,38
Carine,10,20,42
A. Write a Python program using the csv module to read the file and compute each
student’s total score. (5 marks)
B. Modify your program to compute the class average of the final exam only. (3 marks)
C. Explain one advantage of using the csv module instead of manual string processing. (2
marks)
Exercise 7 — Functions and Recursion
A. Explain recursion and state two conditions required for a recursive function. (4 marks)
B. Write a recursive function power(base, exp) that computes base^exp. (3 marks)
C. Explain one advantage and one disadvantage of recursion in Python. (3 marks)
Exercise 8 — Object-Oriented Programming (OOP)
A. Define Object-Oriented Programming and explain the concepts of class and object. (4
marks)
B. Explain the difference between inheritance and polymorphism with short examples.
C. Write a Python class Rectangle with attributes length and width, and a method area() that
returns the area. (3 marks)
D. Create a subclass Square that inherits from Rectangle and automatically sets length and
width to the same value. (3 marks)
E. Write a Python class BankAccount with (5 marks):
Attributes:
owner:
balance:
Required Methods:
1. deposit(amount)
o Accepts a positive numeric amount.
o Adds the amount to the current balance.
o Should return the updated balance or a confirmation message.
o Must not accept negative amounts (award credit for validation if
included).
2. withdraw(amount)
o Accepts a positive numeric amount.
o Deducts the amount from the balance only if sufficient funds exist.
o If the amount exceeds the current balance, the method must raise a
ValueError or return a clear “Insufficient funds” message, depending on
the candidate’s approach.
o Should return the updated balance when successful.
Exercise 9 — Problem Solving with Python
A. Write a function that accepts a list of integers and returns a new list containing only values
greater than the average of the list. (5 marks)
B. Explain how breaking a problem into functions improves code quality and maintainability.
(5 marks)
Part 2 –
Consider the following code snippet:
def process(items):
result = []
for i in range(len(items)):
if items[i] % 2 == 0:
[Link]([Link](i))
return result
a = [6, 7, 8, 9]
print(process(a))
print(a)
A. Write the exact output produced when the code runs and explain, step-by-step, why that
output occurs. (6 marks)
B. Identify the bug(s) and rewrite process so it safely collects and returns all even numbers
from items without corrupting the iteration. Explain why your fix works. (4 marks)