MCD4710 Week3 Sample Test Sol
MCD4710 Week3 Sample Test Sol
Structure of Test
Section Number of Questions Total Possible Actual Marks
to be answered Marks
Output of simple Python code
Total marks 24
During a test, you must not have in your possession a book, notes, paper, calculator, pencil case, mobile phone
or other material/item which has not been authorised for the test or specifically permitted as noted below. Any
material or item on your desk, chair or person will be deemed to be in your possession. You are reminded that
possession of unauthorised materials in a test is a discipline offence under Monash College regulations.
AUTHORISED MATERIALS
INSTRUCTIONS TO CANDIDATES
Show all working - diagrams are a valuable part of working and should be large enough, and
clearly labelled. Marks may be lost if working is not included.
Write the final answer in the space provided.
Do not use pencil.
Write clearly. Marks cannot be given if your handwriting cannot be read.
THIS ENTIRE TEST PAPER MUST BE HANDED IN AT THE END OF THE TEST.
DO NOT OPEN THIS BOOKLET UNTIL INSTRUCTED TO DO SO.
Important Information
All Python code you write for this test must satisfy the following requirements:
Syntax should satisfy Python 3 requirements
Use syntax, modules, structures and constructs presented in lectures.
Avoid inbuilt python functions or libraries that make your work signicantly easier to
write (for example the inbuilt min method when you are asked to nd the smallest
item in a list)
Write down any assumptions you make.
0
Question 1: [1 marks]
What will be printed by the following
aList = [1 ,2 ,3]
a L i s t *= 2
print ( a L i s t )
[1,2,3,1,2,3]
Question 2: [1 marks]
What will be printed by the following
b S t r i n g = " abc123 "
print ( b S t r i n g [ 1 : 4 ] )
bc1
Question 3: [1 marks]
What will be printed by the following
cList = [4 ,2 ,0]
for item in c L i s t :
print ( str ( item ) * item )
4444
22
Question 4: [2 marks]
What will be printed by the following
d L i s t = [ " apple " , "banana" , " orange " , " k i w i f r u i t " ]
e L i s t = [ "APPLE" , "ORANGE" ]
for d in d L i s t :
i f d . upper ( ) in e L i s t :
print ( d )
5
apple
orange
0
Question 5: [1 marks]
What will be printed by the following
def fFunc ( f S t r i n g ) :
return f S t r i n g+f S t r i n g [ : : − 1 ]
print ( fFunc ( " one " ) )
oneeno
Question 6: [2 marks]
What will be printed by the following
g L i s t = [ "a" , "b" , False , 3 ]
h L i s t = [ 3 , True , "b" , "A" ]
for i in range ( len ( g L i s t ) ) :
i f not g L i s t [ i ]== h L i s t [ − i − 1]:
print ( str ( i )+" : "+str ( g L i s t [ i ] ) )
0:a
2:False
3
Question 7: [6 marks]
Consider the task of unlocking your front door and entering your house. Is this an
algorithm? Explain why/why not. Ensure you refer to each of the attributes of an
algorithm individually.
sequence - this task would be structured with each step in sequence. First you
find the key THEN you put it into the lock, etc.
Terminates - the task terminates as the key turns only so far before unlocking
Precise - unambiguous - this will depend on how the algorithm is written (ex. do
you turn clockwise or counter-clockwise?). It's possible to phrase this in a way
which is de˝nite.
Basic - sufficiently simple - again this depends on how it is written but it is
possible. You may need to specify precise angles to rotate the key by but it's
possible to do
input - one could argue that the key is the input into this task, fortunately
inputs are not required to be an algorithm
output - one could make an argument about the output of this task, they could
argue that the output is an unlocked door or equally they could argue that this
task is a procedure not an algorithm as there isn't a result passed on.
each point above is worth 1 mark. Students may answer dierently and are awarded
marks if their answer makes sense. They must still address each point however (ex.
they may believe it non-nite, if their argument makes sense they would get the
marks for niteness)
6
Question 8: [5 marks]
For this task, you should produce a python program to solve the following problem:
You have a string of letters (unique and lower case) and wish to determine how out of
order it is. For each letter, determine how many letters after this should occur before it.
You should print the sum of these counts for each letter.
For example, given the strings "abcdef", "zyda", and "afcbd", your program should
output:
0
6
4
"abcdef" -> "a" has nothing smaller than it to the right, the same is true of "b", "c",
"d", "e". "f" is on the far right so has nothing to the right of it
"zyda" -> 3+2+1+0 = 6 ("y","d" and "a" are all less than "z", "d" and "a" are less
than "y", etc.)
"afcbd" -> 0+3+1+0+0 = 4 ("c","b","d" are less than "f" and "b" is less than "c")
You may assume the string to consider is dened in a variable called inputString
count=0
for l e t P o s in range ( len ( i n p u t S t r i n g ) ) :
l e t t e r = inputString [ letPos ]
for o t h e r L e t t e r in i n p u t S t r i n g [ l e t P o s + 1 : ] :
if otherLetter < l e t t e r :
count+=1
print ( count )
5
Question 9: [5 marks]
Write up a python program which takes as input from the user a positive integer and
determines the factors of that integer.
For instance: