To Exam Info

IT 212 -- Midterm Review Questions

Part A: Multiple Choice Questions. Circle the letter of each correct answer.  Give an optional reason or show your work for each question.  If your answer is correct, the reason will not be considered. 5 points each.

  1. What is the output of this Python script?
    x = 59
    print("{0:08b}".format(x))
    
    a. 00111011           b. 01011001           c. 01110110           d. 01111111
  2. What is the output of this Python script?
    x = 0x7e
    print("*{0:d}*".format(x))
    
    a. 127             b. *126*             c. 711              d. *711*
  3. Which character has the binary ascii code 00111111?
    a. #                b. $                c. %                   d. ?
  4. Express the decimal number 95 as (i) one byte binary, (ii) packed decimal, (iii) binary ASCII:
    a. 01001111; 01110101; 00111001 11000101    
    b. 01001111; 10010101; 00111001 11000101
    c. 01011111; 10010101; 00111001 11000101    
    d. 01011111; 10010101; 00111001 11000111
  5. What is the output?
    n = 123
    print(type(n))
    
    a. <class 'const'>      b. <class 'bool'>      c. <class 'int'>      d. <int>
  6. Which of these is the best spelling for a Python variable?
    a. numberOfCustomers               b. number of customers
    c. number-of-customers            d. number_of_customers
  7. What is the output?
    total = 0
    for n in range(1, 5):
        total += n
    print(total, end="")
    
    a. 1 3 6 10            b. 10            c. 13610            d. 1 3 6 1015
  8. If hr, min, sec, and am_pm are defined as
    hr = 2
    min = 34
    sec = 9
    am_pm = "PM" 
    
    and hr can range from 0 to 11, which script produces the output 2:34:09 PM.
    a.  if hr == 0:
            print("12:{0:02d}:{0:02d} {2:s}".format(min, sec, am_pm}
        else:
            print("{0:02d}:{1:02d}:{1:02d} {3:s}".format(hr, min, sec, am_pm}
    
    b.  if hr == 0:
            print("12:{0:02d}:{1:02d} {2:s}".format(min, sec, am_pm}
        else:
            print("{0:02d}:{1:02d}:{2:02d} {3:s}".format(hr, min, sec, am_pm}
    
    c.  if hr == 0:
            print("12:{0:d}:{0:02d} {2:s}".format(min, sec, am_pm}
        else:
            print("{0:02d}:{1:d}:{1:02d} {3:s}".format(hr, min, sec, am_pm}
    
    d.  if hr == 0:
            print("12:{0:d}:{1:02d} {2:s}".format(min, sec, am_pm}
        else:
            print("{0:02d}:{1:d}:{2:02d} {3:s}".format(hr, min, sec, am_pm}
    
  9. Which of the following is a dunder method?
    a. __init__                    b. open                     c. Person                   d. str
  10. What is the output?
    a = [23, 41, 19, 15]
    print(a.pop( ))
    print(a.append(99))
    a.sort( )
    a.reverse( )
    print(a)
    
    a. [41, 23, 19]                    b. [41, 23, 19, 15]
    c. [99, 41, 23, 19]              d. [99, 41, 23, 19, 15]    
  11. Which is the correct UML diagram is correct for the __str__ method?
    a.  - __str__( ) : str                b.  - __str__(self) : str
    c.  + __str__(self) : str           d.  + __str__(str) : str
  12. A Python instance method always has one more parameter at definition than it does at invocation?
    a. False                            b. True
  13. What is the name of the constructor for a Python Card class?
    What is the name of the constructor for a Python Card class?
    a. Card            b. constructor            c. initialize            d. __init__
  14. Which of these method calls is the best way to test the __str__ method of the Card class? Define a Card object like this:
    c = Card(11, "C")
    
    a. __str__(c)            b. c.__str__( )            c. c.str( )            d. str(c)
  15. The name of an instance variable or method that is private begins with which prefix?
    a. _               b. &                c. $                  d. @    

Part B: Reorder a List. 10 points.

  1. Reorder these operators in sequence from highest precedence to lowest precidence.
    and   =     +    ==    **    .    ( )    *    [ ]

Part C. Correct the Errors. 15 points.

  1. Correct the errors in this VendingMachine class and test script. There are about 15 total errors.
    # --- Source code file vendingmachine.py -------------------
    
    # This vending machine dispenses one type of
    # candy bar, which is paid for with 3 quarters.
    
    class vendingmachine:
    
        def __init__(self)
            self._num_candy_bars == 3
            _num_quarters = 0
    
        def insert_quarter(self):
            self._num_quarters + = 1
            return "Quarter inserted."
    
        def dispense-candy-bar( ):
            if self._num_quarters >= 3 and \
                self._num_candy_bars > 0:
                self._num_quarters = 0
                self._num_candy_bars -= 1
                return "Candy bar dispensed."
            else
                return "VM out of candy bars."
    
        def __str__(self):
            return f"{self._num_candy_bars} " + \
                   "{self._num_quarters}"
    
    # --- Source code file: test1.py ---------------------
    from vendingmachine import VendingMachine
    
    vm = new VendingMachine( )
    print(vm)
    print( )
    print(vm.insert_quarter( ))
    print(vm)
    print(vm.insert_quarter( ))
    print(vm.insert_quarter( ))
    print(vm.dispense_candy_bar( ))
    print(vm)
    
    for i in range(0: 3)
        print
        for j in range(0, 3):
            print(self.insert_quarter( ))
        dispense_candy_bar( )
        print(str(vm))
    

Part D: Predict the Output. Predict the output of test1.py.

# --- Source code file automobile.py
class Automobile:

    def __init__(self, the_vin, the_color):
        self.vin = the_vin
        self.color = the_color
        self.is_running = False
        self.gear = "Park"
        self.speed = 0

    def __str__(self):
        return self.vin + " " + self.color

    def turn_key(self, the_direction):
        if the_direction == "on":
            self.is_running = True
        else:
            self.is_running = False

    def shift(self, the_gear):
        self.gear = the_gear

    def press_accelerator(self, the_amount):
        if self.is_running and self.gear == "Drive":
            if self.speed + the_amount < 100:
                self.speed += the_amount
            else:
                self.speed = 100

    def press_brake(self, the_amount):
        if self.speed > the_amount:
            self.speed =- the_amount
        else:
            self.speed = 0

# --- Source code file test1.py
from automobile import Automobile

auto = Automobile("Ford", "Silver")
print(auto)
auto.turn_key("on")
auto.shift("Drive")
auto.press_accelerator(20)
auto.press_accelerator(15)
print(auto.speed)
auto.press_brake(40)
print(auto.speed)
auto.turn_key("off")
print(auto.is_running)

Part E: Convert Traditional Test to Unit Test. Convert the traditional test file test1.py to a unit test file test2.py.

from automobile import Automobile
import unittest

class MyUnitTestClass(unittest.TestCase):
        
    def test_1(self):
    
        # Sample assertEqual statement: 
        # self.assertEqual(computed, expected)
        
        
        
        
        
        
        
        
        



        
        
        
        
if __name__ == '__main__':
    unittest.main( )