INFO 1200 Final

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

Code Example 4-3 main program: import arithmetic as a def main(): num1 = 5 num2 = 6 result = a.add(num1, num2) print("The sum is", result) if __name__ == "__main__": main() arithmetic module: def add(x = 4, y = 2): z = x + y return z Refer to Code Example 4-3: What values are in x and y after the code runs? A. 9, 8 B. 5, 6 C. 4, 2 D. 20, 12

B. 5, 6

Code Example 6-1 def main(): students = [["Lizzy", 73, "C"], ["Mike", 98, "A"], ["Joel", 88, "B+"], ["Anne", 93, "A"]] for student in students: for item in student: print(item, end=" ") if __name__ == "__main__": main() Refer to Code Example 6-1: What is the value of students[2][1]? A. Joel B. 88 C. Joel, 88, B+ D. B+

B. 88

What line number of the following code contains an error and what type of error is it? 1. def sales_tax(amt) 2. sale = amt + (amt * .06) 3. return sale 4. 5. def main(): 6. print("Welcome to the 6% tax calculator!\n") 7. total = int(input("Please enter the total amount: ")) 8. print("The total amount after tax is: ", sales_tax(total)) A. line 1, runtime error B. line 1, syntax error C. line 3, runtime error D. line 8, logic error

B. line 1, syntax error

Code Example 18-2 (too long, 30 lines source code) Refer to Code Example 18-2: What's the number for the statement that displays the component that allows the user to enter the meal cost? A. 10 B. 11 C. 13 D. 20

B. 11

What is the value of my_num after the following statement executes? my_num = (50 + 2 * 10 - 4) / 2 A. 258 B. 33 C. 156 D. 29

B. 33

What is the value of the total variable after the following code executes? prices = [10, 15, 12, 8] total = 0 i = 1 while i < len(prices): total += prices[i] i += 1 print(total) A. 0 B. 35 C. 8 D. 45

B. 35

Code Example 4-1 def get_username(first, last): s = first + "." + last return s.lower() def main(): first_name = input("Enter your first name: ") last_name = input("Enter your last name: ") username = get_username(first_name, last_name) print("Your username is: " + username) if __name__ == "__main__": main() Refer to Code Example 4-1: What is the scope of the variable named s? A. global B. local C. global in main() but local in get_username() D. local in main() but global in get_username()

B. local

The stack is available when an exception occurs. It displays a list of A. the functions that have been called since the program started B. just the functions that were called prior to the exception C. all the local variables used by the program and their values D. all the local and global variables used by the program and their values

B. just the functions that were called prior to the exception

Code Example 6-1 def main(): students = [["Lizzy", 73, "C"], ["Mike", 98, "A"], ["Joel", 88, "B+"], ["Anne", 93, "A"]] for student in students: for item in student: print(item, end=" ") if __name__ == "__main__": main() Refer to Code Example 6-1: What is in the second row of the students list? A. "Mike", 98, "A" B. "Joel", 88, "B+" C. 73, 98, 88, 93 D. "C", "A", "B+", "A"

A. "Mike", 98, "A"

Code Example 18-1 import tkinter as tk from tkinter import ttk root = tk.Tk() root.title("Escape from the Maze") root.geometry("600x300") frame = ttk.Frame(root, padding="200 100 200 100") frame.pack(fill=tk.BOTH, expand=True) def click_button1(): root.title("Wrong way!") def click_button2(): root.destroy() button1 = ttk.Button(frame, text="Go Left", command=click_button1) button2 = ttk.Button(frame, text="Go Right", command=click_button2) button1.pack() button2.pack() root.mainloop() Refer to Code Example 18-1: How big is the window that this code displays? A. 600 pixels wide by 300 pixels high B. 600 characters wide by 300 characters high C. 200 pixels wide by 100 pixels high D. 200 characters wide by 100 characters high

A. 600 pixels wide by 300 pixels high

To call a function with named arguments, you code the: A. name of each argument, an equals sign, and the value or variable that's being passed B. values that you're passing in the same sequence that the names are defined in the function C. values that you're passing at the beginning of the function call D. values that you're passing at the end of the function call, followed by the names that correspond with the values

A. name of each argument, an equals sign, and the value or variable that's being passed

To create a root window, you can: A. call the title() method B. call the geometry() method C. call the mainloop() method D. call the Tk() constructor

D. call the Tk() constructor

To create a Python program, you use: A. IDLE's editor B. IDLE's interactive shell C. the F5 key D. the command line

A. IDLE's editor

A for loop that uses a range() function is executed: A. once for each integer in the collection returned by the range() function B. until the for condition is equal to the value returned by the range() function C. while the for condition is less than the value returned by the range() function D. until the range() function returns a false value

A. once for each integer in the collection returned by the range() function

A web application runs: A. through a browser B. via a command prompt C. with a GUI D. through another application

A. through a browser

Code Example 5-2 1. # This application displays a student's score after a 5-point curve 2. 3. def display_info(fname, lname, score): 4. print("Hello, " , fname, " " , Lname) 5. score = score + 5 6. print("Your score on this exam is ", score) 7. 8. def main(): 9. first = input("first name: ") 10. last = input("last name: ") 11. grade = input("exam score: ") 12. display_info(last, first, score) 13. 14. # if started as the main module, call the main function 15. if __name__ == "__main__": 16. main() Refer to Code Example 5-2: Assuming the coding errors have been fixed, what is the logic error in this program? A. The arguments are listed in the wrong order. B. The curve is calculated after the score has been displayed. C. The display_info() function should be called before the input statements on lines 9-11. D. There is no logic error in this program.

A. The arguments are listed in the wrong order.

Code Example 5-2 1. # This application displays a student's score after a 5-point curve 2. 3. def display_info(fname, lname, score): 4. print("Hello, " , fname, " " , Lname) 5. score = score + 5 6. print("Your score on this exam is ", score) 7. 8. def main(): 9. first = input("first name: ") 10. last = input("last name: ") 11. grade = input("exam score: ") 12. display_info(last, first, score) 13. 14. # if started as the main module, call the main function 15. if __name__ == "__main__": 16. main() Refer to Code Example 5-2: What is the error in the main() function? A. The input statement on line 11 gets a variable named grade but sends in an undefined variable named score on line 12 B. The input statement on line 11 does not define grade as an int C. The function call on line 12 should send in fname and lname as arguments, not last and first D. There are no errors in main()

A. The input statement on line 11 gets a variable named grade but sends in an undefined variable named score on line 12

Code Example 7-1 import csv def main(): courses = [["Python", 3], ["Trig", 3], ["Physics", 4], ["Yoga", 2]] with open("courses.csv", "w", newline="") as file: writer = csv.writer(file) writer.writerows(courses) course_list = [] with open("courses.csv", newline="") as file: reader = csv.reader(file) for row in reader: course_list.append(row) for i in range(len(course_list) - 2): course = course_list[i] print(f"{course[0]} ({course[1]})") main() Refer to Code Example 7-1. If the first with open statement works, what is written to the file? A. The list named courses. B. The first list in the list named courses. C. The first row in the list named courses. D. The first column in the first row in the list named courses.

A. The list named courses.

Code Example 4-3 main program: import arithmetic as a def main(): num1 = 5 num2 = 6 result = a.add(num1, num2) print("The sum is", result) if __name__ == "__main__": main() arithmetic module: def add(x = 4, y = 2): z = x + y return z Refer to Code Example 4-3: What will be displayed after the code runs? A. The sum is 11 B. The sum is 6 C. The sum is 17 D. Nothing, the code causes an error

A. The sum is 11

Code Example 3-1 num_widgets = 0 while True: choice = input("Would you like to buy a widget? (y/n): ") if choice.lower() == "y": num_widgets += 1 else: break print("You bought", num_widgets , "widget(s).") Refer to Code Example 3-1. If the user enters "no" at the prompt, what does the program print to the console? A. You bought 0 widget(s) today. B. You bought 1 widget(s) today. C. You bought no widget(s) today. D. Nothing, the break statement will cause the program to end

A. You bought 0 widget(s) today.

Which of the following can use the range() function to loop through a block of statements? A. a for statement B. an if statement C. a while statement D. a break statement

A. a for statement

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] B. ages = [22, 35, 3, 24, 17, 28] C. ages = [22, 35, 24, 17, 3, 28] D. ages = [22, 35, 24, 17, 4, 28]

A. ages = [22, 35, 24, 4, 17, 28]

In a while loop, the Boolean expression is tested: A. before the loop is executed B. after the loop is executed C. both before and after the loop is executed D. until it equals the current loop value

A. before the loop is executed

To lay out components in a series of rows and columns, you can use the __________ method. A. grid() B. pack() C. table() D. layout()

A. grid()

The and operator has: A. higher precedence than the or operator, but lower precedence than the not operator B. higher precedence than the or and not operators C. lower precedence than the or operator, but higher precedence than the not operator D. lower precedence than the or and not operators

A. higher precedence than the or operator, but lower precedence than the not operator

Code Example 7-2 import pickle def main(): courses = [["Python", 3], ["Trig", 3], ["Physics", 4], ["Yoga", 2]] with open("classes.bin", "wb") as file: pickle.dump(courses, file) with open("classes.bin", "rb") as file: course_list = pickle.load(file) i = 0 while i < len(course_list): course = course_list[i] print(course[0], course[1], end=" ") i += 2 main() Refer to Code Example 7-2: What does the first with open statement do? A. writes the courses list to a binary file if the file named classes.bin doesn't exist B. causes an exception if the file named classes.bin doesn't exist C. writes the courses list to a binary file if the file named courses.bin doesn't exist D. causes an exception if the file named courses.bin doesn't exist

A. writes the courses list to a binary file if the file named classes.bin doesn't exist

To read the rows in a CSV file, you need to: A. get a reader object by using the reader() function of the file object B. get a reader object by using the reader() function of the csv module C. get a row object by using the row() function of the file object D. get a rows object by using the rows() function of the file object

B. get a reader object by using the reader() function of the csv module

You can use a StringVar object to: A. create a read-only component. B. get and set the text in a component. C. set the width of a component. D. connect a string to a variable.

B. get and set the text in a component.

The finally clause of a try statement: A. is required B. is executed whether or not an exception has been thrown C. can be used to display more information about an exception D. can be used to recover from an exception

B. is executed whether or not an exception has been thrown

To test a Python statement, you use: A. IDLE's editor B. IDLE's interactive shell C. the F5 key D. the command line

B. IDLE's interactive shell

Code Example 18-2 (too long, 30 lines source code) Refer to Code Example 18-2: What does the GUI do if the user enters the cost of the meal and the tip percent and clicks the Calculate button? A. It calculates the amount of a tip to leave based on the user's input, but it can't display it because the Entry text field is read only. B. It calculates and displays the amount of a tip to leave based on the user's input. C. It calculates and displays the total amount to leave based on the user's input. D. It doesn't do anything because the calculate() method isn't connected to the Calculate button.

B. It calculates and displays the amount of a tip to leave based on the user's input.

Code Example 5-2 1. # This application displays a student's score after a 5-point curve 2. 3. def display_info(fname, lname, score): 4. print("Hello, " , fname, " " , Lname) 5. score = score + 5 6. print("Your score on this exam is ", score) 7. 8. def main(): 9. first = input("first name: ") 10. last = input("last name: ") 11. grade = input("exam score: ") 12. display_info(last, first, score) 13. 14. # if started as the main module, call the main function 15. if __name__ == "__main__": 16. main() Refer to Code Example 5-2: What is the first error in the display_info() function? A. The arguments on line 3 should be first and last, not fname and lname B. The variable Lname on line 4 does not exist C. The print statement on line 4 should use the + operator instead of commas D. You cannot add a number to itself, as on line 6

B. The variable Lname on line 4 does not exist

A function that's called when a user clicks on a button is commonly known as: A. a click function B. a callback function C. a GUI function D. an event caller

B. a callback function

To define a function, you code the def keyword and the name of the function followed by: A. a set of parentheses B. a set of parentheses that contains zero or more arguments C. a set of parentheses that contains one or more arguments D. a set of parentheses that contains a list of the local variables

B. a set of parentheses that contains zero or more arguments

What, if anything, is wrong with this code? rating = input("Enter the rating for this product: ") rating = rating + 2 print("The adjusted rating is " + rating + ".") A. nothing is wrong with this code B. a string variable is used in an arithmetic expression C. the coding in the print() function contains illegal plus signs D. the input() function should be an int() function

B. a string variable is used in an arithmetic expression

Code Example 18-2 (too long, 30 lines source code) Refer to Code Example 18-2: The third Entry component has a state of "readonly". As a result, it creates an entry field that: A. can only read a user's entry B. can only display text for the user to read C. reads data directly from a file D. is disabled

B. can only display text for the user to read

Code Example 7-2 import pickle def main(): courses = [["Python", 3], ["Trig", 3], ["Physics", 4], ["Yoga", 2]] with open("classes.bin", "wb") as file: pickle.dump(courses, file) with open("classes.bin", "rb") as file: course_list = pickle.load(file) i = 0 while i < len(course_list): course = course_list[i] print(course[0], course[1], end=" ") i += 2 main() Refer to Code Example 7-2: What does the second with open statement do? A. reads the file named classes.bin into the list named courses B. causes an exception if the file named classes.bin doesn't exist C. reads the list named courses into the list named course_list D. creates an empty list if the file named classes_bin doesn't exist

B. causes an exception if the file named classes.bin doesn't exist

Which of the following statements about list copies is not true? When you make a: A. deep copy of a list, both variables refer to their own copy of the list. B. deep copy of a list, both variables refer to the same list. C. shallow copy of a list, both variables refer to the same list. D. shallow copy of a list, the list is immutable.

B. deep copy of a list, both variables refer to the same list.

The data in __________ is persistent so it is not lost when an application ends. A. the application software B. disk storage C. main memory D. the CPU

B. disk storage

Code Example 18-1 import tkinter as tk from tkinter import ttk root = tk.Tk() root.title("Escape from the Maze") root.geometry("600x300") frame = ttk.Frame(root, padding="200 100 200 100") frame.pack(fill=tk.BOTH, expand=True) def click_button1(): root.title("Wrong way!") def click_button2(): root.destroy() button1 = ttk.Button(frame, text="Go Left", command=click_button1) button2 = ttk.Button(frame, text="Go Right", command=click_button2) button1.pack() button2.pack() root.mainloop() Refer to Code Example 18-1: Which code snippet adds the frame to the root window? A. root = tk.Tk() root.title("Escape from the Maze") root.geometry("600x300") B. frame = ttk.Frame(root, padding="200 100 200 100") frame.pack(fill=tk.BOTH, expand=True) C. import tkinter as tk from tkinter import ttk D. root.mainloop()

B. frame = ttk.Frame(root, padding="200 100 200 100") frame.pack(fill=tk.BOTH, expand=True)

To compare two strings with mixed uppercase and lowercase letters, a programmer can first use the: A. upper() method to convert the first character in each string to uppercase. B. lower() method to convert all characters in each string to lowercase. C. lower() method to convert all characters after the first character in each string to lowercase. D. upper() method to convert all characters after the first character in each string to uppercase.

B. lower() method to convert all characters in each string to lowercase.

Given that pi = 3.1415926535, which of the following print() functions displays: pi = 3.14 A. print("pi= ", round(pi, 2)) B. print("pi = " + round(pi, 2)) C. print("pi = ", float(pi, 2)) D. print("pi = ", round(pi))

B. print("pi = " + round(pi, 2))

To throw an exception with Python code, you use the: A. throw statement B. raise statement C. built-in throw() function D. build-in raise() function

B. raise statement

The goal of __________ is to find all the errors in a program. A. editing B. testing C. debugging D. interpreting

B. testing

Which of the following is the correct way to code a try statement that displays the type and message of the exception that's caught? A. try: number = int(input("Enter a number: ")) print("Your number is: ", number) except Exception as e: print(e(type), e(message)) B. try: number = int(input("Enter a number: ")) print("Your number is: ", number) except Exception as e: print(type(e), e) C. try: number = int(input("Enter a number: ")) print("Your number is: ", number) except Exception: print(Exception(type), Exception(message)) D. try: number = int(input("Enter a number: ")) print("Your number is: ", number) except Exception: print(type(Exception), Exception)

B. try: number = int(input("Enter a number: ")) print("Your number is: ", number) except Exception as e: print(type(e), e)

A console application runs: A. through a browser B. via a command prompt C. with a GUI D. through another application

B. via a command prompt

Code Example 18-2 (too long, 30 lines source code) Refer to Code Example 18-2: In this GUI, the components in the frame are organized in a grid that has A. 1 column and 3 rows B. 2 columns and 3 rows C. 2 columns and 4 rows D. 3 columns and 4 rows

C. 2 columns and 4 rows

Given: x = 7 , y = 2 , z = 1.5 What is the value of new_num after the following statement executes? new_num = x / y + z A. 2 B. 4.5 C. 5.0 D. 3

C. 5.0

Code Example 18-2 (too long, 30 lines source code) Refer to Code Example 18-2: How many visible components does this code display in the window? A. 5 B. 6 C. 7 D. 8

C. 7

The goal of __________ is to fix all the errors in a program. A. Editing B. Testing C. Debugging D. Interpreting

C. Debugging

Code Example 8-1 import csv import sys FILENAME = "names.csv" def main(): try: names = [] with open(FILENAME, newline="") as file: reader = csv.reader(file) for row in reader: names.append(row) except FileNotFoundError as e: print(f"Could not find {FILENAME} file.") sys.exit() except Exception as e: print(type(e), e) sys.exit() print(names) if __name__ == "__main__": main() Refer to Code Example 8-1. If the names.csv file is not in the same directory as the file that contains the Python code, what type of exception will be thrown and caught? A. Exception B. OSError C. FileNotFoundError D. All of the above

C. FileNotFoundError

If a program attempts to read from a file that does not exist, which of the following will catch that error? A. FileNotFoundError and ValueError B. FileNotFoundError and NameError C. FileNotFoundError and OSError D. NameError and OSError

C. FileNotFoundError and OSError

Code Example 8-1 import csv import sys FILENAME = "names.csv" def main(): try: names = [] with open(FILENAME, newline="") as file: reader = csv.reader(file) for row in reader: names.append(row) except FileNotFoundError as e: print(f"Could not find {FILENAME} file.") sys.exit() except Exception as e: print(type(e), e) sys.exit() print(names) if __name__ == "__main__": main() Refer to Code Example 8-1. If the for statement in the try clause refers to readers instead of reader, what type exception will be thrown and caught? A. Exception B. SyntaxError C. NameError D. ReferenceError

C. NameError

Code Example 5-2 1. # This application displays a student's score after a 5-point curve 2. 3. def display_info(fname, lname, score): 4. print("Hello, " , fname, " " , Lname) 5. score = score + 5 6. print("Your score on this exam is ", score) 7. 8. def main(): 9. first = input("first name: ") 10. last = input("last name: ") 11. grade = input("exam score: ") 12. display_info(last, first, score) 13. 14. # if started as the main module, call the main function 15. if __name__ == "__main__": 16. main() Refer to Code Example 5-2: What is the error on line 6? A. You cannot change the value of score after it has been displayed. B. You cannot add a number to itself. C. The variable score has been input as a string so it must be converted to an int or float. D. There is no error on line 6

C. The variable score has been input as a string so it must be converted to an int or float.

Which of the following is not true about a CSV file? A. Each row or record in the file usually ends with a new line character. B. The columns or fields are usually separated by commas. C. The csv module is a standard module so you don't need to import it D. To write data to a file, you need to get a writer object.

C. The csv module is a standard module so you don't need to import it

For the following code, what will the result be if the user enters 4 at the prompt? product = 1 end_value = int(input("Enter a number: ")) for i in range(1, end_value): product = product * i i += 1 print("The product is ", product) A. The product is 1 B. The product is 4 C. The product is 6 D. The product is 24

C. The product is 6

Which of the following is not true about a list of lists? A. You can use nested for statements to loop through the items in a list of lists. B. You can refer to an item in an inner list by using two indexes. C. To delete an item in the outer list, you first have to delete the list in the item. D. The inner lists and the outer list are mutable.

C. To delete an item in the outer list, you first have to delete the list in the item.

What will display after the following code executes? def add_item(list, food): food = "apple pie" list.append(food) def main(): lunch = ["sandwich", "chips", "pickle"] food = "banana" add_item(lunch, food) print(lunch) main() A. ['sandwich', 'chips', 'pickle', 'banana'] B. ['sandwich', 'chips', 'pickle', 'banana', 'apple pie'] C. ['sandwich', 'chips', 'pickle', 'apple pie'] D. Error: list is undefined

C. ['sandwich', 'chips', 'pickle', 'apple pie']

Code Example 7-1 import csv def main(): courses = [["Python", 3], ["Trig", 3], ["Physics", 4], ["Yoga", 2]] with open("courses.csv", "w", newline="") as file: writer = csv.writer(file) writer.writerows(courses) course_list = [] with open("courses.csv", newline="") as file: reader = csv.reader(file) for row in reader: course_list.append(row) for i in range(len(course_list) - 2): course = course_list[i] print(f"{course[0]} ({course[1]})") main() Refer to Code Example 7-1. What happens if the courses.csv file doesn't exist when the first with open statement is executed? A. the program crashes B. an exception is thrown but the program doesn't crash C. a new file named courses.csv is created D. all the resources for the file are released

C. a new file named courses.csv is created

Which of the following begins by testing a condition defined by a Boolean expression and then executes a block of statements if the condition is true? A. a for statement B. a switch statement C. a while statement D. a continue statement

C. a while statement

The best way to call the main() function of a program is to code: A. main() B. an if statement that calls the main() function only if the function exists C. an if statement that calls the main() function only if the current module is the main module D. a while statement that calls the main() function in each loop

C. an if statement that calls the main() function only if the current module is the main module

To jump to the end of the current loop, you can use the: A. end statement B. continue statement C. break statement D. switch statement

C. break statement

To assign a default value to an argument when you define a function, you: A. code the default value instead of its name in the arguments list B. set the default value for the argument in the first line of code inside the function C. code the name of the argument, the assignment operator (=), and the default value D. code the name of the argument, the default operator (:), and the default value

C. code the name of the argument, the assignment operator (=), and the default value

To work with a file when you're using Python, you must do all but one of the following. Which one is it? A. open the file B. write data to or read data from the file C. decode the data in the file D. close the file

C. decode the data in the file

The following is an example of __________. #!/usr/bin/env python 3 A. Java code B. bytecode C. source code D. a shebang line

D. a shebang line

When you trace the execution of a program, you insert print() functions at key points in the program. It makes sense for these functions to do all but one of the following. Which one is it? A. display the name of the function that the print() function is in B. display the values of the local variables in the function C. display the values of the global constants used by the function D. display the values of the global variables used by the function

C. display the values of the global constants used by the function

Code Example 18-1 import tkinter as tk from tkinter import ttk root = tk.Tk() root.title("Escape from the Maze") root.geometry("600x300") frame = ttk.Frame(root, padding="200 100 200 100") frame.pack(fill=tk.BOTH, expand=True) def click_button1(): root.title("Wrong way!") def click_button2(): root.destroy() button1 = ttk.Button(frame, text="Go Left", command=click_button1) button2 = ttk.Button(frame, text="Go Right", command=click_button2) button1.pack() button2.pack() root.mainloop() Refer to Code Example 18-1: In this program, the mainloop() function: A. executes the main() function of the GUI B. connects the two functions to the two buttons C. displays the GUI and starts a loop that listens for events D. loops through all of the components in the GUI and adds padding to them

C. displays the GUI and starts a loop that listens for events

What will be the result of the following code if the user enters 81 at the prompt? score_curve = 7 score = input("Enter your score on the exam: ") score_curve += score print(score_curve) A. 88 will be displayed on the console B. 81 will be displayed on the console C. error: you cannot use the += operator to add a string variable to an int value D. error: you cannot print a numeric variable

C. error: you cannot use the += operator to add a string variable to an int value

To test the functions of a module from the IDLE shell, you: A. run the module with varying input values B. run the module and then call any function from the IDLE shell C. import the module and then call any function from the IDLE shell D. call any function in the module by using the default namespace

C. import the module and then call any function from the IDLE shell

Python relies on correct __________ to determine the meaning of a statement. A. continuation B. punctuation C. indentation D. comments

C. indentation

Code Example 4-1 def get_username(first, last): s = first + "." + last return s.lower() def main(): first_name = input("Enter your first name: ") last_name = input("Enter your last name: ") username = get_username(first_name, last_name) print("Your username is: " + username) if __name__ == "__main__": main() Refer to Code Example 4-1: If the user enters 'Lopez' for the first prompt in main() and 'Maria' for the second prompt, what will display? A. Maria.Lopez B. maria.lopez C. lopez.maria D. Lopez.Maria

C. lopez.maria

Which of the following will get a floating-point number from the user? A. my_number = input(float("Enter a number:")) B. my_number = float(input "Enter a number:") C. my_number = float(input("Enter a number:")) D. my_number = input("Enter a number:")float_num = my_number

C. my_number = float(input("Enter a number:"))

What will be displayed after the following code executes? guess = 19 if guess < 19: print("Too low") elif guess > 19: print("Too high") A. Too low B. Too high C. nothing will display

C. nothing will display

Which type of error throws an exception that stops execution of the program? A. exceptional B. syntax C. runtime D. logic

C. runtime

When two strings are compared, they are evaluated based on the ___________________ of the characters in the string. A. uppercase/lowercase sequence B. alphabetical order C. sort sequence D. string method

C. sort sequence

To run a Python program from IDLE, you use: A. IDLE's editor B. IDLE's interactive shell C. the F5 key D. the command line

C. the F5 key

Which of the following translates bytecode into instructions for the computer? A. the IDE B. the Python interpreter C. the Python virtual machine D. the computer's operating system

C. the Python virtual machine

When an exception occurs while a program is running, A. the program crashes B. an error message is displayed on the console C. the program crashes and an error message is displayed D. an error message is displayed but the program continues

C. the program crashes and an error message is displayed

Given the following 2-dimensional list of 3 rows and 3 columns, how would you write this list to a CSV file named prog.csv? programming = [["Python", "cop1000", 3], ["Java", "cop1020", 3], ["HTML5", "cop1040", 3]] A. open("prog.csv", "w", newline ="") as csv_file:writer = csv.writer(file) writer.writerows(programming) B. with open("programming.csv", "w", newline ="") as programming:writer = csv.writer(prog.csv) writer.writerows(programming) C. with open("prog.csv", "w", newline ="") as file:writer = csv.writer(file) writer.writerows(programming) D. with open("prog.csv", "w", newline ="") as programming:writer = csv.writer(programming)writer.writerows(prog.csv)

C. with open("prog.csv", "w", newline ="") as file:writer = csv.writer(file) writer.writerows(programming)

A Python program should use try statements to handle: A. all exceptions that might be thrown by a program B. only the exceptions related to file and database I/O C. all the exceptions that aren't caused by coding errors D. all exceptions that can't be prevented by normal coding techniques

D. all exceptions that can't be prevented by normal coding techniques

What is the argument of the print() function in the following Python statement? print("My student ID is " + str(123456)) A. 123456 B. str(123456) C. "My student ID is " D. "My student ID is " + str(123456)

D. "My student ID is " + str(123456)

Given: x = 23 , y = 15 What is the value of new_num after the following statement executes? new_num = x % y A. 1 B. 1.533333 C. 0.533333 D. 8

D. 8

Python comments A. are ignored by the compiler B. can be used to document what a program or portion of code does C. can be used so certain lines of code are not executed during testing D. all of the above

D. all of the above

Python is considered a good first language to learn because: A. it has a simple syntax B. it has most of the features of traditional programming languages C. it is open source D. all of the above

D. all of the above

A runtime error is also known as: A. a syntax error B. a logical error C. a violation D. an exception

D. an exception

Code Example 7-1 import csv def main(): courses = [["Python", 3], ["Trig", 3], ["Physics", 4], ["Yoga", 2]] with open("courses.csv", "w", newline="") as file: writer = csv.writer(file) writer.writerows(courses) course_list = [] with open("courses.csv", newline="") as file: reader = csv.reader(file) for row in reader: course_list.append(row) for i in range(len(course_list) - 2): course = course_list[i] print(f"{course[0]} ({course[1]})") main() Refer to Code Example 7-1. What will display on the console after the code executes? A. Python 3 Trig 3 Physics 4 Yoga 2 B. Python (3) Trig (3) Physics (4) Yoga (2) C. Python 3 Trig 3 D. Python (3) Trig (3)

D. Python (3) Trig (3)

Code Example 7-2 import pickle def main(): courses = [["Python", 3], ["Trig", 3], ["Physics", 4], ["Yoga", 2]] with open("classes.bin", "wb") as file: pickle.dump(courses, file) with open("classes.bin", "rb") as file: course_list = pickle.load(file) i = 0 while i < len(course_list): course = course_list[i] print(course[0], course[1], end=" ") i += 2 main() Refer to Code Example 7-2: What is displayed on the console by the while loop? A. Python 3 Trig 3 Physics 4 Yoga 2 B. Python 3 Trig 3 Physics 4 Yoga 2 C. Python 3 Physics 4 D. Python 3 Physics 4

D. Python 3 Physics 4

Which of the following is not true of hierarchy charts? A. Function names should start with a verb and indicate what the functions do. B. Each function should do only what is related to the function name. C. The top level box should be for the main() function. D. Related functions should be combined into a single function.

D. Related functions should be combined into a single function.

Code Example 4-4 main program: import arithmetic as a def multiply(num1, num2): product = num1 * num2 result = a.add(product, product) return result def main(): num1 = 4 num2 = 3 answer = multiply(num1, num2) print("The answer is", answer) if __name__ == "__main__": main() arithmetic module: def add(x, y): z = x + y return z Refer to Code Example 4-4: When this code runs, what does it print to the console? A. The answer is 28 B. The answer is 12 C. The answer is 7 D. The answer is 24

D. The answer is 24

Given the following code, what would be displayed after the code executes? def main(): furry_pets = ["dog", "cat", "ferret", "hamster", "bunny"] feathered_pets = ["canary", "parrot", "budgie", "hawk"] all_pets = furry_pets + feathered_pets new_pets =[] i = 0 for item in all_pets: if item[i][0] == "c": new_pets.append(item) print("The pet store sells:", all_pets) print("These start with the letter c:", new_pets) A. The pet store sells: ["dog", "cat", "ferret", "hamster", "bunny"]These start with the letter c: ['cat'] C. The pet store sells: ['bunny', 'cat', 'dog', 'ferret', 'hamster', 'budgie', 'canary', 'hawk', 'parrot']These start with the letter c: ['cat', 'canary'] D. The pet store sells: ['dog', 'cat', 'ferret', 'hamster', 'bunny', 'canary', 'parrot', 'budgie', 'hawk'] These start with the letter c: ['cat', 'canary']

D. The pet store sells: ['dog', 'cat', 'ferret', 'hamster', 'bunny', 'canary', 'parrot', 'budgie', 'hawk'] These start with the letter c: ['cat', 'canary']

What will display after the following print() function is executed?print("Welcome!\nNow that you have learned about","input\nand output, you may be wondering,\"What\nis","next?\"") A. Welcome! Now that you have learned about input and output, you may be wondering, "What is next?" (6 lines total) B. Welcome! Now that you have learned about input and output, you may be wondering, "What is next?" (3 lines total) C. Welcome!\nNow that you have learned about", "input\nand output, you may be wondering,\"What\nis", "next?\" (3 lines total D. Welcome! Now that you have learned about input and output, you may be wondering, "What is next?" (4 lines total)

D. Welcome! Now that you have learned about input and output, you may be wondering, "What is next?" (4 lines total)

Given the following code and its output: 1. discount_rate = .1 2. item = 89.95 3. discount = item * discount_rate 4. print("The discount on this item is $", discount)) Output: The discount on this item is $ 8.995000000000001 Which of the following would produce a user-friendly correct result? A. change line 1 to: discount_rate = 10% B. change line 2 to: item = int(89.95) C. change line 3 to: discount = int(item * discount_rate) D. change line 4 to: print("The discount on this item is $", round(discount, 2))

D. change line 4 to: print("The discount on this item is $", round(discount, 2))

Given the following code, select the compound condition that makes sure that the input is an integer greater than 0 and less than 1,000. my_num = input("Enter a number between 1 and 999:") check_num = int(my_num) while _________________________: my_num = input("Try again. The number must be between 1 and 999") check_num = int(my_num) A. check_num <= 0 and check_num != 1000 B. check_num < 0 and > 1000 C. check_num <= 0 or check_num > 1000 D. check_num <= 0 or check_num >= 1000

D. check_num <= 0 or check_num >= 1000

Which of the following data types would you use to store the number 25.62? A. str B. int C. num D. float

D. float

A global variable: A. is defined inside the main() function B. cannot be modified inside a function C. cannot be accessed from within a function D. is defined outside of all functions

D. is defined outside of all functions

Which of the following is not an acceptable way to code a nested structure? A. nest a for loop inside a while loop B. nest an if statement inside a for loop C. nest a while loop inside an elif clause D. nest an else clause within an elif clause

D. nest an else clause within an elif clause

Assuming the random module has been imported into its default namespace, which of the following could possibly result in a value of 0.94? A. number = random.randfloat() B. number = random.randint(0, 1) C. number = random.randint(0, 1) / 100 D. number = random.random()

D. number = random.random()

The default namespace for a module is: A. the global namespace B. the name of the module followed by _default C. the first letter of the module name D. the same as the name of the module

D. the same as the name of the module

Given the tuple that follows, which of the following assigns the values in the tuple to variables? numbers = (22, 33, 44, 55) A. for item in numbers: item[i] = numbers[i] B. w, x, y, z = numbers.unpack() C. w = numbers x = numbers y = numbers z = numbers D. w, x, y, z = numbers

D. w, x, y, z = numbers


Kaugnay na mga set ng pag-aaral

CompTIA CySA+ (CS0-003) Practice Exam #3

View Set

Microbiology Lecture and Lab Midterm Review

View Set

PSYC 301 Lecture Chapter 1 and 2

View Set