To Exam Info

IT 211 -- Midterm Review Questions

Part A: Multiple Choice Questions   For each questions circle the letter of the correct answer. You may give an optional reason or optionally show your work. If you mark the correct answer, the reason will not be considered. 5 points each.  Up to 4 points for reason or work.

  1. Which is the Python string concatenation operator?
    a. %             b. &            c. +             d. *
  2. Which is an example of a properly spelled Python variable?
    a. number of customers           b. number_of_customers
    c. number-of-customers           d. numberOfCustomers
  3. Which str method returns a string with leading and trailing white space removed?
    a. chop         b. chomp         c. remove         d. strip   
  4. Which command can be executed in a Command Prompt Window (Windows) or Terminal Window (Mac) to execute the Python script proj1Smith.py?
    a. exec proj1Smith              b. py proj1Smith.py
    c. python proj1Smith          d. python proj1Smith.py
  5. Which symbol is used to redirect output from a Python script from the computer screen to a file?
    a. $               b. <             c. >              d. R
  6. What is the output?
    a = 8
    b = 5
    b = 2 * a + 3 * b
    a = 2 * a + b
    print(b, a)
    
    a. 8 5         b. 5 8         c. 21 31         d. 31 47    
  7. What is the output?
    n = 123
    print(type(n))
    
    a. <class 'const'>    b. <class 'bool'>    c. <class 'int'>    d. int
  8. What is the output?
    total = 0
    for i in range(1, 5):
        total += n
        print(n, end="")
    
    a. 1 3 6 10        b. 15        c. 13610        d. 1 3 6 1015
  9. Which precedence order is correct if the operators are ordered from highest on the left to lowest on the right?
    a. and - ** / >    b. = + * . not     c . ** + == or =      d. = ** + == or
  10. What is the output?
    x = 2
    y = 3
    print(x ** y == y ** x)
    
    a. 2 3           b. 8 9           c. False         d. True
  11. How many different output values are possible?
    import random
    val1 = random.randrange(0, 3)
    val2 = random.randrange(0, 2)
    print(val1 + val2)
    
    a. 2               b. 3               c. 4                d. 6
  12. What is the output?
    print(True or False and True)
    
    a. False              b. True              c. 0              d. 1        
  13. Which of these is a properly spelled class name?
    a. pokerhand      b. poker_hand      c. _poker_hand     d. PokerHand

Part B: Predict Output. Construct the variable trace and predict the output. 10 points each.

  1. a = 8                a     b    Output:
    b = 5              -----+-----
    b = 2 * a + 3 * b       |
    a = 2 * a + b           |
    print(b, a)             |
    
  2. a = 2                  a     b   a + b
    b = 5                -----+-----+-----
    while a + b <= 50:        |     |
        if a + b <= 15:       |     |
            a *= 2            |     |
            b *= 3            |     |
        else:                 |     |
            a *= 3
            b *= 4       Output:
        print(a, b)
    print(a + b)  
    
  3. s = "a"                    s         t
    t = "b"               ----------+---------   
    for i in range(1, 2):  
       s = (s + t) * 2
       t = t + s * 2
    print(s + t)          Output:
    

Part C: Find and Correct Errors. 15 points each.

  1. The following script is supposed to find the distance between the points (x1, y1) and (x2, y2). It inputs the values x1, y1, x2, y2, computes the distance using the Pythagorean Theorem

           distance = ((x2 - x1)2 + (y2 - y1)2)1/2

    The script then outputs the distance, rounded to three digits after the decimal point
    x1 = input("Enter float value x1: ")
    x1 = input("Enter float value y1: ")
    x2 = input('Enter float value x2: ')
    y2 = input(Enter float value y2:)
    distance = (x2 - x2 ** 2 + x2 - x2 ** 2) ** 1 / 2
    print("Distance between points ({x1},{y1}) and ({x2},[y2] is")
    print(round(dist, 2))
    
  2. To find the maximum city, initialize maxpop to a value smaller than any population in the input file, so that the maxpop can only increase as city data is read from the input file. Correcting a pair of parentheses, brackets, braces, single or double quotes only counts as one error. There are about 15 total errors.
    --- Source code file: findmaxpop.py ----------
    fin = open("michigan-cities.txt", r)
    line = readline(fin)
    while line ! = ""
        maxpop = 10000000
        maxcity = "'
        fields = line_split(',')
        city = fields[0].strip
        state = fields[1].strip
        population = fields(3).strip
        if state = MI
            if population > maxpop
                maxpop = population
                city = maxcity
            line = fin.readline( )
    
    if maxpop = 10000000:
        print 'No cities in input file.\n'
    else
        print("Largest city in {state} is [city]."
    end
    
    
    --- Input file: cities.txt --------
    City;State;Population
    Springfield;IL;117076
    Chicago;IL;2707120
    Detroit;MI;706585
    Des Moines;IA;207599
    Indianapolis;IN;827609
    Peoria;IL;115234
    Ann Arbor;MI;114925