takehome-final-exam.txt

IT 212 -- Takehome Final Exam Name _________________ Opens: March 17, 2020 Closes: March 22, 2020 at 11:59 pm. Include this line at the top of your submission: I completed this exam by myself without the help of other persons. Part A. Multiple Choice. 5 points each. Circle the letter of the most correct answer. Give an optional reason for each question for partial credit. If your answer for a question is correct, the reason will not be considered. If you think that none of the answers are correct, show your choice for correct answer. 1. (Python) Which method changes a python int into a string? a. int_to_s b. str c. to_s d. to_string 2. (Python) Which expression does NOT correctly assign a string value to a the variable s? a. s = 'abc\ndef\ghi' b. s = "abc\ndef\ghi" c. s = 'abc d. s = '''abc def def hij' hij''' 3. (Python) If the variables name, gender, and age are defined as in the Person example, which value of format_string is appropriate for this statement? print(format_string.format(name, gender, age)) a. "{0:d} {1:d} {2:d}" b. "{0:s} {1:d} {2:d}" c. "{0:s} {1:s} {2:d}" d. "{0:s} {1:s} {2:s}" 5. (Python) If a is a list variable, which statement returns the length of the list a? a. a.len( ) b. a.length( ) c. len(a) b. length(a) 6. (Python) What is the output? a = [34, 15, 93, 28] a.sort( ) a.insert(2, a.pop(1)) a.reverse( ) print(a) a. [15, 34, 28, 19] b. [93, 34, 29, 93] c. [93, 28, 34, 15] d. [93, 34, 28, 15] 7. (Python) What is the output of the print statement in the testtriple.py module? # ====================================================== # Module pair.py class Pair: def __init__(self, x, y): self.first = x self.second = y def __str__(self): return f"({self.first}, {self.second})" # ====================================================== # Module triple.py from pair import Pair class Triple(Pair): def __init__(self, x, y, z): super( ).__init__(x, y) self.third = z def __str__(self): return f"({self.first}, {self.second}, {self.third})" def __repr__(self): return f"<{self.first}, {self.second}, {self.third}>" # ====================================================== # Module testtriple.py from pair import Pair from triple import Triple t = Triple(3, 5, 7) print(t, str(t), repr(t), [t, t]) # ====================================================== a. (3, 5, 7) (3, 5, 7) (3, 5, 7) [(3, 5, 7), (3, 5, 7)] b. (3, 5, 7) (3, 5, 7) (3, 5, 7) [<3, 5, 7>, <3, 5, 7>] c. (3, 5, 7) (3, 5, 7) <3, 5, 7> [<3, 5, 7>, <3, 5, 7>] d. <3, 5, 7> <3, 5, 7> <3, 5, 7> [(3, 5, 7), (3, 5, 7)] 8. (Python) Which statement inserts a list containing a gender and age using the key name into the dictionary defined by d = { } a. d[name] = [gender, age] b. d.name = (gender, age) c. d.put(name, gender, age) d. d = {name: gender, age} 9. (Python) If the strings r and s are defined by r = 'ab25wyz9utb725cd' s = r'\d+' what is the output? import re print(re.findall(r, s)) a. ['ab', 'wyz', 'utb', 'cd'] b. ['ab', '25', 'wyz', '9', 'utb', '725', 'cd'] c. ['25', '9', '725'] d. [25, 9, 725] 10. (Python) If the strings r and s are defined by r = 'ab25wyz9utb725cd' s = r'\d+' what is the output? import re print(re.split(r, s)) a. ['ab', 'wyz', 'utb', 'cd'] b. ['ab', '25', 'wyz', '9', 'utb', '725', 'cd'] c. ['25', '9', '725'] d. [25, 9, 725] 11. (Python) If the objects b and c are defined as b = sqlite3.connect('persons.db') c = b.cursor( ) which statement is correct for adding a row to the persons table: a. c.execute('insert into kids values(Alice, F, 11);') b. c.execute('insert into kids values("Alice", "F", 11);') c. c.execute("insert into kids values('Alice', 'F', '11');") d. c.execute("insert into kids values('Alice', 'F', 11);") 12. Which statement loads all of the rows for boys younger than ten years old into the cursor object c defined in Problem 11? a. cur.execute("select * from kids if gender='M' and age < 10;") b. cur.execute('select * from kids if gender="M" and age < 10;') c. cur.execute("select * from kids where gender='M' and age < 10;") d. cur.execute('select * from kids where gender="M", age < 10;') 13. (Java) Which token marks a line as a comment? a. ## b. // c. %% d. << 14. (Python, Java) Translate these Python statement into a Java for loop: n = 7; while n <= 1000: print(n, end=" ") n *= 3 a. for(int n = 3; n *= 7; n <= 1000) { System.out.print(n); } b. for(int n = 3; n <= 1000; n *= 7) { System.out.print(n); } c. for(int n = 7; n *= 3; n <= 1000) { System.out.print(n); } d. for(int n = 7; n <= 1000; n *= 3) { System.out.print(n); } 15. Which choice is a correct Java translation of this Python method? def inches_to_cm(inches): return inches * 2.54 a. public double inchesToCm(double inches) { return inches * 2.54; } b. public double inchesToCm(inches) { return inches * 2.54; } c. public void inchesToCm(double inches) { return inches * 2.54; } d. public void inchesToCm(inches) { return inches * 2.54; } Part B: Short Essay -- 15 points. Only submit 1 essay out of 3. About one half of a page to one page. If you use information from the Internet, give references. 1. Explain the differences and similarities between Python and Java. 2. Explain what inheritance is. Why is it useful in object oriented programming? 3. Explain what Python dunder (or magic) methods are. How are they useful in Python classes? Give some examples. Part C: Find Errors in Python Source Code -- 15 points. Correct the errors in the source code files vehicle.py, airplane.py, and test.py. There are about 5 or 6 errors in each source code file (15 to 18 total errors). Submit an error log listing the errors that you find by line number, and the 3 corrected classes. The error log is like what you submitted for Lab 1. Correcting a pair of ( ), [ ], { }, "", or '' only counts as one error. 15 points.) # ============================================================= # Vehicle Class, source code module: vehicle.py import re import sys import datetime class Vehicle: def __init_(self, id_num, year): if search(r'\A[A-Z]+\d+\Z', id_num) == None: print('Illegal id_num') sys.exit( ) else: self.id_num = id_num now = datetime.datetime.now( ) if year > now.year print('Illegal year') sys.exit( ) else: self.year = year def __str__( ): return self.id_num + self.year # ============================================================= # Automobile Class, source code module automobile.py. import vehicle from Vehicle class Airplane(vehicle) def __init__(self, id_num, year, color): super(self).__init__(id_num, year) self.year = year self.color = color self.speed = 0 self.altitude = 0 def __str__(self): return super( ).__str__( ) + ' ' + self.color def __repr__(self): return f'<{self}>' def accelerate(self): self.speed += 60 def decelerate(self): if self.speed > 60: self.speed - = 60 else: self.speed = 0 def climb(self): self.altitude -= 500 def dive(self): if self.altitude > 500: self.altitude -= 500 else: self.altitude = 0 # ============================================================= # __main__ module, source code module testairplane.py from vehicle import Vehicle from Airplane import airplane a1 = Airplane('SG1827' 2009, 'red') a2 = Airplane('WB3827', 2012, 'blue') a3 = Airplane('RX2890', 2011, 'white') a4 = Airplane('QU1753', 2008, yellow) airplanes = {a1, a2, a3, a4} for a in airplanes: for i in range(0, 2): a.accelerate( ) a.climb( ) for a in airplanes: print("{a} speed:{a.speed}, altitude:{a.altitude}") print airplanes[1] for i in range(0, 3): airplanes[i].accelerate( ) airplanes[i].climb( ) print(airplanes[1].speed( ), airplanes[1].altitude) # ============================================================= Part D: Find Errors in Java Source Code -- 10 points. There are between 8 and 10 errors in the errors in the TipCalculator.java file below. The original tipcalculator.py module with no errors is also shown for reference. Submit an error log, where you list the errors by line number, and also the corrected source code file. The error log and corrected source code are like what you submitted for Lab 1. Correcting a pair of ( ), [ ], { }, "", or '' only counts as one error. 15 points. // ************************************************************************ // TipCalculator class // Source code file TipCalculator.java import java.Scanner; public class TipCalculator { public static double calculateTip(double checkAmount double tipPercent) { double tipAmount = checkAmount * tipPercent * 0.01; // Amount of tip is always >= $1.00 if tipAmount > 1.00 { return tipAmount; } else { return 1.00; } public void static Main(String args) { Scanner input = new Scanner(System.in); System.out.print("Input amount of check: "); double amt = Double.parseDouble(input.nextLine( )); if (amt < 0.0) { System.out.println( "Amount of check must be nonnegative."); System.exit(0); } System.print("Input tip percent: "); double percent = Double.parseDouble(input.nextLine); if (percent < 0.0) { System.out.println("Tip percent must be nonnegative."); System.exit( ); } System.out.println("Tip amount: $" + calculateTip(amt, percent)); } } // ************************************************************************ # ************************************************************************* # tipcalculator.py module with no errors import sys def calculate_tip(check_amount, tip_percent): tip_amount = check_amount * tip_percent * 0.01 # Amount of tip is always >= $1.00 if tip_amount > 1.00: return tip_amount else: return 1.00 amt = float(input('Input amount of check: ')) if amt < 0.0: print('Amount of check must be nonnegative.') sys.exit( ) percent = float(input('Input tip percent: ')) if percent < 0.0: print('Tip percent must be nonnegative.') sys.exit( ) print("Tip amount:", calculate_tip(amt, percent)) # *************************************************************************