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


Date:  October 27, 2015
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).

    # Definite Loop
    N = 6
    for i in range(N):
        print(i, sep='')

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

    # Definite Loop
    N = 6
    for i in range(N, 1):
        print(i)
    print(N)


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

    # Simultaneous Assignment
    a, b, c, d = 1, 2, 3, 4
    for i in range(22):
        a, b, c, d = d, c, a, b
    print(a, b, c, d)

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

    # Reverse Order
    alphabet = "ELIZABETH"
    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, 2):
        sum = sum + i
    print(sum)


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

    #
    Stars
    for i in range(2):
        for j in range(3):
            print('*', end='')
        print()

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

    # String Slicing
    N=5
    s = " "*(N-1)+"+"+" "*(N-1)
    for j in range(2):
        for i in range(N-1):
            print( s[ i : i+N ] )
        for i in range(N-1, -1, -1):
            print( "{0}".format(s[ i : i+N ]) )

  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 = "HOLCDEHWK" # ord('A') == 65
    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).
    # Template String
    for i in range(8):
    s = "{0:" + "><"[(i // 2) % 2] + "3}"
    print( s.format("#"*(3-(i%2)*2)) )
  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).
    # Return Values of a Function
    def star(n):
    return '*' * n

    print( star(10) )
    print( star(5) + star(5) )
  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).
    # Return Values of a Function
    def star(n):
    print('*' * n )
    return n

    print( star(10) )
    print( star(5) + star(5) )
  12. (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).
    # File I/O
    outfile = open("exam.dat", "w")
    for i in range(27):
    print(i, file=outfile)
    outfile.close()

    infile = open("exam.dat", "r")
    s = infile.read()
    print( len(s) )
    infile.close()