Mid-term Exam (1)
Introduction to Computer Science
NCNU CSIE


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

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

    # Simultaneous Assignment
    a , b = 144, 233
    for i in range(5):
        a, b = b - a, a
    print(a, b, sep=',')

  2. (10%) Suppose that you created a file "oblivion.txt" using "nano" with the following contents:
    How can a man die better
    than facing fearful odds
    for the ashes of his fathers
    and the temples of his Gods
    What will be the output by running the following code?

    # File I/O
    Infile = open("oblivion.txt", "r")
    w = 0
    for s in Infile:
        w = w + len( s.split() )
    print( w )
    Infile.close()

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

    # Subtraction
    def main():
        sum = 666
        for i in range(36):
            sum = sum - i
        print("sum = ", sum)

    main()

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

    # Reverse Order
    alphabet = "ABCDEFG"
    n = len(alphabet)
    for i in range(n // 2):
        print(alphabet[n - 1 - i], end='')
    print()

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

    # Accumulator

    sum = 0
    for i in range(2, 100):
        sum = sum + i
    print(sum)


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

    #
    Stars
    N = 6
    for i in range(1, N):
        str = "*" * i
        print(str)

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

    # Template Strings
    N = 7
    template = "{0:>" + str(N-1) + "}"
    for i in range(1, N):
        str = "*" * i
        print( template.format(str) )

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

    # Caesar Cipher
    message = "KDWKDZDB"
    key = 23
    for ch in message:
    print( chr( ( ( ord(ch) + key - 65) % 26 ) + 65 ), end='' )
    print()

  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).
    # Indexing and Slicing
    def main():
    first = "Anne"
    last = "Hathaway"
    print(first[-1:] + last[-1])
    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).
    # Dice Rolling
    count = [ 0, 1, 1, 2, 4, 2 ]
    for n in [1, 2, 2, 3, 5, 3 ]:
    count[n-1] = count[n-1] + 1

    for i in range(6):
    print( i+1 , '*' * count[i] )
  11. (10%) 試寫出「島讀臺灣:時空旅行特展」中,三位時光旅人的姓名。
    Traveller
  12. (10%) 數位典藏導覽中,有提到三樣以歷史文物為樣本所設計出來的設計品。請舉出一例,並說明為何喜歡這項設計品。