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


Date:  December 2, 2014
Time: 14:20-16:00
Open book; turn off computer & mobile phone

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

    # Greatest Common Divisor
    def gcd(m, n):
        while m != 0:
            n, m = m, n%m
        return n

    def main():
        a, b = 72, 108
        print( gcd(a,b) )
        print( a, b)

    main()

  2. (10%) Suppose that you created a file "student.txt" using "nano" with the following contents:
    78:62
    91:69
    86:71
    80:74
    79:77
    What will be the output by running the following code?

    # File Loop
    fileName = "student.txt"
    infile = open(fileName, 'r')
    count = 0
    sum = 0
    for line in infile:
        sum = sum + eval( line.split(':')[1] )
        count = count + 1

    print("The average score of", count, "students is", sum / count)
    infile.close()


  3. (10%) Consider the following decision structure:

    a, b, c, = eval(input('Enter three numbers: '))
    if a > b:
        if b > c:
            print("Spam Please!")
        else:
            print("It's a late parrot!")
    elif b > c:
        print("Cheese Shoppe")
        if a >= c:
            print("Cheddar")
        elif a < b:
            print("Gouda")
        elif c == b:
            print("Swiss")
    else:
        print("Trees")
        if a == b:
            print("Chestnut")
        else:
            print("Larch")
    print("Done")

    What will be the output that would result from each of the input 3,4,3?

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

    # for-loop
    sum = n = 0
    for i in range(5):
        sum = sum + i
        n = n + 1
    print(sum, n, i)


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

    # break

    i = 4
    n = 0
    sum = 0
    while True:
        if i == 0: break
        sum = sum + i
        n = n + 1
        i = i - 1
    print( sum / n )



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

    #
    String Slicing
    str = "Nicaragua"
    print( str[-7:7] )

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

    # Accumulator
    sum = 0
    for i in range(2, 100, 3):
        sum = sum + i
    print(sum)
  8. (10%) Waht will be the output of the following program?

    # curses library
    import curses

    stdscr = curses.initscr()
    for i in range(10, 0, -1):
    stdscr.move(i, i)
    stdscr.addstr( "**********"[:i] )
    stdscr.refresh()
    curses.endwin()

  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).
    # Boolean Operator
    Q = True
    P = False
    print( not P or Q)

  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).
    # Boolean Expressions as Decisions (P.255)
    for p in ["True", "False"]:
    for q in ["True", "False"]:
    if ( p or q ):
    print("The value is True.")