CPT-168-WS2

Ace your homework & exams now with Quizwiz!

line 4, runtime error

What line number of the following code contains an error and what type of error is it? 1. count = 1 2. while count <= 4: 3. print(count, end=" ") 4. i *= 1 5. print("\nThe loop has ended.")

a set of parentheses that contains zero or more arguments

To define a function, you code the def keyword and the name of the function followed by

an index number in brackets, starting with the number 0

To refer to an item in a list, you code the list name followed by

35

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)

module

A file that contains reusable code is called a

number = random.randrange(2, 202, 2)

Assuming the random module has been imported into its default namespace, which of the following could be used to generate a random even integer from 2 through 200?

number = random.randint(0, 1)

Assuming the random module has been imported into its default namespace, which of the following could be used to simulate a coin toss where 0 = heads and 1 = tails?

number = random.random()

Assuming the random module has been imported into its default namespace, which of the following could possibly result in a value of 0.94?

import the module

Before you can use a standard module like the random module, you need to

first, last

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() What arguments are defined by the get_username() function?

[['Mike',98,'A'],['Lizzy',73,'C'],['Joel',88,'B+'],['Anne',93,'A']]

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 would display if the following three lines were added at the end of the main() function? students.sort() students.reverse() print(students)

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

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?

change line 5 to: pay = (40 * rate) + ((hours - 40) * rate * 1.5)

Given the following code, if the user worked 45 hours at $10.00/hour, the output is as shown below. 1. def main(): 2. hours = float(input("How many hours did you work? ")) 3. rate = float(input("What is your hourly rate? ")) 4. if (hours > 40) and (rate < 15): 5. pay = (hours * rate) + (hours - 40 * rate * 1.5) 6. else: 7. pay = hours * rate 8. print("Your pay is: $ ", round(pay, 2)) Output: Your pay is: $ -105.0 Which line should be changed to fix this logic error?

my_name = "Donny", names = ["Lizzy", "Mike", "Joel", "Anne"]

Given the following code, what is the value of my_name and what does the list consist of after the second statement is executed? names = ["Lizzy", "Mike", "Joel", "Anne", "Donny"] my_name = name.pop()

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

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)

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

Given the following code, what would the list consist of after the second statement? ages = [22, 35, 24, 17, 28] ages.insert(3, 4)

None: Index error

Given the following list, what is the value of ages[5]? ages = [22, 35, 24, 17, 28]

Joel

Given the following list, what is the value of names[2]?names = ["Lizzy", "Mike", "Joel", "Anne", "Donald Duck"]

w, x, y, z = numbers

Given the tuple that follows, which of the following assigns the values in the tuple to variables? numbers = (22, 33, 44, 55)

a name collision occurs

If you import two modules into the global namespace and each has a function named get_value(),

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

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()

lopez.maria

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() If the user enters 'Lopez' for the first prompt in main() and 'Maria' for the second prompt, what will display?

main()

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() What function is called first when the program runs?

local

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() What is the scope of the variable named s?

40

def get_volume(width, height, length=2): volume = width * height * length return volume def main(): l = 3 w = 4 h = 5 v = get_volume(l, w, h) print(v) if __name__ == "__main__": main() If you add the following code to the end of the main() method, what does it print to the console? print(get_volume(10, 2))

5

def get_volume(width, height, length=2): volume = width * height * length return volume def main(): l = 3 w = 4 h = 5 v = get_volume(l, w, h) print(v) if __name__ == "__main__": main() What value is passed to the height argument by the call to the get_volume() function?

Programmer didn't test for a 0 value in the denominator.

A programmer created this code for a client: def divide(numerator, denominator): quotient = numerator / denominator return quotient def main(): numerator = int(input("Enter the numerator: ")) denominator = int(input("Enter the denominator: ")) quotient = divide(numerator, denominator) print("The quotient is ", quotient) if __name__ == "__main__": main() The programmer tested this code with the following three sets of data: 1. numerator = 9, denominator = 3 2. numerator = -50, denominator = 5 3. numerator = 389, denominator = -26 The results were: 1. The quotient is 3.0 2. The quotient is -10.0 3. The quotient is -14.961538461538462 However, once the program was in use, the client reported that sometimes the program crashed. Can you explain why?

can be used to return a local variable to the calling function

A return statement

The sum is 11

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 What will be displayed after the code runs?

is defined outside of all functions

A global variable

The variable Lname on line 4 does not exist

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. print("Your score on this exam is ", score) 6. score = score + 5 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?

The curve is calculated after the score has been displayed.

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. print("Your score on this exam is ", score) 6. score = score + 5 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?

inside a function

A local variable is defined

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

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. print("Your score on this exam is ", score) 6. score = score + 5 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?

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

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. print("Your score on this exam is ", score) 6. score = score + 5 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?

change line 8 to: average_cost = round(item_total / (count - 1))

1. count = 1 2. item_total = 0 3. item = 0 4. while count < 4: 5. item = int(input("Enter item cost: ")) 6. item_total += item 7. count += 1 8. average_cost = round(item_total / count) 9. print("Total cost:", item_total, "\nAverage cost:", average_cost) Refer to Code Example 5-1: If the user enters 5, 10, and 15 at the prompts, the output is: Total cost: 30 Average cost: 8 Which line should be changed to fix this logic error?

is immutable

The primary difference between a tuple and a list is that a tuple

just the functions that were called prior to the exception

The stack is available when an exception occurs. It displays a list of

line 1, syntax error

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))

append()

The __________ method adds an item to the end of a list.

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

The best way to call the main() function of a program is to code

the same as the name of the module

The default namespace for a module is

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

To assign a default value to an argument when you define a function, you

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

To call a function with named arguments, you code the

a set of parentheses that contains zero or more arguments

To call a function, you code the function name and

fruit.insert(3, "melon")

To insert the item "melon" after "grapes" in the following list, you would use which of these methods?fruit = ["apple", "banana", "grapes", "mangos", "oranges"]

fruit.remove("mangos") or fruit.pop(3)

To remove the item "mangos" from the following list, you would use which of these methods? fruit = ["apple", "banana", "grapes", "mangos", "oranges"]

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

To test the functions of a module from the IDLE shell, you

ghost witch ogre

What would be displayed after the following code snippet executes? costumes = ["ghost", "witch", "elf", "ogre"] name = "elf" if name in costumes: costumes.remove(name) for item in costumes: print(item)

does not need to be returned because lists are mutable

When a function changes the data in a list, the changed list

view the values of all the variables that you've stepped through

When the IDLE debugger reaches a breakpoint, you can do all but one of the following. Which one is it?

list the expected exceptions for each test run

When you plan the test runs for a program, you should do all but one of the following. Which one is it?

display the values of the global constants used by the function

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?

you assign the tuple to a two or more variable names separated by commas

When you use a multiple assignment statement to unpack a tuple,

on a statement before the statement you think is causing the bug

When you use the IDLE debugger, you start by setting a breakpoint

vehicles = ("sedan","SUV","motorcycle","bicycle","hatchback","truck")

Which of the following creates a tuple of six strings?

choice()

Which of the following functions randomly selects one item in a list?

invalid variable names

Which of the following is not a common type of syntax error?

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

Which of the following is not true about a list of lists?

You should always start by coding and testing the most difficult functions.

Which of the following is not true about top-down coding and testing?

Related functions should be combined into a single function.

Which of the following is not true of hierarchy charts?

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

Which of the following statements about list copies is not true? When you make a

import temperature

Which of the following statements imports a module into the default namespace?

from temperature import *

Which of the following statements imports a module into the global namespace?

You can use regular Python comments to document the functions of the module.

Which of the following statements is not true about the documentation of a module?

numbers = [5.3, 4.8, 6.7]

Which of the following would create a list named numbers consisting of 3 floating-point items?

a.print_name(name)

Which statement would you use to call the print_name() function from a module named address that has been imported with this statement? import address as a

syntax

Which type of error prevents a program from compiling and running?

runtime

Which type of error throws an exception that stops execution of the program?

60

def get_volume(width, height, length=2): volume = width * height * length return volume def main(): l = 3 w = 4 h = 5 v = get_volume(l, w, h) print(v) if __name__ == "__main__": main() When this program runs, what does it print to the console?

"Mike", 98, "A"

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?

88

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]?

Lizzy 73 C Mike 98 A Joel 88 B+ Anne 93 A

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 will display after the code executes?

5, 6

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 What values are in x and y after the code runs?

the multiply() function

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 The add() function is called by

12, 12

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 What values are in x and y after the code runs?

The answer is 24

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 When this code runs, what does it print to the console?


Related study sets

Chapter 4 Intro to Project Management, Project Management 1-7

View Set