COMP90059 Introduction to Programming Summer Semester, 2019
Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit
COMP90059 Introduction to Programming
Summer Semester, 2019
Sample Exam Questions: Suggested Solutions
Instructions to students: All questions should be answered by writing a brief response or explanation. These questions provide a sample of exam style questions for COMP90059. The structure and number of questions is not indicative of the exam.
Q1 (5 × 1 = 5 marks): Evaluate each of the following expressions; what is the output in each case?
1. ‘tom’ not in ‘optimist’ A: True
2. (‘{} was a door number, {} was one too’.format(11, 22)) A: ‘11 was a door number, 22 was one too’
3. ‘COMP90059’.find(‘95’[::-1])
A: 7
4. ‘testing 123’.index(‘1’)
A: 8
5. sorted([x for x in range(4, -4, -2)], reverse=True)[2:] A: [0, -2]
Q2 (4 marks): Consider the following piece of code. Briefly explain what this code does? If the variable s = ‘student 23’, what is the output produced by this code?
s = input(‘Input a string: ’)
d = 0
l = 0
for c in s:
if c .isdigit():
d = d + 1
elif c .isalpha():
l = l + 1
print(‘l:’, l)
print(‘d:’, d)
A: It counts how many digits and how many alphabetic characters are in the string, into the int variables d and l respectively. Sample output is:
Input a string: student 23
l: 7
d: 2
Q3 (6 marks): Rewrite the following program code, replacing the two for loops with while loops, while preserving the remainder of the original code structure and output:
n = 10
sum = 0
for i in range(0, n):
for j in range(0, n):
print(‘{}{} ’.format(i, j), end=‘’)
sum = sum + i + j
print(sum)
print(‘End of program’)
A: Change
for var in range(start , stop ):
to
var = start
while var < stop :
...
var += 1
as follows:
n = 10
sum = 0
i = 0
j = 0
while i < 10:
while j < 10:
print("{}{} " .format(i, j), end="")
sum = sum + i + j
j= j + 1
print(sum)
i = i + 1
j = 0
print("End of program")
Q4 (6 marks): The following is intended to print out the number of letters which occur a single time (“hapax” letters) in the given document. Provide a single statement to insert into each of the numbered boxes to complete the code. Note that your code should run at the indentation level indicated for each box.
A:
1 - char_dict = {}
2 - if char_dict[char] == 1:
3 - return len(hapax_letters)
4 - (hapax_letters(text))
B:
1 - char_dict = dict()
2 - if char_dict[char] == 1:
3 - return hapax_letters
4 - (len(hapax_letters(text)))
Q5 (6 Marks): For any element in a list, we define it as frequent element, if it appears more than once in the list. For example, common elements in list [1, 2, 3, 1, 3, 3] should be 1 and 3. Write a function FrequentElements(A) which takes a list A as input, and returns a list containing all frequent elements in the input. Note that the returned list shouldn’t have repeated elements.
A:
def common_elements(lstA):
counts = {}
for num in lstA:
if num in counts:
counts[num] += 1
else:
counts[num] = 1
common = []
for k, v in counts .items():
if v > 1:
common .append(k)
return common
B:
from collections import defaultdict as dd
def common_items(lstA):
counts = dd(int)
for num in lstA:
counts[num] += 1
return [k for k, v in counts .items() if v > 1]
2022-06-20