Exam #2: Chapter 3 and 4

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

Code Example 4-2 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() Refer to Code Example 4-2: 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. 20 c. 40 d. Nothing, it causes an error

c. 40

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.randint(0, 1) / 100 b. number = random.randfloat() c. number = random.random() d. number = random.randint(0, 1)

c. number = random.random()

Python will sort the strings that follow in this sequence: Peach peach1 peach 10Peaches a. 10peaches, 1peach, peach, Peach b. 1peach, 10Peaches, Peach, peach c. Peach, peach, 1peach, 10Peaches d. 10Peaches, 1peach, Peach, peach

d. 10Peaches, 1peach, Peach, peach

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 no widget(s) today. b. You bought 1 widget(s) today. c. Nothing, the break statement will cause the program to end d. You bought 0 widget(s) today.

a. You bought no widget(s) today.

To call a function, you code the function name and a. a set of parentheses that contains zero or more arguments b. a set of parentheses that contains one or more arguments c. a set of parentheses that contains a list of the local variables d. a set of parentheses

a. 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 a. a set of parentheses that contains zero or more arguments b. a set of parentheses that contains a list of the local variables c. a set of parentheses that contains one or more arguments d. a set of parentheses

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

Which of the following in this expression is evaluated first? age >= 65 and status == "retired" or age < 18 a. age >= 65 b. status == "retired" c. status == "retired" or age < 18 d. age <= 18

a. age >= 65

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

a. nothing will display

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? a. number = random.randint(0, 1) b. number = random.randint(0, 2) c. number = random.random() d. number = random.coin()

a. number = random.randint(0, 1)

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 function is called first when the program runs? a. if __name__ == "__main__" b. main() c. get_username() d. input("Enter your first name: ")

b. main()

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

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

Which of the following is true for an if statement that has both elif and else clauses? a. The statements in the else clause are always executed. b. If the condition in an elif clause is true, the statements in that clause are executed. c. If the condition in the else clause is true, the statements in that clause are executed. d. If the condition in the if clause is true, the statements in that clause are executed.

d. If the condition in the if clause is true, the statements in that clause are executed.

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

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

What will this loop display? sum = 0 for i in range(0, 25, 5): sum += i print(sum) a. 50 b. 0, 5, 10, 15, 20 c. 30 d. 0, 5, 10, 15, 20, 25

a. 50

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. Which of the following could be a pseudocode plan for the program? a. Define widget count variable Begin infinite loop Get user input IF user buys widget add 1 to widget count ELSE end loop Display results b. Get user input Loop WHILE user wants to continue IF user buys widget add 1 to widget count Get user input Display results c. Get user input Loop while input != "y" IF user buys widget add 1 to widget count ELSE end loop Display results d. Define widget count variable IF user buys widgets add 1 to widget count ELSE end loop Display results

a. Define widget count variable Begin infinite loop Get user input IF user buys widget add 1 to widget count ELSE end loop Display results

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 6 b. The product is 24 c. The product is 1 d. The product is 4

a. The product is 6

In a while loop, the Boolean expression is tested a. before the loop is executed b. both before and after the loop is executed c. after the loop is executed d. until it equals the current loop value

a. before the loop is executed

Which of the following statements imports a module into the default namespace? a. import temperature b. import temperature as temp c. from temperature import * d. import temperature as t

a. import temperature

To compare two strings with mixed uppercase and lowercase letters, a programmer can first use the a. lower() method to convert all characters in each string to lowercase. b. upper() method to convert the first character in each string to uppercase. c. upper() method to convert all characters after the first character in each string to uppercase. d. lower() method to convert all characters after the first character in eachstring to lowercase.

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

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: What values are in x and y after the code runs? Select one: a. 4, 3 b. 24, 24 c. 12, 12 d. 5, 6

b. 24, 24

Code Example 4-2 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() Refer to Code Example 4-2: What value is passed to the height argument by the call to the get_volume() function? a. 3 b. 4 c. 5 d. 2

b. 4

Code Example 4-2 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() Refer to Code Example 4-2: When this program runs, what does it print to the console? a. 24 b. 60 c. v d. 40

b. 60

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. Nothing, the code causes an error b. The sum is 11 c. The sum is 6 d. The sum is 17

b. The sum is 11

Which of the following can use the range() function to loop through a block of statements? a. an if statement b. a for statement c. a break statement d. a while statement

b. a for statement

If you import two modules into the global namespace and each has a function named get_value(), a. an error occurs b. a name collision occurs c. an exception occurs d. the program crashes

b. a name collision occurs

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. a.print_name(name) c. print_name(name) d. address.print_name(name)

b. a.print_name(name)

Which of the following in this expression is evaluated first? age >= 65 or age <= 18 or status == "retired" a. age <= 18 b. age >= 65 c. age >= 65 or age <= 18 d. status == "retired"

b. age >= 65

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 arguments are defined by the get_username() function? a. s, first, last b. first, last c. username d. first_name, last_name

b. first, last

A local variable is defined a. inside an if statement b. inside a function c. inside the main() function d. outside of all functions

b. inside a function

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. Lopez.Maria b. lopez.maria c. maria.lopez d. Maria.Lopez

b. lopez.maria

What will be displayed after the following code is executed? counter = 1 while counter <= 20: print(counter, end=" ") counter *= 3 print("\nThe loop has ended.") a. 1 3 9 12 15 18 The loop has ended. b. 1 3 9 The loop has ended. c. 1 3 9 The loop has ended. d. 1 3 9 The loop has ended.

c. 1 3 9 The loop has ended.

What will the output of the following code be if the user enters 3 at the prompt? your_num = int(input("Enter a number:")) while (your_num > 0): product = your_num * 5 print(your_num, " * 5 = ", product) your_num -= 1 a. 3 * 5 = 15; 2 * 5 = 10; 1 * 5 = 5; 0 * 5 = 0 b. 3 * 5 = 15 c. 3 * 5 = 15; 2 * 5 = 10; 1 * 5 = 5 d. 3 * 5 = 15; 3 * 5 = 15; 3 * 5 = 15

c. 3 * 5 = 15; 2 * 5 = 10; 1 * 5 = 5

How many times will "Hi there!" be displayed after the following code executes? num = 2 while num < 12: print("Hi, there!") num += 2 a. 2 b. 12 c. 5 d. 6

c. 5

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. 20, 12 c. 5, 6 d. 4, 2

c. 5, 6

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 4 b. The product is 6 c. The product is 24 d. The product is 1

c. The product is 24

Which of the following statements is not true about the documentation of a module? a. You can use Python docstrings to document the functions of the module. b. The documentation can describe each function in the module. c. You can use regular Python comments to document the functions of the module. d. You can call the help() function from the interactive shell to view the documentation.

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

If you want to code an if clause, but you don't want to perform any action, you can code a. a break statement b. a skip statement c. a pass statement d. an end statement

c. a pass statement

To assign a default value to an argument when you define a function, you a. code the name of the argument, the default operator (:), and the default value 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 default value instead of its name in the arguments list

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

Which of the following statements imports a module into the global namespace? a. import temperature as global b. import temperature as temp c. from temperature import * d. import temperature

c. from temperature import *

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 in main() but local in get_username() b. local in main() but global in get_username() c. local d. global

c. local

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

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

When two strings are compared, they are evaluated based on the ___________________ of the characters in the string. a. string method b. uppercase/lowercase sequence c. sort sequence d. alphabetical order

c. sort sequence

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: The add() function is called by a. the result statement b. the main() function c. the multiply() function d. the arithmetic module

c. the multiply() function

How many times will "Hi again!" be displayed after the following code executes? for i in range(0, 12, 2): print("Hi, again!") a. 5 b. 12 c. 2 d. 6

d. 6

Which type of expression has a value of either true or false? a. binary b. if-else c. relational d. Boolean

d. Boolean

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 12 b. The answer is 28 c. The answer is 7 d. The answer is 24

d. The answer is 24

The best way to call the main() function of a program is to code a. main() b. a while statement that calls the main() function in each loop c. an if statement that calls the main() function only if the function exists d. an if statement that calls the main() function only if the current module is the main module

d. 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. switch statement b. continue statement c. end statement d. break statement

d. break statement

A return statement a. can only be used once in each function b. must be coded within every function c. can be used to allow the function to modify the value of a global variable d. can be used to return a local variable to the calling function

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

Which of the following creates a Boolean variable? a. flag = True or False b. flag == "False" c. if flag == True: d. flag = True

d. flag = True

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

Before you can use a standard module like the random module, you need to a. import the module into its default namespace b. import the module into a custom namespace c. import the module into the global namespace d. import the module

d. import the module

A global variable a. cannot be accessed from within a function b. cannot be modified inside a function c. is defined inside the main() function d. is defined outside of all functions

d. is defined outside of all functions

A file that contains reusable code is called a a. namespace b. hierarchy chart c. function d. module

d. module

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 a while loop inside an elif clause c. nest an if statement inside a for loop 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 be used to simulate a coin toss where 0 = heads and 1 = tails? a. number = random.randint(0, 2) b. number = random.random() c. number = random.coin() d. number = random.randint(0, 1)

d. number = random.randint(0, 1)

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? a. number = random.randrange(2, 200, 2) b. number = random.randint(2, 200, 2) c. number = random(1, 100) * 2 d. number = random.randrange(2, 202, 2)

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

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


Ensembles d'études connexes

Macroeconomics Chapter 5: Saving & Investment in the Open Economy

View Set

Ch 10 Middle Childhood: Social and Emotional Development

View Set

Inclusion, Equity, and Diversity

View Set

Real Estate Vocab (need to study)

View Set

Lección 5 - Estructura 5.3 - Reciprocal reflexives - Así fue

View Set

CHEM 122, UNIT 2 - SCALAR/PODER/PRATICE EXAM Questions

View Set