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


Date:  December 10, 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 b(p):
        if p:
            return "T"
        else:
            return "F"

    def main():
        str="{0:^8} {1:^8} {2:^15}"
        print( str.format("P", "Q", "not P or Q") )
        print( str.format("===", "===", "==========") )
        for P in [True, False]:
            for Q in [True, False]:
                print( str.format(b(P), b(Q), b(not P or Q) ) )

    main()

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

    # Type Conversion
    import math

    a = 3.14
    b = 0.5
    c = a + b
    d = int(c)
    e = math.ceil(c)
    print(a, b, c, d, e)


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


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

    # String Slicing

    str = "WELLINGTON"
    print( str[-6:6] )




  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 range( len(a) ):
    while a[i] > 0:
    print('*', end='')
    a[i] = a[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).
    # Modify a List
    aList = [0] * 6
    sequence = "2 4 6 1 2 3 4 5"
    for j in sequence.split():
    i = eval(j)
    aList[i] = aList[i] + 1

    for i in aList:
    print(i, end=' ')
  10. (10%) What will be the output of the following program?
    # Loop Structure
    i = 1
    while i < 10:
    if i == 3: break
    else:
    i = i + 1
    print(i)
    print(i)