科目名稱: 計算機概論 第二次期中考 開課系所:資工系 考試日期 2017.12.21
系所別:
年級:
學號:
姓名:
考試時間 08:20-10:00
Open book; turn off computer & mobile phone
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).

    # List and String
    s = "A SONG OF ICE AND FIRE"
    aList = s.split()
    print(len(aList), len(aList[2]), len(aList[2][1]))


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

    # ord(), chr(), join
    aList = []
    for i in range(6):
        n = ord('A') + i
        c = chr(n)
        aList.append(c)
        print( ''.join(aList) )


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

    # String Formatting
    fmt0 = "{0:0.1%}"
    fmt5 = "{0:5.1%}"
    a = 0.015
    s0 = fmt0.format(a)
    s5 = fmt5.format(a)
    print( len(s0), len(s5) )


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

    # split()
    url = "http://cnn.com/TECH/index.html"
    aList = url.split('/')
    print(len(aList), aList[-1])

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

    # File I/O
    s = "This is a book.\nOpen the door.\nPlease sit down."
    outfile = open("q5.txt", "w")
    print(s, file=outfile)
    outfile.close()

    infile = open("q5.txt", "r")
    for line in infile:
        words = line.split()
        print( len(line), len(words) )
    infile.close()



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

    # Recursive Function Invoking
    def f(n):
        if n >= 1:
            f(n // 2)
        print( n % 2, end='')

    def main():
        f(63)
        print()

    main()


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

    # Passing Arguments to a Function
    def f(x, y):
        x = x + 1
        y = y + 1
        return x + y

    def main():
        a = 12
        b = 21
        print( f(a, a) )
        print( f(a, b) )

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

    # Return
    def star1(n):
    print( '*' * n )

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

    print( star1(10) )
    print( star2(10) )

  9. (10%) Consider the following code.  We want the function f() to return a random uppercase alphabet between 'A' and 'Z'.  What should be the value of N?
    # random
    def f():
    import random
    N = _?_
    c = chr( random.randrange(65, 65+N) )
    return c

    for i in range(100):
    print( f(), end='' )
    print()



  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).
    # String Methods (P.142)
    def main():
    plaintext = "XHOSCTISEMNHLI"
    t = ['ABCDE', 'FGHIJ', 'KLMNO', 'PQRST', 'UVWXY']
    ciphertext = enc(plaintext, t)
    print(ciphertext)

    def enc(s, tabula):
    ciphertext = ""
    for i in range(0, len(s)-1, 2):
    x1, y1, x2, y2 = findDiagonal(tabula, s[i:i+2])
    c3c4 = getDiagonal(tabula, x1, y2, x2, y1)
    ciphertext = ciphertext + c3c4
    return ciphertext

    def getDiagonal(tabula, x1, y1, x2, y2):
    return tabula[x1][y1] + tabula[x2][y2]

    def findDiagonal(tabula, s):
    # s is a 2-character string
    c1, c2 = s[0], s[1]
    longStr = "".join(tabula)
    p1 = longStr.find(c1)
    x1, y1 = p1 // 5, p1 % 5
    p2 = longStr.find(c2)
    x2, y2 = p2 // 5, p2 % 5
    return x1, y1, x2, y2

    main()