科目名稱: 計算機概論 第一次期中考 開課系所:資工系 考試日期 2017.10.19
系所別:
年級:
學號:
姓名:
考試時間 08:20-10:00
Open book; turn off computer & mobile phone
1
2
3

4
5

6



7
8



9
10
11








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

    # print() and String quotation
    print(1, 3, 5+7, end=' ')
    print(9, 2, 4+6, sep=' ')
    s = "TGIF stands for \"Thank God It\'s Friday.\""
    print(s, len(s))


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

    # numbers2text.py (P.145)
    inString = "79 67 84 79 66 69 82"       # chr(66) == "B"
    # Loop through each substring and build Unicde message
    message = ""
    for numStr in inString.split():
        # convert the (sub)string to a number
        # append character to message
        message = message + numStr
    print("The decoded message is:", message)


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

    # String Formatting
    print( "{0}".format(5) )
    print( "{0:2}".format(5) )
    print( "{0:02}".format(5) )


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

    # Accumulator and range()
    a = 5
    b = 3
    sum = 0
    for i in range(1, a**b, 2):
        sum = sum + i
    print( sum )


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

    # Reverse

    s = "Hello"
    r = ""
    print('The reverse of "' + s + '" is')
    for c in range(1, len(s)+1):
        r =  s[-c] + r
    print(r)



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

    #
    Lists, split() and join()
    s = "32,24,25,57"
    for item in s.split("2"):
        print( item )
    months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun",
              "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
    t = "".join( months[4:10] )
    print( len(t), t )

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

    # count() and find()
    s = "abcd:defg:ghij"
    print( s.count('d'), s.find('e'), len(s.split('/')) )

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

    # Stars
    N = 6
    template = "{0:>" + str(N-1) + "}"
    for i in range(1, N):
    print(template.format('*' * i))

  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).
    # ord(), Indexing and Slicing
    first = "George"
    last = "Lucas"
    # chr(65) == "A"; chr(97) == "a"
    for c in first[-2:]:
    print( ord(c) )
    print( ord(last[-2]) )


  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).
    # Caesar Cipher
    key = 21
    encrypted_msg = "JCUJHYTUFYWTSZR"
    plaintext = ""
    for c in encrypted_msg:
    if 'A' <= c <= 'Z':
    c = chr( (ord(c) - 65 + key) % 26 + 65 )
    plaintext = plaintext + c
    print(plaintext)
  11. (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).
    # int() and str()
    N = 6
    template = "{0:" + str(N) + "}"
    # By default, strings are left-justified
    # and numbers are right-justified
    for i in range(N, 0, -1):
    print( template.format( int(str(i)*i) ) )