Midterm Exam (2)
Introduction to Computer Science
NCNU CSIE


Date:  November 9, 2016
Time: 08:10-10:30
Open book; turn off computer & mobile phone

  1. (10%) In LibreOffice, what are the application names of its word processor, spreadsheet, and presentation program, respectively?


  2. (10%) Determine whether the following code has syntax errors 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)

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

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


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

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


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

    # String Slicing

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


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

    #
    Splitting a List
    title = "SONG OF ICE AND FIRE"
    aList = title.split()
    for i in range( len( aList ) ):
        print(aList[i][:2], end='')

  8. (10%) Determine whether the following code has syntax errors 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(119)
        print()

    main()

  9. (10%) Determine whether the following code has syntax errors or
        not.  If it is correct, predict its output.  If it is
        incorrect, point out the
        mistake(s).

    # Pass by Value
    def main():
    aList = [9, 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()
  10. (10%) Determine whether the following code has syntax errors or not.  If it is correct, predict its output.  If it is incorrect, point out the mistake(s).
    # Caesar Cipher
    message = "FLWJJRJSY"
    key = 21
    for ch in message:
    print( chr( ( ( ord(ch) + key - 65) % 26 ) + 65 ), end='' )
    print()
  11. (10%) What will be the output of the following program?
    # Loop Structure
    i = 1
    while i < 10:
    if i == 7: break
    else:
    i = i + 1
    print(i)
    print(i)