COMP100 2017 Main
COMP100 2017 Main
MAIN EXAMINATION
MAY 2017
INSTRUCTIONS:
1. This paper consists of 9 pages (incl. this one).
2. Page 9 contains reference material that you may find useful.
3. Answer all questions.
4. Start the answers to each question on a new page.
5. You may answer the programming questions in pencil.
6. The use of a calculator is NOT allowed.
7. Marks for the programming problems will be awarded for correct solutions and
for efficiency of the code.
University of KwaZulu-Natal, May 2017 Examination: COMP100 P1 2
1.1) (3)
print(12//5//4)
print(12/5/4)
print(12/5.0+4)
print(12//5%4)
print(12%5+4.0)
print(12/5//4)
1.2) (3)
a = False
b = True
print(a or not b)
print(False == a)
print(a == b or b)
print(a != b and not a)
if (234%1 != 0):
print("Not Done")
else:
print("Done")
1.3) (3)
sum = 0
for i in range(4,6):
sum += 3
if sum % 2 == 0:
continue
print(sum)
1.4) (3)
b = 0
for a in range(2, 10, 3):
b += a - 1
print(a,b)
1.5) (3)
def coding(n):
m = 0
while n > 0:
m = 10 * m + n % 5
n = n // 10
return m
print(coding(6345))
University of KwaZulu-Natal, May 2017 Examination: COMP100 P1 3
1.6) (3)
x = 3
y = 2
z = 10
while z >= x + y:
print(z,end=" ")
if z > 5:
z = z - y
else:
break
z = z – x
1.7) (3)
def main():
howMany(24)
print("a pie")
def howMany(num):
what(num)
print("baked in", end=" ")
def what(num):
print(num, "blackbirds", end = " ")
main()
1.8) (3)
import random
number = random.randrange(50,100)
isLucky = False
1.9) (3)
oldstr = "ABC"
newstr=""
for i in range(len(oldstr)):
if i % 2 == 0:
nchar = oldstr[i].upper()
nchar = chr(ord(nchar)+2)
else:
nchar = oldstr[i].lower()
nchar = chr(ord(nchar)+3)
newstr +=nchar
print(newstr)
1.10) (3)
def mysterystr(a, b, mystr ):
newstring = ""
for i in range(a,b,2):
if mystr[i] > 'a':
newstring += "!"
else:
newstring += mystr[i]
return newstring
str1 = "GOODluck"
str2 = mysterystr(0,6,str1)
print(str2)
University of KwaZulu-Natal, May 2017 Examination: COMP100 P1 5
A die (plural dice) is a small cube with each side having a different number of
spots on it, ranging from one to six, thrown and used in games of chance.
The program on page 6 was written to simulate a dice game played between
two people. In each round both players throw the die 3 times. If player 1’s
score (the sum of her three die values) is higher than player 2’s score she wins that round.
If player 2’s score is higher than player 1’s score, then player 2 wins the round. If both
players happen to get the same score then it is considered a tie and there is no winner for
that round. At the start of the game the number of rounds is entered by the user and at
the end of all the rounds, the player who won the most rounds is announced the winner.
Unfortunately, some mischievous person has changed the program so it no longer works
as expected. Identify 5 errors. For each error indicate the line number, what the error is
and how BEST to fix it. DO NOT WRITE OUT THE PROGRAM AGAIN!
Example 2 :
Enter the amount(rands): 12
14 bars can be obtained with 2 unused coupons.
Example 3 :
Enter the amount(rands): 3
3 bars can be obtained with 3 unused coupons.
University of KwaZulu-Natal, June 2017 Examination: COMP100 P1 8
5.2) Write the body of the program that calls the function
findLargerPerfectSquare(lower,upper) (written in 5.1 above) 4 times and
outputs a table similar to the one shown. The user provides both the lower and
upper values. Your program should match the format of the example given below
as closely as possible. (7)
6.2) Write a program that uses your function emphasiseVowel. Your program should
allow the user to enter any number of sentences. It should use the function to
convert and display each sentence in TitleVowelCase. The user indicates that she
wishes to stop entering sentences by typing “#” at the prompt. Your program should
end by displaying the number of sentences processed. (7)
Char ASCII
value
0 48
9 57
A 65
Z 90
a 97
z 122