Reference no: EM132330144
Question 1
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. print_name(name)
b. global.print_name(name)
c. a.print_name(name)
d. address.print_name(name)
Question 2
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
Question 3
The default namespace for a module is
a. the first letter of the module name
b. the same as the name of the module
c. the global namespace
d. the name of the module followed by _default
Question 4
A file that contains reusable code is called a
a. namespace
b. hierarchy chart
c. module
d. function
Question 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 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
Question 6
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 multiply() function
b. the arithmetic module
c. the result statement
d. the main() function
Question 7
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.random()
c. number = random.coin()
d. number = random.randint(0, 2)
Question 8
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
Question 9
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, 202, 2)
b. number = random.randint(2, 200, 2)
c. number = random(1, 100) * 2
d. number = random.randrange(2, 200, 2)
Question 10
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.
Question 11
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. 5, 6
b. 9, 8
c. 4, 2
d. 20, 12
Question 12
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. Lopez.Maria
c. lopez.maria
d. maria.lopez
Question 13
A local variable is defined
a. inside a function
b. inside the main() function
c. inside an if statement
d. outside of all functions
Question 14
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. username
b. first_name, last_name
c. s, first, last
d. first, last
Question 15
Which of the following statements imports a module into the global namespace?
a. from temperature import *
b. import temperature as temp
c. import temperature
d. import temperature as global
Question 16
The best way to call the main() function of a program is to code
a. an if statement that calls the main() function only if the function exists
b. 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
d. main()
Question 17
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. 20
b. 60
c. Nothing, it causes an error
d. 40
Question 18
If you import two modules into the global namespace and each has a function named get_value(),
a. an exception occurs
b. a name collision occurs
c. the program crashes
d. an error occur
Question 19
Which of the following statements imports a module into the default namespace?
a. from temperature import *
b. import temperature as temp
c. import temperature as t
d. import temperature
Question 20
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. input("Enter your first name: ")
c. main()
d. get_username()