0% found this document useful (0 votes)
15 views6 pages

Python Exam Paper for Beginners

The document is an examination paper for a Python course, allowing students 2 hours to answer any 4 out of 5 questions. Each question tests different programming concepts including Object Oriented Programming, string manipulation, iteration, algorithm construction, and recursion. The paper includes specific tasks and coding challenges related to these topics, with marks allocated for each part of the questions.

Uploaded by

erghezi
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views6 pages

Python Exam Paper for Beginners

The document is an examination paper for a Python course, allowing students 2 hours to answer any 4 out of 5 questions. Each question tests different programming concepts including Object Oriented Programming, string manipulation, iteration, algorithm construction, and recursion. The paper includes specific tasks and coding challenges related to these topics, with marks allocated for each part of the questions.

Uploaded by

erghezi
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Python for Rookies

Example Examination Paper

Instructions to Students:

● Time Allowed: 2 hours.

● This is Open Book Examination.

● All questions carry 25 marks.

● There are 5 questions in this exam. You should answer any 4 questions.
Question 1.
This question tests your understanding of Object Oriented Programming. The following
code defines the start of a class to represent bank accounts:

class BankAccount(object):
interest_rate = 0.3
def __init__(self, name, number, balance):
[Link] = name
[Link] = number
[Link] = balance
return

a) Name the class variables and the instance variables in the given code. [5 marks]
b) Add instance methods called deposit() and withdraw() which increase and
decrease the balance of the account. Make sure the withdraw() method doesn't allow
the account to go into overdraft. Add a third method called add_interest() which adds
interest to the balance (the interest should be the interest rate multiplied by the current
balance). [10 marks]

c) Create a subclass of BankAccount called StudentAccount. Every


StudentAccount should have an overdraft limit of 1000. Write a constructor for the new
class. Override the withdraw() method to make sure that students can withdraw money
up to their overdraft limits. [10 marks]
Question 2.
This question tests your ability to manipulate strings.
A palindromic word is one that reads the same backwards as forwards. Hence the words
hello and peel are not palindromes, but the words peep, deed and dad are palindromes.
a) Create a class called Palindrome. [2 marks]

b) In your Palindrome class, create a method called reverse() which takes a string
argument. Your method should return the reverse of the argument as a string. For
example, if the argument is Foobar then your method should return rabooF . [8
marks]

c) Create a second method in Palindrome called isPalindrome() which takes a string


argument. This method should return True if the argument is a palindrome and False
otherwise. [5 marks]

HINT: Probably the easiest way to do this is to use the result of your reverse() method
and compare the string against its reverse.

d) Write some code to test your new Palindrome class and print out results of your testing
to the user. Give some consideration to what sort of strings you might want to use for your
testing. [10 marks]
Question 3.
This question tests your understanding of iteration (e.g. loops and generators). Remember,
you will not be marked down for small errors in syntax or typos, so long as you
demonstrate that you understand how iteration works in programming languages.

a) A program to sum the number of integers from 1 to a given number n. [4 marks]

b) A program which sums the contents of a integer list or array. [5 marks]

c) Translate the following for loop into a while loop (which does the same thing as the
for loop):
for i in range(1,10):
print "i = ", i
[8 marks]

d) Translate the following while loop into a for loop (which does the same thing as the
while loop):
i = 20
while (i > 0):
print "i = ", i
i =- 1
[8 marks]
Question 4.
This question tests your ability to construct algorithms. Your answers can be in English,
pseudo-code or Python.
You may have already used a language like Logo or the Python turtle module, which
allows you to program a simple robot which can move forwards and backwards or rotate
left and right. The remainder of this question requires you to consider how you might
design algorithms to instruct the turtle to perform useful tasks.

a) How would you tell the turtle to move in a complete circle? [5 marks]

b) Imagine that your turtle is now equipped with sensors which can tell if they are over a
white pixel or a black pixel (see the screenshot below). Your mission is to move around the
screen and find a black pixel. How would you tell the turtle to do this?

More marks will be awarded for answers that cope well with difficult test cases. For
example, a terrain that is all-white except for a single black pixel in a corner. [5 marks]

c) The screenshot above shows that the turtle is placed on a track. Each of the three red
dots on the turtle is one of it's sensors. In the picture, one sensor is over a black pixel and
two are over white pixels (see the diagram on the bottom right of the screenshot). The
turtle is currently at the start of the track. Describe (in English) how you would instruct the
turtle to move around the track, given that it can move forwards and backwards, rotate left
and right and tell whether it's sensors are over black or white pixels. [15 marks]
Question 5.
This questions tests your understanding of recursion, which is a fundamental concept in
programming.

a) What is a recursive function? [2 marks]

b) The following iterative function returns the sum of all elements in a list. For example,
given the list [1, 2, 3] the function will return 1+2+3 or 6. Re-write the function as a
recursive function.
def sum(arg):
result = 0
for i in arg:
result += i
return result

[5 marks]

c) The following code implements a recursive function in Python called foobar. What
does the foobar function do? Write a line of code which calls the foobar function with a
suitable argument and state what the return value will be.

def foobar(arg):
if arg == []:
return arg
else:
return foobar(arg[1:]) + [ arg[0] ]
[8 marks]

d) The following code implements two mutually recursive functions – functions which call
each other:

def ise(n):
if n==0: return True
else: return iso(n-1)
def iso(n):
if n==0: return False
else: return ise(n-1)

What will the following function calls return:

• iso(3)
• iso(2)
• ise(3)
• ise(2)

What do the functions iso and ise do?

[10 marks]

You might also like