Python - Exam one - 51-100
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. 35 b. 45 c. 8 d. 0
a. 35
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 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
If you want to code an if clause, but you don't want to perform any action, you can code a. a pass statement b. a break statement c. a skip statement d. an end statement
a. a pass statement
To refer to an item in a list, you code the list name followed by a. an index number in brackets, starting with the number 0 b. an index number in brackets, starting with the number 1 c. an index number in parentheses starting with the number 0 d. an index number in parentheses, starting with the number 1
a. an index number in brackets, starting with the number 0
Which of the following functions randomly selects one item in a list? a. choice() b. shuffle() c. lrandom() d. randomitem()
a. choice()
Python relies on correct __________ to determine the meaning of a statement. a. indentation b. punctuation c. continuation d. comments
a. indentation
What will the following print() function display? print("lions", "tigers", "bears", sep = ' & ', end = ' oh, my!!') a. lions & tigers & bears oh, my!! b. lions&tigers&bearsoh, my!! c. lions & tigers & bears & oh, my!! d. lions, & tigers, & bears, oh, my!!
a. lions & tigers & bears oh, my!!
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)) a. 60 b. 40 c. Nothing, it causes an error d. 20
c. Nothing, it causes an error
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 24 b. The product is 4 c. The product is 6 d. The product is 1
c. The product is 6
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']
What, if anything, is wrong with this code? student_score = int(input("Enter this student's score: ")) score = student_score + 5 print("With the 5-point curve, the student's score is ", scores, ".") a. nothing is wrong with this code b. the input() function is chained with the int() function c. an undeclared variable name is used in the print() function d. a string variable is used in an arithmetic expression
c. an undeclared variable name is used in the print() function
The __________ method adds an item to the end of a list. a. index() b. insert() c. append() d. pop()
c. append()
Which of the following data types would you use to store the number 25.62? a. str b. int c. float d. num
c. float
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:"))
Which of the following translates bytecode into instructions for the computer? a. the computer's operating system b. the Python interpreter c. the Python virtual machine d. the IDE
c. the Python virtual machine
The default namespace for a module is a. the global namespace b. the first letter of the module name c. the same as the name of the module d. the name of the module followed by _default
c. the same as the name of the module
Given the following list, what is the value of ages[5]? ages = [22, 35, 24, 17, 28] a. 22 b. 17 c. 28 d. None: Index error
d. None: Index error
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+1): product = product * i i += 1 print("The product is ", product) a. The product is 1 b. The product is 6 c. The product is 4 d. The product is 24
d. The product is 24
The following is an example of __________. #!/usr/bin/env python 3 a. bytecode b. source code c. Java code d. a shebang line
d. a shebang line
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 a. global.print_name(name) b. print_name(name) c. address.print_name(name) d. a.print_name(name)
d. a.print_name(name)
Which of the following in this expression is evaluated first? age >= 65 or age <= 18 or status == "retired" a. age >= 65 or age <= 18 b. age <= 18 c. status == "retired" d. age >= 65
d. age >= 65
In a while loop, the Boolean expression is tested a. both before and after the loop is executed b. until it equals the current loop value c. after the loop is executed d. before the loop is executed
d. before the loop is executed
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 default operator (:), and the default value d. code the name of the argument, the assignment operator (=), and the default value
d. code the name of the argument, the assignment operator (=), and the default value
The goal of __________ is to fix all the errors in a program. a. testing b. editing c. interpreting d. debugging
d. debugging
To insert the item "melon" after "grapes" in the following list, you would use which of these methods? fruit = ["apple", "banana", "grapes", "mangos", "oranges"] a. fruit.append(3, "melon") b. fruit.pop("melon", 3) c. fruit.insert("melon", 3) d. fruit.insert(3, "melon")
d. fruit.insert(3, "melon")
The and operator has a. lower precedence than the or and not operators b. higher precedence than the or and not operators c. lower precedence than the or operator, but higher precedence than the not operator d. higher precedence than the or operator, but lower precedence than the not operator
d. higher precedence than the or operator, but lower precedence than the not operator
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
The primary difference between a tuple and a list is that a tuple a. has a limited range b. is indexed starting from 1 c. is mutable d. is immutable
d. is immutable
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? a. get_username() b. if __name__ == "__main__" c. input("Enter your first name: ") d. main()
d. main()
A for loop that uses a range() function is executed a. until the range() function returns a false value 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. once for each integer in the collection returned by the range() function
d. once for each integer in the collection returned by the range() function
The __________ software for a computer provides the software that's needed for running applications. a. application b. GUI c. operation d. systems
d. systems
A console application runs a. through a browser b. with a GUI c. through another application d. via a command prompt
d. via a command prompt
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).") Which of the following could be a pseudocode plan for the program? a. Define widget count variable IF user buys widgets add 1 to widget count ELSE end loop Display results b. Define widget count variable Begin infinite loop Get user input IF user buys widget add 1 to widget count ELSE end loop Display results c. Get user input Loop WHILE user wants to continue IF user buys widget add 1 to widget count Get user input Display results d. Get user input Loop while input != "y" IF user buys widget add 1 to widget count ELSE end loop Display results
b. Define widget count variable Begin infinite loop Get user input IF user buys widget add 1 to widget count ELSE end loop Display results
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: ['canary', 'parrot', 'budgie', 'hawk'] These start with the letter c: ['canary'] b. The pet store sells: ['dog', 'cat', 'ferret', 'hamster', 'bunny', 'canary', 'parrot', 'budgie', 'hawk'] These start with the letter c: ['cat', 'canary'] c. The pet store sells: ["dog", "cat", "ferret", "hamster", "bunny"] These start with the letter c: ['cat'] d. The pet store sells: ['bunny', 'cat', 'dog', 'ferret', 'hamster', 'budgie', 'canary', 'hawk', 'parrot'] These start with the letter c: ['cat', 'canary']
b. The pet store sells: ['dog', 'cat', 'ferret', 'hamster', 'bunny', 'canary', 'parrot', 'budgie', 'hawk'] These start with the letter c: ['cat', 'canary']
Which type of expression has a value of either true or false? a. relational b. Boolean c. if-else d. binary
b. Boolean
To create a Python program, you use: a. the command line b. IDLE's editor c. the F5 key d. IDLE's interactive shell
b. IDLE's editor
To test a Python statement, you use: a. the F5 key b. IDLE's interactive shell c. IDLE's editor d. the command line
b. IDLE's interactive shell
Given the following list, what is the value of names[2]? names = ["Lizzy", "Mike", "Joel", "Anne", "Donald Duck"] a. Mike b. Joel c. Anne d. None, improper assignment of "Donald Duck" due to space in the name
b. Joel
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() What will display after the code executes? a. Lizzy 73 C Mike 98 A Joel 88 B+ Anne 93 A b. Lizzy 73 C Mike 98 A Joel 88 B+ Anne 93 A c. Lizzy,73,C Mike,98,A Joel,88,B+ Anne,93,A d. Lizzy,73,C Mike,98,A Joel,88,B+ Anne,93,A
b. Lizzy 73 C Mike 98 A Joel 88 B+ Anne 93 A
Which of the following in this expression is evaluated first? age >= 65 and status == "retired" or age < 18 a. age <= 18 b. age >= 65 c. status == "retired" or age < 18 d. status == "retired"
b. age >= 65
The best way to call the main() function of a program is to code a. a while statement that calls the main() function in each loop b. an if statement that calls the main() function only if the current module is the main module c. main() d. an if statement that calls the main() function only if the function exists
b. an if statement that calls the main() function only if the current module is the main module
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.
When a function changes the data in a list, the changed list a. does not need to be returned because lists are immutable. b. does not need to be returned because lists are mutable. c. needs to be returned because lists are immutable. d. is only available within that function.
b. does not need to be returned because lists are mutable.
Which of the following statements imports a module into the global namespace? a. import temperature as temp b. from temperature import * c. import temperature as global d. import temperature
b. from temperature import *
The data in __________ is lost when an application ends. a. disk storage b. main memory c. the CPU d. the application software
b. main memory
Which of the following doesn't follow the best naming practices for variables? a. firstName b. pRate c. pay_rate d. first_name
b. pRate
When you use a multiple assignment statement to unpack a tuple, a. you assign the tuple to a list b. you assign the tuple to a two or more variable names separated by commas c. you use a for statement to assign the values in the tuple to a list d. you use indexing to assign the values in the tuple to multiple variables
b. you assign the tuple to a two or more variable names separated by commas
Python will sort the strings that follow in this sequence: Peach peach 1peach 10Peaches a. Peach, peach, 1peach, 10Peaches b. 1peach, 10Peaches, Peach, peach c. 10Peaches, 1peach, Peach, peach d. 10peaches, 1peach, peach, Peach
c. 10Peaches, 1peach, Peach, peach
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? a. 2 b. 3 c. 4 d. 5
c. 4
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() What is the value of students[2][1]? :a. Joel b. Joel, 88, B+ c. 88 d. B+
c. 88