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

Date:  December 3, 2013
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
    for i in range(5):
        print(i)
        print(i)

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

    # Boolean Formula
    def main():
        print( "P", "Q", "P or Q", sep='\t' )
        for P in [True, False]:
            for Q in [True, False]:
                print( P, Q, P or Q, sep='\t')

    main()

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

    # Type Conversion
    a = "2"
    b = "3"
    c = a + b
    d = eval(a + b)
    e = eval(a) + eval(b)
    print(a, b, c, d, e)


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

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


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

    # String Slicing
    str = "UNIVERSITY"
    print( str[3:-3] )


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

    # Splitting a List
    school = "NATIONAL CHI NAN UNIVERSITY"
    aList = school.split()
    for i in range( len( aList ) ):
        print(aList[i][2], end='')


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

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

    def main():
        f(65)
        print()

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

    # Pass by Value
    def main():
    aList = [1, 2, 3]
    print(aList)
    print_star( aList )
    print(aList)

    def print_star( a ):
    for i in a:
    while i > 0:
    print('*', end='')
    i = i - 1
    print()

    main()


  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).
    # Modifying a List
    aList = [0] * 6
    sequence = "1 2 3 4 5 1 3 5"
    for i in sequence.split():
    aList[i] = aList[i] + 1

    for i in aList:
    print(i, end=' ')

  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).
    # Loop Structure
    for i in range(10):
    if i == 3: break
    print(i)
    print(i)