Final Exam
Introduction to Computer Science
NCNU CSIE

Date: January 14th, 2014
Time: 14:10-16:00
Open book; turn off computer & mobile phone

  1. (10%) In the "Last Lecture" of Prof. Randy Pausch, when he hit the "brick wall" that NASA made it very clear that under no circumstances were faculty members allowed to fly with the teams in zero gravity, how did he find a solution?



  2. (10%) When Prof. Pausch told Tommy Burnette that Lucasfilm are probably not going to make the next Star Wars movies, what did Tommy reply?



  3. (10%) What will be the output of the following program?

    # String Formatting (P.147)
    def star(n):
        return '*'*n

    height = 5
    for i in range(height):
        template = "{0:^" + str( 2 * height - 1 ) + "}"
        print( template.format( star( 2 * i + 1 ) ) )



  4. (10%) What will be the output of the following program?

    # Numeric and Sring Operations (P.58 and P.125)

    def star(n):
        return '*'*n

    def layer(width, n):
        left = (width - n) // 2
        right = width - left
        return ' '*left + star(n) + ' '*right

    height = 4
    for i in range(height):
        print( layer( 2 * height - 1 , 2 * i + 1 ) )

  5. (10%) What will be the output of the following program?

    # Functions that Modify Parameters (P.183)

    def modify(aList):
        aList[0] = 5
        aList = aList + [2]
        return aList

    a = [3]
    modify(a)
    print(a)

  6. (10%) What will be the output of the following program?

    # Pass by Value (P.186)

    def modify(aList):
        aList = aList + [2]
        aList[0] = 5
        return aList

    a = [3]
    modify(a)
    print(a)


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

    # Some List Methods (P.345)

    def modify(aList):
        aList[0] = 5
        aList.append(2)
        return aList

    a = [3]
    modify(a)
    print(a)

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

    # Getting Results from a Function (P.179)

    def modify(aList):
        aList[0] = 5
        aList.insert(0, 2)
        return aList

    a = [3]
    print( modify( a ) )


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

    #
    __str__() Method of a Class (P.336)

    class Triangle:
        def __init__(self, h):
            self.height = h
        def __str__(self):
            s = ""
            for i in range(self.height):
                s = s + ( i + 1 ) * '*' + '\n'
            return s

    def main():
        print( Triangle( 2 ) )
        print( Triangle( 5 ) )
        # Please clearly specify how many blank lines are there between
        # these two triangles
    main()

  10. (10%) Determine whether the following code has syntax erros 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(n) + '/' + str(d)

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

    main()
  11. (10%) Determine whether the following code has syntax erros or not.  If it is correct, predict its output.  If it is incorrect, point out the mistake(s).
    # Reduce a rational number automatically 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):
            return str(self.numerator) + '/' + str(self.denominator)

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

    main()
  12. (10%) In addition to the string methods in P.140, Python also provides some built-in methods such as isalpha(), isdigit(), islower() to help us determine whether all characters in a string are alphabetic, digits, or lowercase characters.  For example, "book".isalpha() returns True, while "2014".isalpha() returns False.  Now please predict the output of the following code.
    # ASCII code (P.132)
    msg = "DRO ZYBD XEWLOB SC DGOXDI DRYECKXN YXO REXNBON KXN PYBDI YXO"
    key = 16
    plaintext = ""

    for ch in msg:
    if ch.isalpha():
    plaintext = plaintext + chr( (ord(ch) - 65 + key) % 26 + 65 )
    else:
    plaintext = plaintext + ch

    print(plaintext)