Python QA
Python QA
Question 1
Question: Consider the following Python code snippet. What will the contents of
my_numbers be after execution?
Python
for i in range(4):
my_numbers.insert(i, my_numbers[-1])
print(my_numbers)
Question 2
Question: In the context of a function call in Python, what determines the meaning of a
positional argument?
Question 3
Python
beta = alpha[:]
beta[2] = 99
Which of the following statements are true regarding the code above? (Select two answers)
Answers:
• The beta list is of the same length as the alpha list, but their contents are not
identical.
• alpha and beta are separate lists; the alpha list remains unchanged after the
modification.
Question 4
Question: An operator able to check whether two values are not equal is coded as
Answer: !=
Question 5
Python
def compute_square(x):
return x * x
def compute_quad(x):
print(compute_quad(4))
Question 6
Question 7
Question: Consider the Python function defined below:
Python
print(raise_power(exponent=3, 2))
Question 8
Python
z=0
y = 10
Answer: False
Question 9
Question: Review the following identifiers and select the ones that are reserved keywords
in Python and would cause a SyntaxError if used as variable names. (Select two answers)
Answers:
• continue
• class
Question 10
def modify_list(values):
del values[values[2] // 3]
return values
print(modify_list(my_values))
Question 11
Python
x=1
y=2
x, y, z = x, x, y
z, y, z = x, y, z
print(x, y, z)
Answer: 1 2 2
Question 12
Python
a=1
b=0
a=a^b
b=a^b
a=a^b
print(a, b)
Answer: 0 1
Question 13
Python
def custom_function(value):
if value % 3 == 0:
return 1
else:
return 2
print(custom_function(custom_function(4)))
Answer: 2
Question 14
Question: Consider the following Python code snippet and determine which statement is
correct:
Python
backup_inventory = inventory
del backup_inventory[:]
Answer: Both inventory and backup_inventory contain the same number of items.
Question 15
Question: Consider the following Python code, where only integer inputs are allowed:
Python
print(second_integer)
Which condition1 will ensure that the code runs successfully without a runtime error?
Answer: first_integer must be larger than second_integer (at the point of the modulo
operation, first_integer cannot be zero).
Question 16
Question: Assuming the user enters 5 on the first line and 0 on the second line, what is the
output of the following Python code?
Python
print(second_input + first_input)
Answer: 05
Question 17
Python
Answer: asepbsepc
Question 18
Python
x = 1 // 5 + 1 / 5
print(x)
Answer: 0.2
Question 19
Question: What is the result of trying to execute the following code snippet if sample_tuple
is a tuple that has been previously defined with at least two elements?
Python
sample_tuple[0] = 5
print(sample_tuple)
Question 20
Question: What is the output of the following piece of code if the user enters two lines
containing 2.0 and 4.0 respectively?
Python
x = float(input())
y = float(input())
print(y ** (1 / x))
Answer: 2.0
Question 21
my_list = [1, 2, 3, 4, 5]
value = my_list[2]
value = my_list[value - 1]
print(value)
Answer: 4
Question 22
Python
Answer: 5
Question 23
Question: Which of the following function calls correctly invoke the function named
my_function? (Select two answers)
Python
return x + y
Answers:
• my_function(y=1, x=2)
• my_function(1, 2)
Question 24
x = 10
if x == 10:
print("Ten")
else:
print("Not Ten")
print(x + 1)
Question 25
Python
i=0
while i < 1:
i += 1
print(i)
else:
print("Done")
Answer: One
Question 26
Python
my_tuple = (1, 2, 3)
print(my_tuple)
Answer: (1, 3, 2)
Question 27
Question: Examine the following Python code: print("Hello, world!", "Python is fun!")
Answer: The print() function with two arguments displays two values.
Question 28
Python
dct = {}
dct[1] = 10
dct[2] = 20
for k, v in dct.items():
Answer: 1 2
Question 29
Python
my_list = [1, 2, 3]
del my_list[3]
print(my_list)
Question: How many times will the * symbol be emitted onto the console by the following
code snippet?
Python
count = 0
if element % 2 == 0:
count += 1
print(count * '*')
Answer: Zero
Question 31
Python
my_list.insert(0, my_list.pop(2))
print(my_list)
Question 32
Python
x = 10
y=3
x=y/x
print(x)
Question 33
Python
a = 10
b=0
c=a/b
print(c)
Question 34
Python
print(())
Answer: ()
Question 35
Python
my_tuple = (1, 2)
my_tuple.append(3)
print(my_tuple)
Answer: The code will raise a TypeError exception.