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.
x = 59
print("{0:08b}".format(x))
a. 00111011 b.
01011001 c.
01110110 d.
01111111
x = 0x7e
print("*{0:d}*".format(x))
a. 127 b.
*126* c.
711 d.
*711*
n = 123 print(type(n))a. <class 'const'> b. <class 'bool'> c. <class 'int'> d. <int>
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 1015hr = 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}
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 = Card(11, "C")a. __str__(c) b. c.__str__( ) c. c.str( ) d. str(c)
Part B: Reorder a List. 10 points.
and = + == ** . ( ) * [ ]
Part C. Correct the Errors. 15 points.
# --- 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( )