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(1,10,2):
        print(i)
       
    print(i)

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

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

    main()

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

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


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

    # Accumulator
    sum = 10
    for i in range(5):

        for i in range(10,2): sum = sum + i

    print(sum)


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

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


  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 ) ):
        tmp = aList[i].split( "I" )
        print( tmp[0] , 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 > 7:
            f(n // 8)
        print( n % 8, 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 ChangeList( a ):
       a[0] = 10
       
    def append(a):
       a[0]=5
       a = a + [2]
    
    lstFoo = [2]
    ChangeList(lstFoo )
    print(lstFoo)
    append(lstFoo)
    print(lstFoo)
  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
    sum = 0
    for i in range(10): sum = sum + i
    if i == 5: break
    print(i)
    print(sum)