Midterm Exam (1)
Introduction to Computer Science
NCNU CSIE


Date:  October 12th, 2016
Time: 08:30-10:30
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).

    # Simple Calculation
    a = 16
    b = 5
    print(a+b, a-b, a*b, a/b, a//b, a%b)
    b = -5
    print(a+b, a-b, a*b, a/b, a//b, a%b)


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

    # Simultaneous Assignment
    a , b = 377, 610
    for i in range(7):
        a, b = b - a, a
    print(a, b, sep=',')


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

    # Subtraction
    sum = 3003
    for i in range(78):
        sum = sum - i
    print("sum = ", sum)


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

    # Reverse Order
    alphabet = "DRAGONFLY"
    n = len(alphabet)
    for i in range(n // 2):
        print(alphabet[n - 1 - i], end='')
    print()


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

    # Accumulator

    sum = 0
    a = 1
    n = 100
    for i in range(n):
        a = a + 2
        sum = sum + a
    print(sum)



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

    #
    Stars
    N = 5
    for i in range(1, N):
        str = "*" * i
        print(str)

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

    # range()
    sum = 0
    for i in range(10, 2, -2):
        sum = sum + i
        print(sum)

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

    # Indexing
    str = "ALICE"
    n = len(str)
    for i in range(n):
    print(str[-i], sep='')


  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).
    # Indexing and Slicing
    first = "Brad"
    last = "Pitt"
    print(first[-1:] + last[-1])


  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).
    # Stars
    N = 5
    str = ' '*(N-1) + '*'*N
    for i in range(N):
    print( str[i : i+N] )