國立暨南國際大學 105 學年度 模擬考試試卷

科目名稱:基礎程式設計 開課系所:資訊工程 學系 考試日期 2016.12.30
系所別:
年級:
學號:
姓名:
考試時間 08:10-10:00

1
2
3
4
5  
6
7  
8
9  
10

  1. (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)


  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).

    # 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") )


  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).

    # 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)


  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).

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

  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).

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


  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).

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

  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).

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


  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).

    #  __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='*')
  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).

    # 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)
  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).
    # 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] )