Here’s a set of 100 multiple-choice questions (MCQs) related to Python programming. Please note that the answers are provided immediately after each question.
1) Which of the following is the correct way to define a function in Python?
a) func function_name():
b) def function_name()
c) define function_name()
d) def function_name():
Answer: d) def function_name():
2) What is the purpose of the return statement in a Python function?
a) It prints the output of the function.
b) It terminates the function execution.
c) It returns a value from the function.
d) It is used for error handling.
Answer: c) It returns a value from the function.
3) Which of the following is an example of a valid function call in Python?
a) function_name[]
b) function_name()
c) function_name{}
d) function_name<>
Answer: b) function_name()
4) What is the scope of a local variable in Python?
a) It is accessible throughout the entire program.
b) It is accessible only within the function it is defined.
c) It is accessible within the module where it is defined.
d) It is accessible within the class where it is defined.
Answer: b) It is accessible only within the function it is defined.
5) Which keyword is used to pass a variable number of arguments to a Python function?
a) pass
b) varargs
c) nonlocal
d) *args
Answer: d) *args
6) What does the lambda keyword do in Python?
a) It defines a multiline function.
b) It creates an anonymous function.
c) It imports a module.
d) It defines a global variable.
Answer: b) It creates an anonymous function.
7) What is the output of the following code snippet?
def increment(x):
return x + 1
result = increment(5)
print(result)
a) 5
b) 6
c) 1
d) Error: increment is not defined
Answer: b) 6
8) Which of the following is the correct way to define a default argument in a Python function?
a) def my_func(default):
b) def my_func(default=0):
c) def my_func(0=default):
d) def my_func:
Answer: b) def my_func(default=0):
9) What is the output of the following code snippet?
def greet(name="Guest"):
print("Hello, " + name + "!")
greet("Alice")
greet()
a) Hello, Alice!
Hello, Guest!
b) Hello, Alice!
c) Hello, Guest!
Hello, Guest!
d) Error: greet() takes no arguments
Answer: a) Hello, Alice!
Hello, Guest!
10) Which of the following is not a valid way to pass arguments to a Python function?
a) Positional arguments
b) Keyword arguments
c) Default arguments
d) None of the above
Answer: d) None of the above
11) What is the output of the following code snippet?
def calculate_sum(*args):
return sum(args)
result = calculate_sum(1, 2, 3, 4, 5)
print(result)
a) 1
b) 15
c) (1, 2, 3, 4, 5)
d) Error: sum() takes exactly one argument
Answer: b) 15
12) Which keyword is used to access the superclass in Python?
a) parent
b) super
c) base
d) this
Answer: b) super
13) What is the purpose of the pass statement in Python?
a) It raises an exception.
b) It terminates the program.
c) It is used for type hinting.
d) It represents a placeholder for future code.
Answer: d) It represents a placeholder for future code.
14) What is the output of the following code snippet?
def square(x):
return x ** 2
numbers = [1, 2, 3, 4, 5]
squared_numbers = list(map(square, numbers))
print(squared_numbers)
a) [1, 4, 9, 16, 25]
b) [2, 4, 6, 8, 10]
c) [1, 2, 3, 4, 5]
d) [1, 8, 27, 64, 125]
Answer: a) [1, 4, 9, 16, 25]
15) What is the output of the following code snippet?
def my_func(a, b=0, c=0):
return a + b + c
result = my_func(2, c=3)
print(result)
a) 0
b) 2
c) 5
d) Error: my_func() missing 1 required positional argument: ‘a’
Answer: c) 5
16) Which of the following is the correct syntax to call a function named my_func from the module my_module?
a) call my_module.my_func()
b) my_module.call my_func()
c) my_module.my_func()
d) my_func.my_module()
Answer: c) my_module.my_func()
17) What is the output of the following code snippet?
def update_list(my_list):
my_list.append(4)
numbers = [1, 2, 3]
update_list(numbers)
print(numbers)
a) [1, 2, 3, 4]
b) [1, 2, 3]
c) [4, 1, 2, 3]
d) [1, 2, 3, [4]]
Answer: a) [1, 2, 3, 4]
18) Which of the following is the correct way to define a docstring in Python?
a) “This is a docstring.”
b) ‘This is a docstring.’
c) “””This is a docstring.”””
d) ‘This is a docstring.’
Answer: c) “””This is a docstring.”””
19) What is the output of the following code snippet?
def multiply(*args):
result = 1
for num in args:
result *= num
return result
result = multiply(2, 3, 4)
print(result)
a) 24
b) 10
c) 9
d) 12
Answer: a) 24
20) Which of the following is not a valid way to define a Python function?
a) def my_func():
pass
b) def my_func
c) def my_func(arg1, arg2):
pass
d) def my_func(arg=0):
pass
Answer: b) def my_func
21) What is the output of the following code snippet?
def divide(a, b):
try:
result = a / b
return result
except ZeroDivisionError:
return "Cannot divide by zero."
print(divide(10, 2))
print(divide(5, 0))
a) 5.0
Cannot divide by zero.
b) 5.0
Error: Division by zero.
c) 0.5
Cannot divide by zero.
d) 0.5
Error: Division by zero.
Answer: a) 5.0
Cannot divide by zero.
22) What is the purpose of the global keyword in Python?
a) It defines a global variable.
b) It imports a module.
c) It terminates the program.
d) It specifies the variable scope.
Answer: a) It defines a global variable.
23) What is the output of the following code snippet?
def is_even(num):
return num % 2 == 0
numbers = [1, 2, 3, 4, 5]
even_numbers = list(filter(is_even, numbers))
print(even_numbers)
a) [2, 4]
b) [1, 3, 5]
c) [True, False, True, False, True]
d) [1, 2, 3, 4, 5]
Answer: a) [2, 4]
24) What is the purpose of the name attribute in Python?
a) It represents the name of the current module.
b) It represents the name of the current class.
c) It represents the name of the current function.
d) It represents the name of the current instance.
Answer: a) It represents the name of the current module.
25) What is the output of the following code snippet?
def greet(name):
print("Hello, " + name + "!")
greet(name="Alice")
a) “Hello, name!”
b) “Hello,!”
c) “Hello, Alice!”
d) Error: greet() got an unexpected keyword argument ‘name’
Answer: c) “Hello, Alice!”
26) What is the output of the following code snippet?
def my_func(x, y, z):
return x + y + z
numbers = [1, 2, 3]
result = my_func(*numbers)
print(result)
a) 6
b) [1, 2, 3]
c) Error: my_func() takes exactly 3 arguments
d) Error: my_func() argument after * must be an iterable
Answer: c) Error: my_func() takes exactly 3 arguments
27) Which of the following statements is true about recursive functions?
a) Recursive functions cannot have a base case.
b) Recursive functions can only be used for mathematical calculations.
c) Recursive functions can call themselves.
d) Recursive functions are more efficient than iterative functions.
Answer: c) Recursive functions can call themselves.
28) What is the output of the following code snippet?
def greet():
print("Hello!")
result = greet()
print(result)
a) “Hello!”
b) None
c) Error: greet() does not return a value
d) Error: greet() is not defined
Answer: a) “Hello!”
29) What is the output of the following code snippet?
def power(base, exponent):
return base ** exponent
result = power(2, 3)
print(result)
a) 5
b) 8
c) 6
d) 2^3
Answer: b) 8
30) Which keyword is used to raise an exception in Python?
a) except
b) raise
c) try
d) assert
Answer: b) raise
31) What is the output of the following code snippet?
def add(a, b):
return a + b
def multiply(a, b):
return a * b
result = multiply(add(2, 3), 4)
print(result)
a) 14
b) 20
c) 9
d) 12
Answer: a) 14
32) What is the purpose of the init method in a Python class?
a) It is called when an object is created from the class and initializes its attributes.
b) It is used to define the class hierarchy.
c) It is called when an object is destroyed and performs cleanup actions.
d) It is used to define class methods.
Answer: a) It is called when an object is created from the class and initializes its attributes.
33) What is the output of the following code snippet?
def calculate_sum(*args, **kwargs):
result = sum(args) + sum(kwargs.values())
return result
result = calculate_sum(1, 2, 3, a=4, b=5)
print(result)
result = calculate_sum(1, 2, 3, a=4, b=5)
print(result)
a) 10
b) 15
c) 6
d) 9
Answer: b) 15
34) Which of the following is the correct way to call a method named my_method on an object my_object?
a) my_method(my_object)
b) my_object.call(my_method)
c) my_method.call(my_object)
d) my_object.my_method()
Answer: d) my_object.my_method()
35) What is the output of the following code snippet?
def my_func():
return 1, 2, 3
result = my_func()
print(result)
a) 1, 2, 3
b) [1, 2, 3]
c) (1, 2, 3)
d) (1)
Answer: c) (1, 2, 3)
36) What is the output of the following code snippet?
python
Copy code
def greet(name):
print(“Hello, ” + name + “!”)
greet(“Bob”, “Alice”)
a) “Hello, Bob!”
b) “Hello, Alice!”
c) “Hello, Bob! Hello, Alice!”
d) Error: greet() takes exactly 1 argument
Answer: d) Error: greet() takes exactly 1 argument
37) Which of the following is the correct way to define a default argument in a Python function?
a) def my_func(default):
b) def my_func(default=0):
c) def my_func(0=default):
d) def my_func:
Answer: b) def my_func(default=0):
38) Which of the following statements is true about recursive functions?
a) Recursive functions cannot have a base case.
b) Recursive functions can only be used for mathematical calculations.
c) Recursive functions can call themselves.
d) Recursive functions are more efficient than iterative functions.
Answer: c) Recursive functions can call themselves.
39) What is the purpose of the init method in a Python class?
a) It is called when an object is created from the class and initializes its attributes.
b) It is used to define the class hierarchy.
c) It is called when an object is destroyed and performs cleanup actions.
d) It is used to define class methods.
Answer: a) It is called when an object is created from the class and initializes its attributes.
40) What is the output of the following code snippet?
def calculate_sum(*args, **kwargs):
result = sum(args) + sum(kwargs.values())
return result
result = calculate_sum(1, 2, 3, a=4, b=5)
print(result)
a) 9
b) 10
c) 15
d) 6
Answer: b) 10
41) What is the output of the following code snippet?
def power(base, exponent=2):
return base ** exponent
result = power(3)
print(result)
a) 3
b) 6
c) 8
d) 9
Answer: d) 9
42) What is the output of the following code snippet?
def greet(name):
print("Hello, " + name + "!")
greet("Alice")
greet("Bob")
greet("Charlie")
a) “Hello, Alice!”
“Hello, Bob!”
“Hello, Charlie!”
b) “Hello, name!”
“Hello, name!”
“Hello, name!”
c) “Hello,!”
“Hello,!”
“Hello,!”
d) Error: greet() takes no arguments
Answer: a) “Hello, Alice!”
“Hello, Bob!”
“Hello, Charlie!”
43) What is the output of the following code snippet?
def calculate_sum(a, b, c):
return a + b + c
result = calculate_sum(a=1, b=2, c=3)
print(result)
a) 1
b) 2
c) 3
d) 6
Answer: d) 6
44) What is the output of the following code snippet?
def greet(name="Guest"):
print("Hello, " + name + "!")
greet("Alice")
greet()
a) “Hello, Alice!”
“Hello, Guest!”
b) “Hello, Alice!”
c) “Hello, Guest!”
“Hello, Guest!”
d) Error: greet() takes no arguments
Answer: a) “Hello, Alice!”
“Hello, Guest!”
45) What is the output of the following code snippet?
def greet(name):
print("Hello, " + name + "!")
greet(name="Alice")
greet()
a) “Hello, name!”
Error: greet() missing 1 required positional argument: ‘name’
b) “Hello, name!”
“Hello,!”
c) “Hello, Alice!”
“Hello,!”
d) Error: greet() takes no arguments
Answer: c) “Hello, Alice!”
“Hello,!”
46) What is the output of the following code snippet?
def greet(name):
print("Hello, " + name + "!")
greet("Alice")
greet("Bob")
a) “Hello, Alice!”
“Hello, Bob!”
b) “Hello, name!”
“Hello, name!”
c) “Hello,!”
“Hello,!”
d) Error: greet() takes no arguments
Answer: a) “Hello, Alice!”
“Hello, Bob!”
47) What is the output of the following code snippet?
def greet(name="Guest"):
print("Hello, " + name + "!")
greet("Alice")
greet()
greet("Bob")
a) “Hello, Alice!”
“Hello, Guest!”
“Hello, Bob!”
b) “Hello, Alice!”
“Hello, Guest!”
c) “Hello, Alice!”
“Hello, Bob!”
“Hello, Guest!”
d) “Hello, Guest!”
“Hello, Guest!”
“Hello, Guest!”
Answer: a) “Hello, Alice!”
“Hello, Guest!”
“Hello, Bob!”