COMP90059 Introduction to Programming Summer Semester, 2020
Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit
Practice Examination Solution, Summer Semester, 2020
COMP90059 Introduction to Programming
Question One [4 Marks]
Assuming the assignments i = 1, f = 2.0, s = "3" and t = "b4" have been made,
evaluate the following code snippets:
(a) i + f
3.0
(b) str(i) + s "13" |
(c) int(f) + int(s) 5 |
(d) float(t[-1]) * i 4.0 |
Question Two [4 Marks]
Assuming the assignment s="internationalization" has been made, evaluate:
(a) s[1]
‘n’
(b) s[:6] + s[11:13] ‘internal’ |
(c) s[25] IndexError: string index out of range |
(d) s[-1:-10:-3] ‘ nti’ |
What is wrong with this code? How can you fix it?
The issue is with the logic in the test in the if statement — it will run just fine, but the semantics is “run this code if any of the following are true: letter is ‘a’ OR 'e' is a non-empty string OR ... Because 'e' is, of course, a non-empty string, the test will always succeed, irrespective of the value of letter.
Correctly:
if letter == 'a' or letter == 'e' or letter == 'i' or letter == 'o' or letter == 'u' :
or cleaner again:
if letter in 'aeiou' :
[5 Marks]
Given the assignment mylist = [3, ["green","eggs"], True, "computing"], evaluate the following statements and provide:
(a) the value the expression evaluates to
(b) the value of mylist after the code statement has been executed.
Assume that mylist is reassigned to its original value for each sub-question:
mylist[2]
(a) True, (b) [3, ["green","eggs"], True, "computing"] – No change
(b) mylist[1][1][:-1] (a) 'egg', (b) [3, ["green","eggs"], True, "computing"] – No change |
(c) mylist.pop() (a) 'computing', (b) [3, ['green', 'eggs'], True] – last item of mylist is removed |
(d) mylist.append("new item") (a) None, (b) [3, ['green', 'eggs'], True, 'computing', 'new item'] – new item added to the end of the list (e)[str(x) for x in mylist] (b) “[‘g”, (b) [3, ['green', 'eggs'], True, 'computing', 'new item'] – No change |
Rewrite the following code using a for loop, but preserving the remainder of the original code structure:
items = ['eggs', 'spam', 'moreeggs']
for count in range(len(items)):
print(“We need to buy {0}!” .format(items[count]))
This version will incur a penalty as you have not preserved the original code
for item in items:
print(f"We need to buy {item}!")
Question 6 [12 Marks]
Write a function freq_letter(string) that takes a single argument string (a string) and returns a 2-tuple containing the most common letter in string, and how many times it occurs. In case of a tie, it should return the letter that occurred first in the string.
[12 Marks]
Write a function letter_overlap(s1, s2) that takes two string arguments (s1 and s2), and returns the number of unique letters that are present in both strings.
def letter_overlap(s1, s2):
return len(set(s1) & set(s2))
Question 8
What is the output of the following code:
Solution:
10
10
-2
NameError: name 'avg' is not defined
Identify 3 errors in the following code, determine for each whether it is a syntax, run-time or
logic error, and provide a replacement line which corrects the error.
Solution:
• line 1; syntax; def big_ratio(numlist,n):
• line 4; logic; total = greater_n = 0
• line 8; logic; correct indentation for total (outside if block)
Question 10 [6 Marks]
Write a code snippet to produce each of the following exceptions and associated error
messages:
• SyntaxError: invalid syntax
• ZeroDivisionError: division by zero
• KeyError: 1
• IndexError: list index out of range
• TypeError: can only concatenate str (not "int") to str
• ValueError: invalid literal for int() with base 10: ’a’
SyntaxError: 1+ |
ZeroDivisionError: 1/0 |
KeyError: {}[1] |
IndexError: list = []; list[1] |
TypeError: 'a'+ 1 |
ValueError: int('a') |
2022-06-20