To Exam Info

IT 211 -- Final 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 repetition operator?
    a. %             b. +            c. *             d. **
  2. Which method call returns a string with with only trailing new line characters removed?
    a. line.split("\n")           b. line.strip( )
    c. line.rstrip("\n")          d. rstrip(line, "\n")
  3. What is the output?
    a = [["philadelphia", "chicago", "denver"],
          "houston", "tulsa", "bismark", "cheyenne"]]
    output = [ ]
    for b in a:
        for c in b:
            output.append(c[2])
    print("".join(output))
    
    a. ["chicago", "tulsa"]       b. hheouih        
    c. iin                                   d. iinulse
  4. Which keyword is used to start the definition of a user defined method?
    a. def           b. function           c. method           d. sub
  5. If the input file letters.txt contains
    abc
    def
    hij
    
    what is the output from this script?
    fin = open("letters.txt", "r")
    lines = [ ]
    line = fin.readline( )
    while line:
        lines.append(line)
        line = fin.readline( )
    print(lines)
    
    a. abcdefhij                            b. abc def hij
    c. ["abc", "def", "hij"]        d. ["abc\n", "def\n", "hij\n"]
  6. The following script creates a PPM image file. What does the image file display?
    fimage = open("image.ppm", "w")
    fimage.fwrite("P3 5 5 255\n")
    line1 = "0 0 0  0 0 0  0 0 0    0 0 0  0 0 0\n"
    line2 = "0 0 0  0 0 0  0 0 255  0 0 0  0 0 0\n"
    fimage.fwrite(line1)
    fimage.fwrite(line1)
    fimage.fwrite(line2)
    fimage.fwrite(line1)
    fimage.fwrite(line1)
    fimage.close( )
    
    a. A blue dot on a black background.
    b. A red dot on a blue background.
    c. A green dot on a red background.
    d. A black dot on a white background.
  7. This script converts a value in miles to inches:
    value1 = float(input("Input a value in miles: ")
    value2 = value1 * 12 * 5280
    
    Which user defined method inputs a value in miles and returns that value in inches?
    a. def miles_to_inches(  ):
              return value2
    	
    b. def miles_to_inches( ):
           value2 = value1 * 12 * 5280
           return value2
    
    c. def miles_to_inches(the_miles):
           return the_inches * 12 * 5280
    
    d. def miles_to_inches(the_miles):
           return value2
    
  8. What happens if you forget to close a file after you are finished writing to it?
    a. An error message is generated.
    b. No output is written to the output file.
    c. The output is the same as if the file is not closed.
    d. The output may be written to the wrong output file.
  9. If the TextIOWrapper object fout has been used to write to an output file, which method call closes it?
    a. close(fin)             b. close(fin, "w")
    c. fout.close( )        d. fout.close("w")
  10. Which of these is NOT a reason to use methods in software development?
    a. code reuse     b. encapsulation     c. modularization     d. segmentation
  11. If a dictionary object is defined like this:
    d =  {"name" : "Sally", "id" : 2345, "quiz_average" : 89}
    
    how would the quiz average be printed?
    a. print(d(quiz_average))        b. print(d["quiz_average"])
    c. print(quiz_average)             d. print("quiz_average")
  12. Which is an example of a properly spelled Python class variable?
    a. pokerhand               b. poker_hand
    c. PokerHand                d. POKER_HAND
  13. If the class A is defined like this:
    class A:
        def __init__(the_x, the_y):
            self.x = the_x
            y = the_y
    
    which of the following is a local variable?
    a. the_x         b. the_y        c. x         d. y

Part B: Predict Output. Predict the output. 10 points each.

  1. for i in range(0, 10):
        print(" " * (10 - i), "==")
    for i in range(0, 10):
        print(" " * i, "==")
    
  2. import json
    arr = ["zero", "one", "two", "three"]
    dictionary = { }
    for i in range(1, 4):
        dictionary[i] = arr[i]
    s = json.dumps(dictionary)
    print(s)
    
  3. # Source code file b.py
    class B:
    
        def __init__(self, the_x, the_y):
        self.x = the_x
        self.y = the_y
    
        def __str__(self):
            return f"//{self.x}{self.y}//"
    
        def augment(self):
            self.x *= 2
            self.y += 3
    
    # Source code file test.py
    from b.py import B
    b = B(5, 6)
    print(str(b))
    print(b.x, b.y)
    b.augment( )
    print(b)
    
    

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

  1. This script is supposed to input a three letter currency code and output the converted currency amount from U.S. Dollars (USD). Find the errors. Correct the errors directly on this page--do not recopy.
    d = {"CHF": 0.9936, "CHY": 6.3984, EUR; 0.8608 /
         "GBP": 0.7516, "JPY": 109.05)
    target currency = read_line "Enter desired currency: "
    amount = readline("Enter amount to exchange: " + )
    print("Currency amount in " + target_currency + /
       "is" + round(d[target_currency * amount], 2)
    
  2. This script is supposed to read a JSON script and input a first and last name from the keyboard. It then outputs the GPA of that person. Correct the errors.  Correct the errors directly on this page--do not recopy.
    import Json
    fin.open("grade-records.json", "w")
    json_string = fin.read( )
    arr = loads(json_string)
    
    # Enter desired first and last name.
    desired_first_name = input(Enter desired first name: )
    desired_last_name = input(Enter desired last name: )      
    
    # Print the GPA of the desired person.
    grade_dict = ['A': 4, 'B': 3, 'C': 2, 'D': 1, 'F':= 0}]
    for each d in arr
        if desired_first_name = d["first_name"] && \
           desired_last_name = d["last_name"]
            credit_hours = d["credit_hours"]:
            grade == d[grade]
            grade_points = grade_dict[grade]
            total_credit_hours += credit_hours
            total_grade_points + = grade_points
    
        gpa = total_grade_points \ total_credit_hours
        
    print("GPA:" {round(gpa; 2)})
    
    Here is the beginning of the JSON file grade-recores.json:
    [{"student_id": "148383847", "last_name": "Hoffman", 
    "first_name": "Raymond", "course_number": "ART104", 
    "course_name": "Creating Art", "credit_hours": 4, 
    "grade": "B"}, {"student_id": "148383847", "last_name": 
    "Hoffman", "first_name": "Raymond", "course_number":
    "CHM127", "course_name": "Quantitative Analysis", 
    "credit_hours": 4, "grade": "A"}, ...