Python Collection MCQ Questions and Answers
Q. What is the output of the code?
def greet(name):
return f"Hello, {name}!"
print(greet("Alice"))
A. HelloB. Hello, Alice
C. Hello, World
D. None of the above
Q. What is the purpose of the "pass" statement in Python?
A. To indicate a placeholder for future codeB. To indicate that a function has no return value
C. To skip the execution of a particular block of code
D. To indicate that a function has no parameters
Q. What is the output of the code?
def add_numbers(a, b):
return a + b
result = add_numbers(3, 4)
print(result)
A. 6B. 7
C. None
D. An error occurs
Q. What is the purpose of the "if __name__ == '__main__':" block in Python?
A. To indicate the main execution block of the scriptB. To import modules and packages
C. To define global variables
D. To handle command-line arguments
Q. What is the purpose of the "try-except" block in Python?
def calculate_area(radius):
pi = 3.14159
area = pi * radius ** 2
return area
radius = 5
area = calculate_area(radius)
print(area)
A. To handle errors and exceptionsB. To execute code conditionally
C. To repeat a block of code multiple times
D. To define custom error messages
Q. What is the output of the code?
def greet(name):
return "Hello, " + name
name = "Bob"
greeting = greet(name)
print(greeting)
A. Hello, BobB. Bob
C. Hello
D. None of the above
Q. What is the purpose of the "for" loop in Python?
A. To repeat a block of code a specified number of timesB. To iterate over the elements of a sequence (list, tuple, string, etc.)
C. To execute code as long as a condition is true
D. To handle exceptions and errors