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

科目名稱:計算機概 論 開課系所:資訊工程 學系 考試日期 2014.12.30
系所別:
年級:
學號:
姓名:
考試時間 14:20-14:35
  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).
    class Point:
        def __init__(self,x,y) :   # Constructor
            self.x=x
            self.y=y
        def __str__(self):
            return "({0},{1})".format(self.x,self.y)

    def main():
        aList = [1, 3, 5, 7, 2, 4, 6]
        bList = [2, 4, 6, 1, 3, 5, 7]
        pList = []
        for x in aList:
            pList.append( Point(x, bList[aList.index(x)]) )
            # P.345
        for p in pList:
            print(p)

    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).
    class Point:
        def __init__(self,x,y) :
            self.x=x
            self.y=y
        def distance(self) :
            r=(self.x**2+self.y**2)**(1/2)
            return r
        def __str__(self):
            return "({0},{1})".format(self.x,self.y)

    def main():
        aList = [ Point(1,2), Point(2,7), Point(3,4), Point(12,5), Point(6,8) ]
        aList.sort(key=Point.x)  
        # Sort by the X coordinate (pp.353--354)

        print(aList)

    main()



  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).
    # Key Function for Sorting
    class Student:
        def __init__(self, n, id):
            self.name = n
            self.id = id
        def __str__(self):
            return self.id + ':' + self.name
        def get_id(self):
            return self.id
        def get_name(self):
            return self.name

    def get_name(aStudent):
        return 150 - ord(aStudent.name[0])

    def main():
        aList = [ Student("Alice", "177"),
            Student("Bob", "115"),
            Student("Carol", "135"),
            Student("Dickens", "193"),
            Student("Emily", "117") ]
        aList.sort(key=get_name)
        for p in aList:
            print(p)

    main()