Answers to Sample Final Exam Questions

The part of the final dealing with material covered before the midterm will be small, 2-4 questions at most. The final will emphasize material from May 9 (Programming languages and the Windows System) on. Some of the questions will be on the Python parts of the course. These questions will explore various aspects of Python and of programming. The central themes are writing a program, data types (with variables), operators, selection, and looping. These questions are sample Python questions. The other questions will be like the ones asked on the midterm.

  1. Which of the following are valid Python identifiers?
    1. N1
    2. N_1
    3. N.1
    4. N-1
    5. Zzzzzzz
    6. SumUp
    7. Good_Show
    8. Number
    9. NUMBER
    10. RateOfIncrease
    11. 2Good2BeTrue
    12. XYZ
    Answer: a, b, e, f, g, h, i, j, and l. c is not because it has "."; d is not because it has "-"; and k is not because it begins with a digit. See p. 34 of the Python programming text.

  2. Convert each of the following (non-Python) expressions into Python expressions:
    1. 3x
    2. 3x + y
    3. x is evenly divisible by 12
    4. x plus 7 is more than 100 or else x is less than fifty
    5. xy and 2 ≤ z
    Answer:
    1. 3 * x
    2. 3 * x + y
    3. (x + y) / 7
    4. (3 * x + y) / (z + 2)
    5. (x % 12) == 0
    6. (x + 7 > 100) or (x < 50)
    7. (x <= y) and (2 <= z)

  3. Assuming num = 20, determine the value of each of the following Python expressions:
    1. num / 12
    2. 123 % 100
    3. 8 + 3 * 7
    4. (0 == 1) and (2 < 3)
    5. not ((4.5 < 12.9) and (6 * 2 <= 13))
    6. (0 ==1) or ( 2 < 3)
    7. (0 == 1) or (2 < 3) and (7 < 6)
    8. (2 < 3) or (0 == 1) and (7 < 6)
    Answer:
    1. 1
    2. 23
    3. 29
    4. false
    5. false
    6. true
    7. false
    8. true

  4. Write the output produced by this program below.
    x = 3              
    if 2 > x :
        print 'First'
    else :
        print  'Second'
        if 2 > x :
            print 'Third'
        print 'Fourth'
    print `Fifth'
    
    Answer:
    Second
    Fourth
    Fifth
    

  5. Write the output produced by this program below.
    words = 'this IS NoT EvEN'
    print words.title()
    print words.replace("IS", 'was')
    print words.upper()
    print words * 2
    
    Answer:
    This Is Not Even
    this was NoT EvEN
    THIS IS NOT EVEN
    this IS NoT EvENthis IS NoT EvEN
    
  6. Find the error in the following program.
    line = raw_input("Type a word")
    print "You typed", line
    line = line + "h"
    num = int(line)
    print "You typed the number ", num
    
    Answer: The problem lies in lines 3 and 4. Line 3 appends a letter ("h") to the input string. Line 4 then tries to convert the result to an integer. The result, however, will never be a valid integer because of the letter "h". So you get an error.

  7. Outline a program that will prompt a user to enter a temperature as an integer. Your program will print "it is hot" is the temperature is over 100, "it is cold" if the temperature is under 60, and "it is just right" if the temperature is between 61 and 99 inclusive. The program continues to ask for termperatures, and evaluates them as above, until the user enters a temperature of 0.

    Answer: Here is my version, in pseudocode.

    prompt the user for an integer input
    read the input and convert it to an integer
    while the integer is not 0
        if the integer is 100 or more, print "it is hot"
        if the integer is 60 or less, print "it is cold"
        otherwise, print "it is just right"
        prompt the user for an integer input
        read the input and convert it to an integer
    

  8. Write a Python program that does the above. Here is what the output should look like:
    Please enter a temperature: 95
    It is just right.
    Please enter a temperature: 110
    It is hot.
    Please enter a temperature: 32
    It is cold.
    Please enter a temperature: 0
    Good bye!
    
    Answer: Building on the pseudocode in the answer to question 7, we get:
    # 
    # pseudocode:
    #   prompt the user for an integer input
    #   read the input and convert it to an integer
    #
    temp = int(raw_input("Enter temperature please: "))
    #
    # pseudocode: while the integer is not 0
    #
    while temp != 0:
        #
        # pseudocode:
        #   if the integer is 100 or more, print "it is hot"
        #
        if temp >= 100:
            print "it is hot"
        #
        # pseudocode:
        #   if the integer is 60 or less, print "it is cold"
        #
        elif temp <= 60:
            print "it is cold"
        #
        # pseudocode:
        #   otherwise, print "it is just right"
        #
        else:
            print "it is just right"
        #
        # pseudocode:
        #   prompt the user for an integer input
        #   read the input and convert it to an integer
        #
        temp = int(raw_input("Enter temperature please: "))
    #
    # Say goodbye
    #
    print "Good bye!"
    


Here is a PDF version of this document.