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

科目名稱:計算機概 論 開課系所:資訊工程 學系 考試日期 2014.1.7
系所別:
年級:
學號:
姓名:
考試時間 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 how to print out an object

    class Rational:
        def __init__(self, n, d):
            self.numerator = n
            self.denominator = d
        def __str__(self):
            return str(self.numerator) + '/' + str(self.denominator)

    def main():
        a = Rational(6, 8)
        print(a)

    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).
    # Reduce a rational number automatically reduces before it is printed out

    def gcd(a, b):      # Greatest Common Divisor
        if b == 0:
            return a
        else:
            return gcd(b, a % b)

    class Rational:
        def __init__(self, n, d):
            self.numerator = n
            self.denominator = d
        def reduce(self):
            g = gcd(self.numerator, self.denominator)
            self.numerator = self.numerator // g
            self.denominator = self.denominator // g
        def __str__(self):
            self.reduce()
            return str(self.numerator) + '/' + str(self.denominator)

    def main():
        a = Rational(6, 8)
        print(a)

    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).
    # Modify a list within a function

    def update(lst):
        lst = lst + [5]
        lst[0] = 3

    def main():
        a = [2]
        update(a)
        print(a)

    main()