0% found this document useful (0 votes)
10 views9 pages

COMP100 2017 Main

This document is an examination paper for the Introduction to Computer Science course (COMP100P1) at the University of KwaZulu-Natal, dated May 2017. It includes instructions for the exam, various programming questions, and tasks related to loops, chocolate bars, perfect squares, and vowel emphasis. The exam consists of 9 pages, and students are required to answer all questions within a 3-hour timeframe.
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)
10 views9 pages

COMP100 2017 Main

This document is an examination paper for the Introduction to Computer Science course (COMP100P1) at the University of KwaZulu-Natal, dated May 2017. It includes instructions for the exam, various programming questions, and tasks related to loops, chocolate bars, perfect squares, and vowel emphasis. The exam consists of 9 pages, and students are required to answer all questions within a 3-hour timeframe.
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
You are on page 1/ 9

UNIVERSITY OF KWAZULU-NATAL

SCHOOL OF MATHEMATICS, STATISTICS


& COMPUTER SCIENCE

MAIN EXAMINATION

MAY 2017

COURSE AND CODE


Introduction to Computer Science COMP100P1

DURATION: 3 Hours TOTAL MARKS: 100

INTERNAL EXAMINERS: Rosanne Els and Mzomuhle Nkosi


INTERNAL MODERATOR: Anban Pillay

THIS EXAM CONSISTS OF 9 PAGES INCLUDING THIS ONE.


PLEASE ENSURE THAT YOU HAVE ALL PAGES.

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

Question 1 - Program Output [30]


Give the exact output of each of the following programs. If there is no output due to
error(s) then explain the error briefly.

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

for i in range(2, 3):


if number >=50:
isLucky = True
elif (number < 100):
isLucky = False
if (number < 100):
print("The number is Lucky-",isLucky,end="")
print("!")
University of KwaZulu-Natal, May 2017 Examination: COMP100 P1 4

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

Question 2 - Find the errors [10]

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.

See examples of expected output of the program below.


Example 1: Example 2:
How many rounds would you like to play?3 How many rounds would you like to play?2
---Round 1 --- ---Round 1 ---
Player 1 Player 1
Throw 1 - 3 Throw 1 - 4
Throw 2 - 2 Throw 2 - 4
Throw 3 - 5 Throw 3 - 2
Player 2 Player 2
Throw 1 - 4 Throw 1 - 1
Throw 2 - 6 Throw 2 - 5
Throw 3 - 6 Throw 3 - 6
Player 2 wins by 6 (16 vs 10 points) Player 2 wins by 2 (12 vs 10 points)
---Round 2 --- ---Round 2 ---
Player 1 Player 1
Throw 1 - 6 Throw 1 - 1
Throw 2 - 3 Throw 2 - 4
Throw 3 - 4 Throw 3 - 4
Player 2 Player 2
Throw 1 - 5 Throw 1 - 1
Throw 2 - 5 Throw 2 - 6
Throw 3 - 3 Throw 3 - 6
Tie - both players had a score of 13 Player 2 wins by 4 (13 vs 9 points)
---Round 3 --- ---Overall winner---
Player 1 Player 2 wins ( 2 vs 0 )
Throw 1 - 3
Throw 2 - 5
Throw 3 - 3
Player 2
Throw 1 - 4
Throw 2 - 4
Throw 3 - 1
Player 1 wins by 2 (11 vs 9 points)
---Overall winner---
Tie ( 1 vs 1 )

Question continues on next page:


University of KwaZulu-Natal, June 2017 Examination: COMP100 P1 6

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!

import random #line 1


round1 = 0 #line 2
round2 = 0 #line 3
numrounds = int(input("How many rounds would you like to play?"))
#line 4
for rounds in range(numrounds): #line 5
print("---Round ",rounds+1,"---") #line 6
score=0 #line 7
for player in [1,2]: #line 8
print("\tPlayer",player) #line 9
for throw in range(3): #line 10
dice = random.randrange(1,6) #line 11
print("\t\tThrow",throw+1,"-",dice) #line 12
score = score + dice #line 13
if (player ==1): #line 14
score1 = score #line 15
else: #line 16
score2 = score #line 17
if (score1>score2): #line 18
diffscore = score1-score2 #line 19
msg = "Player 1 wins by "+str(diffscore)+" ("+str(score1)
msg += " vs "+str(score2)+" points)" #line 21
round1 +=1 #line 22
elif (score1 < score2): #line 23
diffscore = score2-score1 #line 24
msg = "Player 2 wins by "+str(diffscore)+" ("+str(score2)
msg += " vs "+str(score1)+" points)" #line 26
else: #line 27
msg = "Tie - both players had a score of "+score1 #line 28
print("---Overall winner---") #line 29
if (round1 > round2): #line 30
print("Player 1 wins (",round1,"vs",round2,")") #line 31
elif (round1 < round2): #line 32
print("Player 2 wins (",round2,"vs",round1,")") #line 33
else: #line 34
print("Tie (",round2,"vs",round1,")") #line 35
University of KwaZulu-Natal, June 2017 Examination: COMP100 P1 7

Question 3 - Loops [10]


Consider the following code:
i = 2
while i > 0:
j = 0
while j < 4:
if j % 3 == 0:
print("|",end="")
else:
print("*",end="")
j += 1
print()
i −= 1

3.1) Give the output of the code. (2)


3.2) How many times is the if condition tested? (2)
3.3) How many times does the outer while loop run? (2)
3.4) Rewrite the above code using a nested for loop structure (4)

Question 4 –Chocolate Bars [10]


Chocolate bars can be purchased from a vending machine at a cost of R1 each. For each
bar purchased, the machine gives you one coupon. You can use 6 coupons to buy an
additional chocolate bar. (Note that you still get one coupon for each bar bought using
coupons.)
For example,
- With R6, 7 chocolate bars can be obtained after purchasing 6 bars and then
redeeming 6 coupons for an additional bar. This would leave the user with one
extra coupon.
- For R11, 13 chocolate bars can be obtained and the user will still have one coupon
left.
- For R12, 14 chocolate bars can be obtained and there will be two unused coupons.
Write a program to determine how many chocolate bars can be obtained from the vending
machine if you spend N rands and always use your coupons to buy additional bars if you
have enough coupons. Also determine how many unused coupons will be left over.
Given the same input, your program should match as closely as possible the examples
below.
Example 1 :
Enter the amount(rands): 6
7 bars can be obtained with 1 unused coupon.

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

Question 5 – Larger Perfect Square [20]


5.1) findLargerPerfectSquare(lower, upper)is a function that accepts two
integer parameters and generates a random integer number between the given
parameters. It then finds the first even perfect square that is larger than the
randomly generated number. The function displays two values: the randomly
generated number and the found even perfect square. Write the function
findLargerPerfectSquare(lower, upper) (13)

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)

Example output (with input shown in bold)


Lower bound? 5
Upper bound? 50

Random Num Output


29 36
43 64
15 16
16 36

Question 6: Emphasise Vowels Case [20]


6.1) Write a function, emphasiseVowel(inStr) that converts and returns the input
string in TitleVowelCase. A string in TitleVowelCase has all vowels and the first
character of every word in uppercase. For e.g., if inStr is “How is the
Exam?”, the function returns “HOw Is ThE ExAm?” (13)

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)

Below is an example of expected output of the program. Your program should


match the format of the example given below as closely as possible.

Example output (with input shown in bold)


Enter your sentence: Hello
HEllO
Enter your sentence: The cat jumped over the mat
ThE CAt JUmpEd OvEr ThE MAt
Enter your sentence: How is the Exam?
HOw Is ThE ExAm?
Enter your sentence: #
3 strings processed
University of KwaZulu-Natal, June 2017 Examination: COMP100 P1 9

Appendix A: Python Reference


Useful functions in the math module:

Useful functions in the string module:

Operator precedence table: ASCII codes of some characters:

Char ASCII
value
0 48
9 57
A 65
Z 90
a 97
z 122

You might also like