國立暨南國際大學 104 學年度第一學期 期末考試卷

科目名稱:計算機概 論 開課系所:資訊工程 學系 考試日期 2016.1.12
系所別:
年級:
學號:
姓名:
考試時間 14:20-17:00
1
2
3
4
5  
6
7  
8
9  
10
11
12

13

14

15


  1. (10%) Where did Dr. Randy Pausch receive his bachelor's degree?
    1. Carnegie Mellon University
    2. Brown University
    3. University of Southern California
    4. Ohio University


  2. (10%) Determine whether the following code has syntax errors or not.  If it is correct, predict its output.  If it is incorrect, point out the mistake(s).

    # Simultaneous Assignment (P.40)
    a, b, c, d, e, f, g = 1, 2, 3, 4, 5, 6, 7
    for i in range(7):
        a, b, c, d, e, f, g = a, c, e, g, b, d, f
    print(a, b, c, d, e, f, g)


  3. (10%) Determine whether the following code has syntax errors or not.  If it is correct, predict its output.  If it is incorrect, point out the mistake(s).

    # Merge Sort (P.445)
    def showList(nums):
        for x in nums:
            if x == nums[-1]:
                print(x, end='\n')
            else:
                print(x, end='\t')

    def merge(lst1, lst2, lst3):
        i1, i2, i3 = 0, 0, 0  # all start at the front
        n1, n2 = len(lst1), len(lst2)

        while i1 < n1 and i2 < n2:
            if lst1[i1] < lst2[i2]: # top of lst1 is smaller
                lst3[i3] = lst1[i1] #  copy it into current spot in lst3
                i1 = i1 + 1
            else:                   # top of lst2 is smaller
                lst3[i3] = lst2[i2] #  copy itinto current spot in lst3
                i2 = i2 + 1
            i3 = i3 + 1             # item added to lst3, update position

        while i1 < n1:
            lst3[i3] = lst1[i1]
            i1 = i1 + 1
            i3 = i3 + 1

        while i2 < n2:
            lst3[i3] = lst2[i2]
            i2 = i2 + 1
            i3 = i3 + 1

    def mergeSort(nums):
        n = len(nums)
        if n > 1:
            m = n//2
            nums1, nums2 = nums[:m], nums[m:]
            mergeSort(nums1)
            mergeSort(nums2)
            merge(nums1, nums2, nums)
            showList(nums)

    import random
    a = [9, 3, 47, 18, 97, 74, 82, 90, 24]
    mergeSort(a)


  4. (10%) Determine whether the following code has syntax errors or not.  If it is correct, predict its output.  If it is incorrect, point out the mistake(s).

    # Recursive Functions (P.437)
    def anagrams(s):
        if s == "":
            return [s]
        else:
            ans = []
            for w in anagrams(s[1:]):
                for pos in range(len(w)+1):
                    ans.append(w[:pos]+s[0]+w[pos:])
            return ans

    print( anagrams("abcd") )


  5. (10%) Determine whether the following code has syntax errors or not.  If it is correct, predict its output.  If it is incorrect, point out the mistake(s).

    # Nested loop (P.244)  and break (P.253)
    sum = 0
    i = 1
    while i < 6:
        j = i
        while j < 6:
            sum = sum + j
            if i == j: break
            j = j + 1
        i = i + 1
    print(sum)


  6. (10%) Determine whether the following code has syntax errors or not.  If it is correct, predict its output.  If it is incorrect, point out the mistake(s).

    # print()  (P.34)
    for i in range(2):
        for j in range(3):
            print('*', end='')
            print()

  7. (10%) Determine whether the following code has syntax errors or not.  If it is correct, predict its output.  If it is incorrect, point out the mistake(s).

    # Definite Loop  (P.43)
    for i in range(10, 2, -3):
        print(i)
    print(i)


  8. (10%) Determine whether the following code has syntax errors or not.  If it is correct, predict its output.  If it is incorrect, point out the mistake(s).

    #
    List slicing and concatenation (P.343)
    s1 = [2,1,4,3]
    s2=['c','a','b']
    print(s1[:2] + s2[-1:])

  9. (10%) Determine whether the following code has syntax errors or not.  If it is correct, predict its output.  If it is incorrect, point out the mistake(s).

    # String concatenation (P.124)
    N = 5
    s = " "*N + "*"*N
    for i in range(1, N):
        print( s[ i : i+N ] )


  10. (10%) Determine whether the following code has syntax errors or not.  If it is correct, predict its output.  If it is incorrect, point out the mistake(s).
    # String Formatting (P.147)
    N = 5
    s = " "*N + "{0}"
    for i in range(N):
        print( s[i+1:].format( "*"*(2*i+1) ) )

  11. (10%) Determine whether the following code has syntax errors or not.  If it is correct, predict its output.  If it is incorrect, point out the mistake(s).

    #  __str__()    (P.336)
    class Char():
        def __init__(self, c):
            self.character = c
        def __str__(self):
            return self.character

    a = Char('*')
    print(a, a, a, sep='*')

  12. (10%) Determine whether the following code has syntax errors or not.  If it is correct, predict its output.  If it is incorrect, point out the mistake(s).

    # Data Processing with Class (P.311)
    class Time:
        '
    HSR-NTbus Waiting Time'
        def __init__(self, s):
            h, m= s.split(':')
            self.hour, self.minute = int(h), int(m)
        def __str__(self):
            return "{0:02}:{1:02}".format(self.hour, self.minute)
        def subtract(self, t):
            m = (self.hour*60 + self.minute) - (t.hour*60 + t.minute)
            # return Time("{0:02}:{1:02}".format(m//60, m % 60) )
            return m

    HSR_arrival = ["13:04", "13:13", "13:37",
            "14:04", "14:13", "14:37", "14:49",
            "15:04", "15:13", "15:37", "15:49",
            "16:04", "16:13", "16:20", "16:37", "16:49"]
    NTbus_departure = ["12:55", "13:55", "14:55", "15:55", "16:55"]
    iHSR, iNTbus = 0, 0
    s = "{0} {1}, {2} {3}, waiting {4:2} minutes."
    for iHSR in range(len(HSR_arrival)):
        while HSR_arrival[iHSR] > NTbus_departure[iNTbus]:
            iNTbus = iNTbus + 1
        print(s.format("HSR", HSR_arrival[iHSR],
                    "NTbus", NTbus_departure[iNTbus],
                    Time(NTbus_departure[iNTbus]).subtract(
                    Time(HSR_arrival[iHSR]) ) ) )

  13. (10%) Determine whether the following code has syntax errors or not.  If it is correct, predict its output.  If it is incorrect, point out the mistake(s).

    # Define a class for Polynomial (P.305)
    class Polynomial:
    def __init__(self, aList):
    self.coefficients = aList
    def __str__(self):
    def needPlus(coefficient, order):
    if order == 0:
    necessity = False
    else:
    if coefficient > 0:
    necessity = True
    else:
    necessity = False
    return necessity

    result = ""
    cList = self.coefficients
    for i in range( len(cList) ):
    a = cList[i] # coefficient

    if i == 0:
    expo = ''
    elif i == 1:
    expo = 'x'
    else:
    expo = "x**" + str(i)

    if a != 0:
    if needPlus(a, i):
    if a == 1: # i must be gt 0, because needPlus
    result = result + "+" + expo
    else:
    result = result + "+" + str( a ) + expo
    else:
    if a == -1 and i > 0:
    result = result + '-' + expo
    else:
    result = result + str(a) + expo

    return result


    p1 = Polynomial([-1, -1, 3])
    p2 = Polynomial([1, -3, 0, -1])
    print(p1)
    print(p2)


  14. (10%) Determine whether the following code has syntax errors or not.  If it is correct, predict its output.  If it is incorrect, point out the mistake(s).
    # Sorting a List (P.354)
    class Student:
    def __init__(self, i, n, s):
    self.id, self.name, self.score = i, n, s
    def __str__(self):
    return str(self.id) + ' ' + self.name + ' ' + str(self.score)
    def getScore(self):
    return self.id

    def getScore(self):
    return self.score

    students = [Student(580, "Alice", 35),
    Student(191, "Bob", 75),
    Student(641, "Charlie", 76),
    Student(813, "Dennis", 27),
    Student(809, "Emily", 79),
    Student(568, "Fiona", 65) ]
    students.sort(key=Student.getScore)
    print( students[2] )
  15. (10%) Determine whether the following code has syntax errors or not.  If it is correct, predict its output.  If it is incorrect, point out the mistake(s).
    # Key Function for Sorting (P.353)
    class Student:
    def __init__(self, i, n, s):
    self.id, self.name, self.score = i, n, s
    def __str__(self):
    return str(self.id) + ' ' + self.name + ' ' + str(self.score)

    def score(aStudent):
    return 100 - aStudent.score

    students = [Student(580, "Alice", 35),
    Student(191, "Bob", 75),
    Student(641, "Charlie", 76),
    Student(813, "Dennis", 27),
    Student(809, "Emily", 79),
    Student(568, "Fiona", 65) ]
    students.sort(key=score)
    print( students[4] )