CS1010E SECTION I. SHORT QUESTIONS Write down the output of the following programs. 1. print(int(-2.9)) 2. def f(x): return x**2 print(f(2)) 3. def f(s, ch): count = 0 for val in s: if val == ch: count = count + 1 return count print(f('abbacadaba', 'a')) 4. for i in range(2, 100): if i%2 == 0 and i%3 == 0: break print(i) 5. lst = [1, 2, 3] del lst[1] lst.append(4) print(sum(lst)) 6. t1 = (1, 2, 3, 4, 5) t2 = (6, 2, 4, 8) def f(): - 1 of 11 - CS1010E s = set() for val in t2: if val not in t1: s.add(val) print(len(s)) f() 7. def f(n, d): result, weight = 0, 1 while n > 0: if n%10 != d: result = result + n%10*weight weight = weight * 10 n = n // 10 return result print(f(123123, 2)) 8. def f(t1, t2): idx_t1, idx_t2 = 0, 0 while idx_t1 < len(t1) and idx_t2 < len(t2): if t1[idx_t1] > t2[idx_t2]: idx_t1 = idx_t1 + 1 elif t1[idx_t1] < t2[idx_t2]: idx_t2 = idx_t2 + 1 else: return t1[idx_t1] return -1 t1, t2 = (8, 7, 5, 3, 1), (10, 9, 8) print(f(t1, t2)) - 2 of 11 - CS1010E 9. Please omit opening and closing quotation marks in your answer. fruit = ( (2, 'apple'), (0, 'orange'), (4, 'banana'), (-1, 'pear'), (3, 'mango'), (-2, 'kiwi') ) p = 2 while p > 0: p = fruit[p][0] print(fruit[p][1]) 10. def f(s): d = {} for ch in s: if ch in d: d[ch] = d[ch] + 1 else: d[ch] = 1 count = 0 for key in d: if d[key] > 1: count = count + 1 return count print(f('I_did_it')) 11. x = 100 for a in range(5): for b in range(20): if a%2 == 0: x = x + a else: x = x - a print(x) - 3 of 11 - CS1010E SECTION II. Multiple Choice Questions (MCQs) Each MCQ has one correct answer. 12. Which of the following expressions will be evaluated to True? A. B. C. D. E. (4) == (4,) [1, 2] == [2, 1] {1, 2} == {2, 1} 7//2 == 3.5 None of the above 13. What is printed by the following program? x = 5 def f(x): x = 2 return x+1 print(x, f(x)) A. B. C. D. E. 2 2 3 5 5 2 3 3 2 3 14. George decides what to eat for breakfast every morning by throwing a six-sided dice three times. He follows the following rules. If the first throw is less than the second throw, then he will eat apple for breakfast. Otherwise, if the first throw is not more than the third throw, then George will eat cereal for breakfast. Otherwise, he will eat a slice of bread. Which of the following sequences of throws would lead to George eating bread? A. B. C. D. E. 443 444 445 453 None of the above - 4 of 11 - CS1010E 15. Consider the following dictionary d. d = { (0,): True, 'kiwi': 0, True: False } Which of the following expressions is evaluated to True? A. B. C. D. E. d['kiwi'] d[True] d[0] False in d None of the above 16. What can be said about the following program? x, y = 2, 1 while x < 50: x = x ** 2 y = y + 1 print(x == 256) A. B. C. D. E. In the first line of the program, the value of y is 2. The value of x after the loop is 64. The value of y after the loop is 4. The program will print true on the screen. None of the above 17. Suppose a and n are positive integers, what does the following function return? def f(a, n): for i in range(n): a = a + a return a A. B. C. D. E. 𝑎𝑎𝑛𝑛 𝑎𝑎2∗𝑛𝑛 2 ∗ 𝑎𝑎𝑛𝑛 2𝑛𝑛 ∗ 𝑎𝑎 None of the above - 5 of 11 - CS1010E 18. Assuming that n is a positive integer less than 100, consider the following three functions, f1(), f2() and f3(). def f1(n): # assume 0 < n < 100 total = 0 for a in range(n+1): total = total + a return total def f2(n): # assume 0 < n < 100 total = 0 while n > 0: total = total + n n = n - 1 return total def f3(n): # assume 0 < n < 100 return n*(n+1)//2 Which of the following statements is TRUE? A. B. C. D. E. Function calls f1(1) and f2(1) return different values. Function calls f1(2) and f3(2) return different values. Function calls f2(3) and f3(3) return different values. Given the same n, all three functions return the same value. None of the above 19. What is printed by the following program? lt = [0, 1, 2, 3, 4] for a in range(1, 5): for b in range(a): lt[a] = lt[a] + lt[b] print(lt) A. B. C. D. E. [0, 0, 0, 0, 0] [0, 1, 3, 7, 15] [0, 1, 3, 6, 10] [10, 31, 93, 278, 832] None of the above - 6 of 11 - CS1010E 20. The function f() has the following input-output relationship: n f(n) 222 111 521 654 0 111 101 10 The function f(n) is given below but line 5 is partially hidden from you (hidden part is shown as XXXXXXXXX). def f(n): b, k = 0, 10 while n > 0: d = n % 10 b = XXXXXXXXX: n = n // 10 return b # line 5 Which of the following expressions can be substituted into XXXXXXXXX to produce the desired output? A. B. C. D. E. b*k + d%2 b*k + 1 - d%2 b + d%2*k b + (1-d%2)*k None of the above - 7 of 11 - CS1010E 21. The function p3_p5(n) prints out, in ascending order, all the integers in the range [3, n] (both ends inclusive) that are power of 3 or 5. You may assume that n > 3. For example, function call p3_p5(125) will print out: 3 5 9 25 27 81 125 In the above example, 3, 9, 27 and 81 are power of 3, while 5, 25 and 125 are power of 5. The function p3_p5(n) is given below but line 3 is partially hidden from you (hidden part is shown as XXXXXXXXX). def p3_p5(n): p3, p5 = 3, 5 while XXXXXXXXX: if p3 < p5: print(p3) p3 = p3 * 3 else: print(p5) p5 = p5 * 5 # line 3 Which of the following expressions can be substituted into the hidden part of line 3 to produce the desired result? A. B. C. D. E. p3 < n or p5 < n p3 <= n or p5 <= n p3 < n and p5 < n p3 <= n and p5 <= n None of the above - 8 of 11 - CS1010E 22. A polynomial 𝑝𝑝(𝑥𝑥) = 𝑎𝑎𝑥𝑥 5 + 𝑏𝑏𝑥𝑥 4 + 𝑐𝑐𝑥𝑥 3 + 𝑑𝑑𝑥𝑥 2 + 𝑒𝑒𝑒𝑒 + 𝑓𝑓 has its six coefficients 𝑎𝑎, 𝑏𝑏, 𝑐𝑐, 𝑑𝑑, 𝑒𝑒 and 𝑓𝑓 stored in a tuple poly where poly[0] is 𝑎𝑎, poly[1] is 𝑏𝑏, poly[2] is 𝑐𝑐, poly[3] is 𝑑𝑑, poly[4] is 𝑒𝑒 and poly[5] is 𝑓𝑓. Assuming that 𝑥𝑥 and the coefficients are integers, which of the following functions correctly returns 𝑝𝑝(𝑥𝑥) for a given value of 𝑥𝑥? A. def compute(poly, x): ans = 0 for i in range(5): ans = (ans + poly[i]) * x return ans + poly[5] B. def compute(poly, x): ans = 0 for i in range(5): ans = ans + poly[i] * x return ans + poly[5] C. def compute(poly, x): ans = 0 for i in range(6): ans = (ans + poly[i]) * x return ans D. def compute(poly, x): ans = 0 for i in range(6): ans = ans + poly[i] * x return ans E. None of the above - 9 of 11 - CS1010E 23. A palindrome is a word that can be read the same way in either direction. Examples of palindrome include: 'I' • 'DID' • 'NOON' • 'ROTATOR' • The function is_palindrome(s) returns True if parameter s is a palindrome, or False otherwise. You may assume that s is a string contains upper-case alphabet (i.e. upper-case English letters) only. Which of the following are the correct implementations of the function is_palindrome(s)? (i) def is_palindrome(s): string = '' # begin with an empty string for ch in s: string = ch + string return s == string (ii) def is_palindrome(s): return s == s[::-1] (iii) def is_palindrome(s): for i in range(len(s)//2): if s[i] != s[len(s)-i-1]: return False return True (iv) def is_palindrome(s): left, right = 0, -1 while left < len(s)//2: if s[left] != s[right]: return False left = left + 1 right = right - 1 return True - 10 of 11 - CS1010E A. B. C. D. E. (i) and (ii) only (ii) and (iii) only (iii) and (iv) only (i), (ii), (iii) and (iv) None of the above === END OF PAPER === - 11 of 11 -