True or False:
A variable must be declared before it can be used in Python.
False
Python does not require explicit declaration of variables before using them. Variables are created when they are first assigned a value.
Which of the following would you use if an element is to be removed from a specific index in a list?
a) a remove method
b) a slice method
c) a del statement
d) an index method
c) a del statement
Which of the following is the correct if clause to determine whether choice is anything other than 10?
a) if choice != 10
b) if not(choice < 10 and choice > 10):
c) if choice != 10:
d) if choice <> 10:
c) if choice != 10:
What values will list2 contain after the following code executes?
list1 = [1, 2, 3]
list2 = [item + 1 for item in list1]
a) [1, 2, 3]
b) [[1, 2, 3],[2, 3, 4],[3, 4, 5]]
c) [6, 7, 8]
d) [2, 3, 4]
d) [2, 3, 4]
What is a lambda function in Python?
a) A function with multiple parameters
b) A one-liner anonymous function
c) A function with a long list of statements
d) A built-in Python function
b) A one-liner anonymous function
After the execution of the following statement, what values will be assigned to the variables x, y, and z?
x, y, z = 9, 8, 7
a) x will be assigned 9, y will be assigned 8, and z will be assigned 7.
b) First 9 is assigned to all three variables, then 8 is assigned to all three, and then 7 is assigned to all three. After the statement finishes executing, all three variables will be assigned 7.
c) x will be assigned 7, y will be assigned 8, and z will be assigned 9.
d) All three variables will be assigned 9 + 8 + 7.
a) x will be assigned 9, y will be assigned 8, and z will be assigned 7.
What is the result of the following expression in Python?
round(int(round (10.123456780, 3)))
a. 10
b. 10.123
c. .123456780
6. The provided code shows a syntax error.
a. 10
What values will list2 contain after the following code executes?
list1 = [1, 10, 3, 6]
list2 = [item * 2 for item in list1 if item > 5]
a) [[1, 10, 3, 6],[1, 10, 3, 6]]
b) [20, 12]
c) [1, 20, 3, 12]
d) [2, 20, 6, 12]
b) [20, 12]
Which list will be generated by the variable number after the following code is executed?
for number in range(-1, -20, -3):
a) [-3, -4, -5, -6, ..., -20]
b) [-1, -20, -3]
c) [-1, -4, -7, -10, -13, -16, -19]
d) [0]
c) [-1, -4, -7, -10, -13, -16, -19]
Which of the following is a correct way to define a Python function with parameters?
a) def my_function:
b) function my_function():
c) def my_function(param1, param2):
d) function my_function(param1, param2)
c) def my_function(param1, param2):
What will be the value of the variable list after the following code executes?
list = [1, 2, 3, 4]
list [3] = 10
a) [1, 2, 3, 10]
b) [1, 10, 10, 10]
c) [1, 2, 10, 4]
d) [10, 10, 10]
a) [1, 2, 3, 10]
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
a) 8
b) True
c) 5
d) False
d) False
What will be the value of result after the "f" function is called with the argument 5?
def f(n):
if n < 0 :
return 1
else:
return f(n)
result = f(2)
print (result)
a) 1
b) 2
c) f(2)
d) The provided code will result in a "RecursionError" due to an infinite recursion.
d) The provided code will result in a "RecursionError" due to an infinite recursion.
The function f is defined to call itself (return f(n)), but the recursion is not controlled by a base case that eventually stops the recursion.
How many times will the following loop iterate?
count = 1
while count > 5:
count += 1
break
else:
count -= 1
print(count)
a. 5
b. 2
c. 1
d. 0
d. 0
count is initialized to 1. The while loop condition count > 5 is initially False, so the loop is not executed. The else block is executed, where count is decremented by 1. The print(count) statement outputs the value of count, which is now 0.
How do you declare a function "greet" that takes a name as a parameter and prints a greeting message?
a) def greet:
b) def greet(name):
c) def greet():
d) def greet(name) -> str:
b) def greet(name):
What will be the value of the variable list after the following code executes?
list = [1, 2]
list = list * 3
a) [3, 6]
b) [1, 2, 1, 2, 1, 2]
c) [1, 2], [1, 2], [1, 2]
d) [1, 2] * 3
c) [1, 2], [1, 2], [1, 2]
Which variable is considered local to a function in Python?
a) A variable declared outside any function
b) A variable declared within the function using the local keyword
c) A variable declared within the function
d) A variable declared with the global keyword
c) A variable declared within the function
What is the result of the following code?
num = 4
while num < 8:
num += 1
pass
print (num)
a) 4
b) 8
c) 4, 5, 6, 7
d) The provided code will not produce any output because of the "pass" statement.
b) 8
What will be the value of the result list?
numbers = [2, 5, 8, 11, 14, 17]
result = [x * 3 for x in numbers if x % 2 > 0]
a) [15, 33, 51]
b) [6, 15, 24, 33, 42, 51]
c) [6, 24, 42]
d) []
a) [15, 33, 51]
What is the result of the following code?
square = lambda x: x ** 2
result = square(4)
print(result)
a) 8
b) 12
c) 16
d) 20
c) 16
What is the output of the following code?
n = [5, -4, 3, -2, 1]
s = list(map(lambda x: x**2, n))
print(s)
a. [25, 16, 9, 4, 1]
b. [10, -8, 6, -4, 2]
c. [25, -16, 9, -4, 1]
d. [10, 8, 6, 4, 2]
a. [25, 16, 9, 4, 1]
What is the output of the following code?
value = 123.456789
precision = 3
formatted_value = f'{value:.{precision}f}'
if value > 100:
formatted_value = formatted_value.upper()
else:
formatted_value = formatted_value.lower()
print(f'The formatted value is: {formatted_value}')
a) 123.456789
b) 123.457
c) 123
d) .456
b) 123.457
What would be the output of the following code?
num1 = 15
num2 = 20
condition = True
if condition:
if num1 > num2:
print("A")
elif num1 == num2:
print("B")
else:
print("C")
else:
print("D")
a) A
b) B
c) C
d) D
c) C
What will be printed by the above code?
count = 2
while count > 10:
print(count, end=" ")
count -= 1
a) 2, 3, 4, 5, 6, 7, 8, 9, 10
b) 11, 12, 13, 14, 15, ...
c) 10, 9, 8, 7, 6, 5, 4, 3, 2
d) The provided code will not produce any output.
d) The provided code will not produce any output.
The reason is that the while loop condition count > 10 is initially False because count is initialized to 2. As a result, the code inside the while loop will not be executed, and no output will be printed.
What is the output of the following code?
def concatenate_strings(*args):
return ''.join(args)
result = concatenate_strings("Hello", " ", "World")
print(result)
a) HelloWorld
b) Hello World
c) ['Hello', ' ', 'World']
d) ('Hello', ' ', 'World')
b) Hello World