Midterm Review Questions Answers Part A: Multiple Choice Questions 1. d 2. b,d 3. d 4. b 5. c 6. d 7. c 8. c 9. c 10. c 11. c 12. b 13. d 14. d 15. c Part B: Predict the Output 1. a b Output: -----+----- 31 47 8 5 47 31 2. a b a + b Output: -----+-----+----- 4 15 2 5 7 12 60 4 15 17 72 12 60 72 s t Output: 3. ----------------+---------------- ababbabababab "a" "b" "abab" "babababab" Part C: Correct the Errors # 1. # Script with errors: x1 = input("Enter float value x1: ") y1 = 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)) # Corrected script: x1 = input("Enter float value x1: ") y1 = input("Enter float value y1: ") x2 = input('Enter float value x2: ') y2 = input("Enter float value y2: ") distance = ((x2 - x2) ** 2 + (x2 - y2) ** 2) ** (1 / 2) print(f"Distance between points ({x1},{y1}) and ({x2},{y2} is") print(round(distance, 2)) # 2. # Script with errors: 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 # Corrected script: maxpop = -1 maxcity = "" fin = open("cities.txt", "r") # Throw away header line fin.readline( ) line = fin.readline( ) while line != "": fields = line.split(';') city = fields[0].strip( ) state = fields[1].strip( ) population = int(fields[2].strip( )) if state == "MI": if population > maxpop: maxpop = population maxcity = city line = fin.readline( ) if maxpop == -1: print('No cities in input file.') else: print(f"Largest city in MI is {maxcity}.")