You are on page 1of 11

*** Sample Exam ***

ALLOWABLE MATERIALS AND INSTRUCTIONS TO CANDIDATES:

1. The examination paper contains 5 questions. Attempt all questions in the exam paper itself.
2. This exam is a hurdle and contributes 50% to the overall assessment. Total marks for this
exam is 50.
3. This is a closed-book examination. You may not refer to any other material during the
examination.
4. This exam lasts for two hours. If you complete it sooner, you are encouraged to check your
answers carefully in the remaining time. Do those questions you feel confident with first.
Questions are NOT of equal value.
5. Electronic devices are not permitted. Mobile phones must be switched off. No computer
access is permitted during this exam.
6. When writing code, observe the usual style guidelines for meaningful names and layout. You
are encouraged to rewrite your final solution if necessary.
7. If you are unclear about a question and feel that you need to make an assumption, state it
clearly with your answer.

Page 1 of 11

Question 1 (5 marks)
A programmer has intended to write a function that creates an image with square segments
and rectangular segments.
def Q1SE(length):
newImage=makeEmptyPicture(length,length)
i=0
while(i<=(length/4) ):
for y in range(0,length):
for x in range(0,length):
if (x==i) or (x==length-1-i) or (y==i) or (y==length-1-i):
px=getPixel(newImage,x,y)
setColor(px,blue)
i=i+(length/4)
repaint(newImage)

If the above function is run with a length value of 300, can it achieve the intended outcome?
If yes, describe the output, in plain English and draw a small labelled diagram of the
image/output to assist your explanation:

If no, state whether the problem is a syntax error or if it is an error in the logic. Give a short
description in plain English:

Do not write outside the space provided above.

Page 2 of 11

Question 2 (5+2 marks)


def Q2SE(picture):
width=getWidth(picture)
height=getHeight(picture)
x=0
while(x<width):
y=0
while(y<height):
px=getPixel(picture,x,y)
r=getRed(px)-(y*2)
g=getGreen(px)-(y*2)
b=getBlue(px)-(y*2)
clr=makeColor(r,g,b)
setColor(px,clr)
y=y+1
x=x+1
show(picture)

2.1) In plain English, what is the result when the above function is run (assume colour
wrapping is turned off)? Your description must be short and be in plain English. I.e. explain
just the effect to someone who does not understand programming. Do not translate
individual lines of code. Write your answer below:

2.2) Describe oen way in which this function could be made more general. Your answer must
be specific to the given code (i.e. explain to someone who understands programming). There
is no need to re-write the code. Write your answer below:

Page 3 of 11

Question 3 (10 marks)


A JES function is required to produce the following output when j is 3:
3
2
1
1
2
3
Consider the following individual lines of code that are randomly extracted from the
function that produces the above output:
Line number
1
2
3
4
5
6

Code
while(i<=j):
def Q3SE(j):
i=i+1
print i-1
print i
for i in range(j,0,-1):

Complete the sequence below by writing the line numbers from the above table in the
correct order below. The first line has been shown as an example:
Line number (write
below)
2

Code
**********
**********
**********
**********
**********
**********

Note: There should be no repeats and all numbers must be used.

Page 4 of 11

Question 4 (2+11=13 marks)


Consider the following function that produces an image with a particular pattern:
def makePattern():
canvas=makeEmptyPicture(401,301)
#Format: addRect(picture,startX,startY,width,height)
addRect(canvas, 0, 0,400,300)
addRect(canvas, 50, 37,350,262)
addRect(canvas,100, 75,300,225)
addRect(canvas,150,112,250,187)
addRect(canvas,200,150,200,150)
addRect(canvas, 0, 0,200,150)
repaint(canvas)
a. What is the output of this function? Draw a small, approximate diagram to support your

answer and only explain the output in plain English.

Write your answer below:

b. Currently, the function has some limitations: the produced image is of a fixed size, the

number of times the repeating element is repeated is fixed and the final image is only
shown on the screen.
Page 5 of 11

Your task is to overcome the above limitations by re-writing this function in a more
general form. The function must now take three parameter (argument) inputs as follows:
makePattern(width,height,times)
Your code must meet the following requirements:
1. The produced image must be based on the specified dimensions. (If necessary,

assume only dimensions that are valid for the pattern will be given.)
2. The pattern must be correctly produced on images of any size. If and when loops are

necessary, you must only use while loops.


3. The number of repetitions of the repeating element is now specified by the final

parameter of the function.


4. Instead of displaying the result on the screen, your function must return it to the

calling function.
5. There is no need to perform input/data validation on any of the parameters.

Write your answer below (show your code indentation clearly):

Page 6 of 11

Page 7 of 11

Question 5 (15 marks)


Watch Gayans Sample Exam Discussion chat for clarifications on this question.
This question will be related to some of the operations that you had performed in the
assignments. In the final exam, there will be a general description of a task here. Below will
be some specific requirements listed in point form. You will be expected to (but not limited
to):
1. Taking multiple user inputs, some will need validation, others will not
2. Use a given function based on the inputs.
3. Repeat segments of code (until conditions are satisfied) using while loops.
4. Produce final outputs.

Space to write your final answer is below (show your code indentation clearly):

Page 8 of 11

Page 9 of 11

Page 10 of 11

Page 11 of 11

You might also like