15-110 Spring 2011 Quiz 4
15 minutes

1.       Which of the following finds the number of columns in a rectangular 2d list named “a”?

(A) a.cols()          (B) cols(a)                   (C) len(a)                     (D) len(a[0])               (E) None of the above

2.       What is the missing code in this excerpt from a helper function for areAnagrams?

def letterCounts(s):
    counts = [0] * 26
    for ch in s.upper():
        if ((ch >= "A") and (ch <= "Z")):
            counts[MISSING_CODE] += 1
    return counts

(A) ch                    (B) ord(ch)                  (C) ch - “A”                  (D) ord(ch-“A”)         (E) ord(ch) - ord(“A”)

3.  What will this code print?
def f(a):
    result = 0
    for x in a:
        if (x > result):
            result = x
    return result
print f([2,3,9,8,3])

(A) 2                      (B) 3                               (C) 9                               (D) 8                              (E) None of the above

4.  What will this code print?
def f(a,b):
    result = 0
    for i in range(len(a)):
        if (i < len(b)):
            result += a[i] - b[i]
    return result
print f([5,7,9,11], [1,4])

(A) [4, 3]              (B) 1                               (C) 5                               (D) 7                              (E) None of the above

5.  What is the median of this list:  [2, 6, 4, 8]?

(A) 2                      (B) 4                               (C) 6                               (D) 8                              (E) None of the above

6.       In our Automated Readability Index (reading level) solution, charCount(“ur2nice!”) returns what value?

(A) 6                      (B) 7                               (C) 8                               (D) 10                            (E) None of the above

7.       What does the Google ngram viewer demonstrate?  Choose the most accurate answer.
(A) How language evolves over time.
(B) How Google searches evolve over time.
(C) How word frequencies evolve over time.
(D) How languages borrow terms from each other over time.
(E) How misspellings become part of a language over time.

8.       Why did we use s.strip() in our solution to makeSubtable?
(A) To eliminate the non-alphabetic characters from the string.
(B) To count the leading spaces in the string.
(C) To make another string without the leading spaces in the original string.
(D) To make another string composed of the leading spaces in the original string.
(E) None of the above.

9.  What will this code print?
def f(a):
    result = []
    for row in range(len(a)):
        x = 0
        for col in range(len(a[0])):
            x += a[row][col]
        result += [x]
    return result
print f([[1,2,3],[4,5,6]])

(A) 21                    (B) [5, 7, 9]                  (C) [6, 15]                    (D) [21]                         (E) None of the above

10. What will this code print?
def f(a):
    a.sort()
    a = [2] + a
    return a[0]
a = [5, 3, 4]
print f(a), a

(A) [2, 3, 4, 5]     (B) 2 [3, 4, 5]               (C)  [2] [3, 4, 5]          (D) 2 [5, 3, 4]              (E) None of the above

11.   Bonus/Optional [4 pts]:  Name the recently deposed leader of Egypt and the Nobel Peace Prize winning activist who helped topple him.