Docsity

Murach’s Python Programming (1st Edition) – Complete Study Notes & Exercises, Exams of Programming Languages

Partial preview of the text

Download Murach’s Python Programming (1st Edition) – Complete Study Notes & Exercises and more Exams Programming Languages in PDF only on Docsity!

Final Exam Review Questions, COP2002 Murach's Python (1st Edition) a Python is considered a good first language to learn because: : it has a simple syntax it has all the features of traditional programming language 5 itis fhe source Which of the following translates bytecode into instructions [or the computer? a. the IDE ¢. the Python virtual machine b. the Python interpreter d. the computer’s operating system The dala in is lost when an application ends. a. the application software c. main memory b. disk storage d. the CPU The data in is persistent so it is not lost when an application ends. a. the application software c. main memory b. Geese d. the CPU The goal of is to find all the errors in a program. a. editing b. testing c. debugging d. interpreting The goal of. is to fix all the errors in a program. a. editing b. testing | debugging d. interpreting Python relies on correct to determine the beginning and end ofa statement. a. continuation a indentation b. punctuation d. comments Which of the following is not an acceptable variable name in Python? a. 3 feet c, thirdFoot b, feet3 d, variable_3feet Given:x = 10 , y=2, z= 3.5 What is the value of new_num after the following statement executes? new_num =x /y * x ANS: 17.5 What is the value of number atfier the following slalement execules? number = (3 ** 3) * ((10 - 2) / 4) ANS: 54 Final Exam Review Questions, COP2002 Given: x = 47 , y = 15, What is the value of new_num aller the following statement executes? new num = x 4 y ANS: 2 Given: x = 47 , y = 15, Whatis the value of new_num aller the following statement executes? new_num = x // y ANS: 3 What is the value of my_num afier the following stalements execute? my_num my_num += 12 my num -= 3 my_num *= 0.5 ANS: 8.5 Assume you want the output of a print statement to read: Jessica said, "Goodbye!" How can you code the quotation marks around the word Goodbye!? use the /" escape sequence use single quotes instead of double quotes this cannot be done in Python Bore What is the argument of the print function in the following Python statement? print("Jane's ID is " + str (5678000) ) Given that pi = 3.1415926535, which of the following code will cause pi to display as: pi = 3.14 ee ¢, Peint ("pi =", float (pi) b. print ("pi =" + round(pi, 2)) eee eee What will be the result of the following code if the user enters 81 al the prompt? score_curve = 7 score = input ("Enter your score on the exam: ") score_curve += score print (score_curve) 88 81 error: ee cannot use the += oc with a variable Which type of expression has a value of either true or false? ANS: Boolean Boon Which is evaluated first when this statement executes? Final Exam Review Questions, COP2002 num += 2 What will be the result of the following code if the user enters 4 at the prompt? answer = 1 value = int(input("Enter a number: ")) for i in range(1, value): answer = answer * i i¢=l print("The result is ", answer) What will be the result of the following code if the user enters 10 at the prompt? entry = 0 value = int(input("Enter a number: ")) for i in range(1, value, 2): entry = entry +i it=1 print("The answer is ", entry) To assign a default value to an argument in a function, in the list of arguments in the function definition, you code code the default value of the argument the name of the argument and set its value on the first line of code inside the function aor send over the value for that argument in the function call A local variable a. is defined inside a function b. can only be used within the function that defines it c. isnot available to the program outside the function that defines it d. | A global variable a. is defined either inside a function or outside all functions b. c. is used for all variables in a program except counters d. all of the above are truc about global variables Amodule a. isa file thal contains three or more functions b. cannot receive data from a program outside that module rc eg ile iia Ga d. all of the above are true about modules Which type of error prevents a program from compiling and running? Final Exam Review Questions, COP2002 a. exceptional c. runtime b. d. logic Which type of error throws an exception that stops execution of the program? a. exceptional ©. runtime b. syntax d. logic Which of the following could possibly result in a value of 0.799? number = random() a. b, number = random.randint(0, 1) c, number = (random.randint (0, 1)) / 100 d, number = random. random() Code Example def get_volume(w, h, 1=6) : answer = w * h* 1 return answer def main(): lgth = 3 wdth = 4 hght = 5 volume = get_volume(1gth, wdth, hght) print (volume) if name == "_main_": main () Refer to Cude Example: What value is passed to h by the call to the get_ volume() function? Refer to Code Example: What is (are) the named argument(s)? ANS: 1 Refer to Cade Example: What is the output of the code? ANS: 60 Final Exam Review Questions, COP2002 Given the following code, if the user bought 12 widgets al $10.00/widgel, the output is as shown below. 1. def main(): widgets = float (input ("How many widgets did you buy? ")) cost = float(input ("What is the cost of a widget? ")) if (widgets > 10) and (cost < 15): your_bill = (widgets * cost) + (widgets - 10 * cost * 1.5) else: your bill = widgets * cost print ("Your cost is: § ", round(your bill, 2)) OIF Hawn Output: Your cost is: $§ -18.0 Which line should be changed to fix this logic error? The method adds an item to the end of a list. ANS: append() The list type is a. Boolean c. mutable b. immutable d. primary When a value is passed by to a function, the function receives its value, but when a value is passed by , the function receives a reference to that valuc in storage. a. reference, value c. acall, an argument b. value, reference d. its name, its value Which of the following statements about list copies is not true? a. When you make a deep copy of a list, both variables refer to their own copy of the list so changes to one variable are not reflected in the other variable. ” Srovahtoucntetst tects nomen c. When you make a shallow copy of a list, each variable refers to the same list so changes to one variable are reflected in the other variable. d. All of the above are truc. Which of the following would creale a list named numbers consisting of 3 floating-point items? a, numbers[1] = 5.3 ¢, numbers = [3] * 0 numbers [2] = 4.8 numbers [3] = 6.7 b, numbers = (5.3, 4.8, 6.71 d. numbers (31 = (5.3 4.8 6.7 Final Exam Review Questions, COP2002 Given the following code, what would the list consist of after the second statement? ages = [22, 35, 24, 17, 28] ages. append (78) ages = [78, 22, 35, 24, 17, 28] ages = [78,78, 78, 78, 78] because the list only contains 5 items, you cannot add a sixth item ae oe Given the following code, what would the list consist of after the second statement? ages = [22, 35, 24, 17, 28] ages. insert (3, 4) a, ages = (22, 35, 24, 4, 17, 28] gages = [22, 35, 24, 17, 3, 28] b. ages = [22, 35, 3, 24, 17, 28] d. [22, 35, 24, 17, 4, 28] What would be displayed after the following code snippet exccutes? costumes = name = "el if name in costumes: "ghost", "witch", "elf", "ogre"] costumes . remove (name) for item in costumes: print (item) a. ¢. "ghost" “witch” ele" “ogre” b, ghost, witch, ogre d, "ghost", "witch", "elf", "ogre" What is the value of the variable result after the following code snippet executes? nums = [6, 10, 20, 9] result = 0 i=0 while i < len(nums) : result += nums[i] itei print (result) Given the tuple shown, which of the following assigns variables to each of the items in the tuple? my tuple = (15, 24, 67, 5) a. for item in my tuple: c. w=my tuple item[i] = my_tuple[il x = my_tuple y = my tuple tuple : b. wx, y, 2% = my tuple-unpack () a Final Exam Review Questions, COP2002 Given the following 2-dimensional list of 3 rows and 3 columns, how would you write this list toa CSV file named books.csv? two d= [["Alice", "Carroll", 15], [Harry Potter", "Rowling", 21], ["Romeo & Juliet", "Shakespeare", 3]] a. open("books.csv", "w", newline ="") as csv_file: writer = csv.writer (file) writer.writerows (two_d) b, with open("two d.csv", "w", newline ) as two d: writer = csv.writer (books.csv) writer.writerows (two _d) c. d, with open("books.csv", "w", newline ="") as two_d: writer = csv.writer(two_d) writer .writerows (books .csv) Which is the most general exception class? a. c. FileNotFoundError b. ValueError d. OSError A(n) exception class defines a more specific exception than its a. parent, child b. child, parent c. object, parent d. parent, object Ina , you include any statements thal may throw an exception in the a. try clause, except clause c. except clause, try clause b. try statement, except clause a The function of the module causes the program to exit. a. exit(), built-in c. e(), exit b. type(), sys a A finally clause a. manually cleans up resources that an object is using. b. is always executed, even if an exception occurs or a return statement is executed in the try block. c. canbe used for objects that don’t define standard clean-up actions. d. Picante canna teed When you raise an exception: a. you force an exception to occur b. you use the raise keyword with the name of the exception you want to raise c. the error message you want to appear is displayed, surrounded by quotes, inside parentheses after the exception name. 10 Final Exam Review Questions, COP2002 Which of the following is not a floating-point number? a. 234.34 b. +683 c. 0.0 d. 2.343e-8 One way to deal with the unexpected results that may come from floating-point numbers is to use rounding, but you can also use decimal numbers by importing the from the a. decimal numbers, Decimal object c. decimal numbers, decimal module b. Decimal class, Decimal object A Decimal stores a decimal number. This is a(n) that is passed to the constructor of the Decimal class. a. object, string c. module, object b. class, number d. object, class While you can add or subtract an int value to or from a Decimal object, you cannot Decimal objects with . a. multiply, ints c. divide, anything b. aS d. mix, the quantize method In the following code, how many decimal places are specified? sales_tax = sales_tax. quantize (Decimal ("1.0000")) b. 3 € 4 a 3 Which of the following will produce the same result as the code shown? p tw import math as m area = m.pi * radius**2 area = (round(m.pi * m.pow(radius, 2)), 2) area = m.pi * radius, 2 area = m(pi * pow(radius, 2)) Bose In the following code, how many decimal places are specified? sales tax = sales_tax.quantize (Decimal ("1.0000")) a 2 b. 3 @ 4 d. on 11 Final Exam Review Questions, COP2002 Code Example 1. phone = input ("Enter phone number: ").strip() 2. if len(phone) == 10: as Phone = "(" + phone[:3] + ")" + phone[3:6] + "-" + phone[6:] 4. print("Phone number: ", phone) 5. else: 6. print ("Phone number: ", "Phone number must be 10 digits") Refer to Code Example. If the user enters two extra spaces after entering a phone number, what will happen’? a. The length of the number will be greater than 10 so the else clause will execute. b. Whitespace at the end of the input is always ignored. c d. The program will pause and wait until the user fixes the entry. Relér to Code Example Il the user enters 555-123-4567 al the prompt, what will happen? a. b. Non-numeric characters will be ignored. c. The.strip() method on line 1 will strip away the extra characters. d. The program will pause and wait until the user fixes the entry. Refer to Code Example If the user enters 5551234567 at the prompt, what will happen? a. Thedisplay will be: Phone number: 5551234567 b. c. The display will be: Phone number: Phone number must be 10 digits. d. The program will pause and wait until the user fixes the entry. Given the following code, what would display aller the code executes? name = "Frankie the Fantastic" words = name.split() print (words[0] + ", you are quite " + words[2].lower()) Fantastic b. Frankie, you are quite Fantastic q. Frankie , you are quite Fantastic A object stores the year, month, and day Lor a date while a object stores the hours, minutes, seconds, and microseconds for a time. a. datctime, time c. date, datetime b. d. constructor, timedelta The method is used lo format a datetime object while the method is used to parse a string to create a datetime object. a. strptime(), strftime() c. b. format(), parse() 13 Final Exam Review Questions, COP2002 It is possible to get the number of days, seconds, or microseconds between (wo date/time objects by subtracting one from the other and storing the results in a(n) variable c. constructor a. b. timedelta object d. timedelta class ‘You can get various parts of a date/time object by accessing ils ‘i a. attributes c. methods b. constructor d. dale object or lime object What would the following code display? from datetime import date this day = date(2020, 5, 6) this _day = this_day.strftime("%B %d, %Y") print ("Today is:", this day) a, Today is: 2020 05 06 b, Today is: June 05, 2020 c What will the following code display if the user enters 29/09/2020 at the prompt? from datetime import datetime today = input("Enter today's date: ") this day = datetime.strptime (today, "sm/%d/%¥") print (this day) a, 2020-29-09 00:00:00 b, 2020/09/29 c. 2020/09/29 00:00:00 d. A dictionary is a collection of items while a list is a collection of items. a. ordered, unordered & b. sequential, unordered d. unordered, element Each entry in a dictionary contains a. ° y a string b. ordered list items d. an array The value for a key in a dictionary can be: a. astring or a number b. another dictionary How many dictionaries named colors exist after the following code executes? colors = {"red": "pink", "white": "ecru", "yellow": "butter"} colors = {} 14