國立暨南國際大學 102 學年度第一學期小考試卷

科目名稱:計算機概 論 開課系所:資訊工程 學系 考試日期 2013.12.31
系所別:
年級:
學號:
姓名:
考試時間 14:10-14:20
  1. (10%) Determine whether the following code is correct or not.  If it is correct, predict its output.  If it is incorrect, point out the mistake(s).
    # Defining Classes (P.333)

    class Bozo:

        def __init__(self, value):
            print("Creating a Bozo from:", value)
            self.value = 2 * value

        def clown(self, x):
            print("Clowning:", x)
            print(x * self.value)
            return x + self.value

    def main():
        print("Clowning around now.")
        c1 = Bozo(3)
        c2 = Bozo(4)
        print( c1.clown(3) )
        print( c2.clown(c1.clown(2)) )

    main()


  2. (10%) Determine whether the following code is correct or not.  If it is correct, predict its output.  If it is incorrect, point out the mistake(s).
    # Useful List-specific Methods (P.345)

    aList = [3, 1, 4, 1, 5, 9]
    aList.insert(5, 8)
    aList.insert(7, 8)
    print(aList)



  3. (10%) Determine whether the following code is correct or not.  If it is correct, predict its output.  If it is incorrect, point out the mistake(s).
    # Sorting a List (P.351--355)

    class MyPoint:

        def __init__(self, x, y):
            self.x = x
            self.y = y
        def __str__(self):
            return '(' + str(self.x) + ',' + str(self.y) + ')'

        def descX(self):
            return -self.x

    def showPoints(aList):
        for p in aList:
            print( p )

    def main():
        points = [ MyPoint(1, 2), MyPoint(2, 7), MyPoint(3, 4),
                    MyPoint(12, 5), MyPoint(6, 8) ]
        points.sort(key=MyPoint.descX)
        showPoints(points)

    main()