Python Midterm ch 2-5,7
What will be displayed after the following code is executed? def pass_it(x, y): z = x*y result = get_result(z) return(result) def get_result(number): z = number + 2return(z) num1 = 3 num2 = 4 answer = pass_it(num1, num2) print(answer)
14
What are the values that the variable num contains through the iterations of the following for loop?for num in range(2, 9, 2):
2, 4, 6, 8
What will be displayed after the following code is executed? total = 0 for count in range(4,6): total += count print(total)
4 9
What will be displayed after the following code is executed? total = 0 for count in range(1,4): total += count print(total)
6 1 3 6 10
Which of the following is not an augmented assignment operator?
<=
Match the functions from the random module with what they do. randint: Randrange: random: uniform: seed:
B. Returns a random number in the range provided by the arguments inclusively E. Returns a random value from the sequence indicated by the arguments C. Returns a random float between 0.0 and 1.0 A. Returns a random float in the specified range D. Initializes the formula used by the random module to generate random numbers
What will be the output after the following code is executed? def pass_it(x, y): z = x + ", " + y return(z) name2 = "Tony" name1 = "Gaddis" fullname = pass_it(name1, name2) print(fullname)
Gaddis, Tony
A while or for loop can be followed by an else clause. When will the block of code in an else clause be executed?
Only after the loop terminates normally, without encountering a break statement
What is an advantage of using a tuple rather than a list?
Processing a tuple is faster than processing a list.
Which list will be referenced by the variable number after the following code is executed? number = list(range(0, 9, 2))
[0, 2, 4, 6, 8)
What will be the value of the variable num_list after the following code executes? num_list = [1, 2, 3, 4] num_list[3] = 10
[1, 2 , 3, 10]
What will be the value of the variable list after the following code executes?list = [1, 2] list = list * 3
[1, 2, 1, 2, 1, 2]
Which of the following would you use if an element is to be removed from a specific index?
a del statement
A value-returning function is
a function that will return a value back to the part of the program that called it
When using the ________ logical operator, both subexpressions must be true for the compound expression to be true.
and
The ________ method is commonly used to add items to a list.
append()
A set of statements that belong together as a group and contribute to the function definition is known as a
block
Which of the following Python statements can be used to cause a for or while loop to terminate immediately?
break
What will be displayed after the following code is executed? count = 4 while count < 12: print("counting") count = count + 2
counting counting counting counting
def display_results(subtotal, tax_rate): total = subtotal + (subtotal * tax_rate) print(f'The total purchase is {total}') amount = 35.99 rate = 0.07 The amount variable contains the customer purchase before tax has been calculated. The rate variable contains the tax rate which is 7% in this case. Given the code above, how should the function display_results be called to display the total purchase amount?
display_results(amount, rate)
What are the data items in a list called?
elements
A function definition specifies what a function does and causes the function to execute.
false
A list cannot be passed as an argument to a function.
false
Python allows you to compare strings, but it is not case sensitive.
false
Python uses the same symbols for the assignment operator as for the equality operator.
false
The Python language is not sensitive to how code is indented. You can use any number of spaces at the beginning of a line and Python will ignore those spaces.
false
The index of the first element in a list is 1, the index of the second element is 2, and so forth.
false
The remove method removes all occurrences of an item from a list.
false
What is the result of the following Boolean expression, given that x =5, y = 3, and z = 8? X < y and z > x
false
It is recommended that programmers avoid using ________ variables in a program whenever possible.
global
Which of the follow will display "Found it!" if the string 'May 4' is found in the list appointments?
if 'May 4' in appointments: print('Found it!') if appointments.count('May 4') > 0: print('Found it!') try: idx = appointments.index('May 4') print('Found it!') except ValueError: pass
Which of the following is the correct if clause to determine whether choice is anything other than 10?
if choice != 10:
Which of the following is the correct if clause to determine whether y is in the range 10 through 50, inclusive?
if y >=10 and y <= 50:
Tuples are ________ sequences which means that once a tuple is created, it cannot be changed.
immutable
In order to create a graph in Python, you need to include
import matploblib.pyplot
Which of the following statements causes the interpreter to load the contents of the random module into memory?
import random
Which of the following will put a random number between 1 and 10 inclusive into the variable num? (There are two correct answers. Choose them both.)
import random num = random.randrange(1, 11)
________ is the process of inspecting data that has been input into a program in order to ensure that the data is valid before it is used in a computation.
input validation
What does the following program do? student = 1 while student <=3: total = 0 for score in range(1,4): score = int(input("Enter test score: ")) total += score average = total/3 print("Student ", student, "average", average) student +=1
it accepts 3 test scores for each of 3 students and outputs the average for each student
The built-in function ________ returns the length of a sequence.
len()
Rewrite the following code with list comprehension: list1 = [4, 7, 12, 8, 25] list2 = [] for x in list1: if x > 10: list2.append(x*2)
list1 = [4, 7, 12, 8, 25] list2 = [x*2 for x in list1 if x > 10]
Given the following list comprehension, write the equivalent code without using list comprehension:. list1 =[[x,y] for x in range(3) for y in range(2) if (x + y) % 2 == 0]
list1 = [] for x in range(3): for y in range(2): if (x + y ) % 2 == 0: list1.append([x,y]
A ________ variable is created inside a function.
local
When working with multiple sets of data, like a table made up of rows and columns, one would typically use a(n)
nested list
The logical ________ operator reverses the truth of a Boolean expression.
not
The primary difference between a tuple and a list is that
once a tuple is created, it cannot be changed
When using the ________ logical operator, one or both of the subexpressions must be true for the compound expression to be true.
or
Both of the following for clauses would generate the same number of loop iterations. for num in range(4): for num in range(1, 5):
true
Different functions can have local variables with the same names.
true
In a flowchart, both the decision structure and the repetition structure use the diamond symbol to represent the condition that is tested.
true
Invalid indexes do not cause slicing expressions to raise an exception.
true
The if statement causes one or more statements to execute only when a Boolean expression is true.
true
The index -1 identifies the last element in a list.
true
To calculate the average of the numeric values in a list, the first step is to get the total of values in the list.
true
What is the result of the following Boolean expression, given that x = 5, y = 3, and z= 8?not (x < y or z > x) and y < z
true
The ________ function can be used to convert a list to a tuple.
tuple
When will the following loop terminate? while keep_on_going != 999:
when keep_on_goin refers to a value equal to 999