Practice Midterm Spring 18 Answers Part A 1. a 2. c 3. a 4. d 5. c 6. b 7. a 8. b 9. c 10. d Part B Variable Trace: Output: x y 6,10 -----+----- 3 5 6 10 Part C Here are the correct source code files: # --- Source code file: moviereview.py class MovieReview: def __init__(self, the_title, the_director): self.title = the_title self.director = the_director self.comment = "" # The rating can be between 0.0 and 5.0, inclusive. # A rating of -1 means uninitialized. self.rating = -1 def set_rating(the_rating): if 0.0 < = self.rating and self.rating <= 5.0: self.rating = the_rating else: self.rating = -1 def __str__(self): return '''Title: {0:s} Director: {1:s} Comment: {2:s} Rating: {4:.1f}'''.format(self.title, self.director, \ self.comment, self.rating) # Source code file: test1.rb # Script to test MovieReview class. from moviereview imports MovieReview review = MovieReview("La La Land", "Damien Chazelle") review.comment = "An effervescent sample of pure entertainment." review.set_rating(4.5) print(review) print(review.title, review.director) print(review.comment) print(review.rating) review.set_rating(6.0) print(review.rating) Part D: Variable trace table for Television object tv: self.brand | self.channel | self.volume Output: --------------+----------------+--------------- Soni 2 0 "Soni" 2 0 Soni 11 11 0 0 20 Part E: from television import Television import unittest class MyUnitTestClass(unittest.TestCase): def test_1(self): # To write the unit test, convert the print statements to assert # equal statements of this form: # self.assertEqual(computed, expected) # Leave the other statements as is. tv = Television("Soni") assertEqual(str(tv), "Soni 2 0") assertEqual(tv.brand, "Soni") assertEqual(tv.channel, 2) tv.channel = 11 assertEqual(tv.channel, 11) assertEqual(tv.volume, 0) tv.decrease_volume( ) assertEqual(tv.volume, 0) tv.increase_volume( ) tv.increase_volume( ) assertEqual(tv.volume, 20) if __name__ == '__main__': unittest.main( )