Thursday, November 7, 2024

30+ MCQs on Python Features

Welcome to our Python Features quiz! Features are important constructing blocks in Python programming, permitting you to prepare code, promote reusability, and improve readability. This quiz is designed to guage your proficiency in defining, calling, and using features successfully. Get able to sharpen your expertise and deepen your understanding of Python perform ideas!

Python Functions

30+ MCQs on Python Features

Q1. What’s the main function of a perform in Python?

a) To execute a block of code repeatedly
b) To arrange code into manageable items and promote code reuse
c) To carry out mathematical operations
d) To outline conditional statements

Reply: b

Clarification: Features in Python are primarily used to prepare code into manageable items, promote code reuse, and enhance readability.

Q2. What key phrase is used to outline a perform in Python?

a) def
b) perform
c) outline
d) func

Reply: a

Clarification: The def key phrase is used to outline a perform in Python.

Q3. What’s the function of the return assertion in a perform?

a) To cease the execution of the perform
b) To print a worth to the console
c) To return a worth to the caller
d) To outline a recursive perform

Reply: c

Clarification: The return assertion is used to return a worth from a perform to the caller.

This fall. What would be the output of the next code snippet?

def add(a, b):
    return a + b

outcome = add(3, 5)
print(outcome)

a) 8
b) 3 + 5
c) 35
d) None

Reply: a

Clarification: The perform add takes two arguments and returns their sum, which is then printed.

Q5. Which of the next statements about perform arguments in Python is true?

a) All arguments should have default values
b) Features can not have multiple argument
c) Arguments are handed by worth
d) Arguments can have default values

Reply: d

Clarification: In Python, perform arguments can have default values, permitting them to be omitted when the perform known as.

Q6. What would be the output of the next code snippet?

def greet(identify):
    print("Hi there, " + identify + "!")

greet("Alice")

a) Hi there, Alice!
b) Hi there, identify!
c) Alice
d) None

Reply: a

Clarification: The perform greet takes a reputation argument and prints a greeting message with the offered identify.

Q7. What’s the function of the **kwargs parameter in a Python perform definition?

a) To simply accept a variable variety of positional arguments
b) To simply accept key phrase arguments as a dictionary
c) To specify default values for key phrase arguments
d) To boost an exception

Reply: b

Clarification: The **kwargs parameter permits a perform to just accept key phrase arguments as a dictionary.

Q8. What would be the output of the next code snippet?

def multiply(*args):
    outcome = 1
    for num in args:
        outcome *= num
    return outcome

print(multiply(2, 3, 4))

a) 24
b) 9
c) 10
d) None

Reply: a

Clarification: The perform multiply takes a variable variety of arguments and returns their product.

Q9. What’s the function of the worldwide key phrase in Python?

a) To outline a variable inside a perform
b) To entry a variable outdoors a perform
c) To switch a variable outlined within the world scope from inside a perform
d) To specify a variable as fixed

Reply: c

Clarification: The worldwide key phrase is used to change a variable outlined within the world scope from inside a perform.

Q10. What would be the output of the next code snippet?

x = 5

def modify():
    world x
    x = 10

modify()
print(x)

a) 5
b) 10
c) Error
d) None

Reply: b

Clarification: The perform modify modifies the worldwide variable x, altering its worth to 10.

Q11. What’s the function of the lambda key phrase in Python?

a) To outline nameless features
b) To outline a variable
c) To import modules
d) To deal with exceptions

Reply: a

Clarification: The lambda key phrase is used to create nameless features, that are small, one-line features with out a identify.

Q12. What would be the output of the next code snippet?

sq. = lambda x: x ** 2
print(sq.(5))

a) 10
b) 25
c) 5
d) None

Reply: b

Clarification: The lambda perform sq. squares its enter, so sq.(5) returns 25.

Q13. What’s a recursive perform?

a) A perform that calls itself
b) A perform with a number of return statements
c) A perform that returns a dictionary
d) A perform that takes a number of arguments

Reply: a

Clarification: A recursive perform is a perform that calls itself both immediately or not directly.

Q14. What would be the output of the next code snippet?

def factorial(n):
    if n == 0:
        return 1
    else:
        return n * factorial(n - 1)

print(factorial(5))

a) 5
b) 10
c) 120
d) None

Reply: c

Clarification: The recursive perform factorial computes the factorial of a quantity, so factorial(5) returns 120.

Q15. Which of the next statements about recursive features in Python is true?

a) Recursive features can not have a base case
b) Recursive features all the time end in infinite loops
c) Recursive features are much less memory-efficient than iterative options
d) Recursive features may be extra readable and stylish for sure issues

Reply: d

Clarification: Recursive features may be extra readable and stylish for sure issues, reminiscent of these involving tree constructions or mathematical recursion.

Q16. What would be the output of the next code snippet?

def fibonacci(n):
    if n <= 1:
        return n
    else:
        return fibonacci(n-1) + fibonacci(n-2)

print(fibonacci(6))

a) 6
b) 8
c) 13
d) 21

Reply: c

Clarification: The recursive perform fibonacci computes the Fibonacci sequence, so fibonacci(6) returns 13.

Q17. What’s the function of the docstring in a Python perform?

a) To outline the perform’s identify
b) To supply documentation and details about the perform
c) To specify the perform’s return sort
d) To specify the perform’s parameters

Reply: b

Clarification: The docstring is used to supply documentation and details about the perform, together with its function, parameters, and return values.

Q18. What would be the output of the next code snippet?

def add(a, b):
    """This perform provides two numbers."""
    return a + b

print(add.__doc__)

a) add
b) This perform provides two numbers.
c) a + b
d) None

Reply: b

Clarification: The print(add.__doc__) assertion prints the docstring related to the add perform.

Q19. What’s a decorator in Python?

a) A particular sort of perform that modifies different features
b) A key phrase used to outline a perform
c) An object-oriented programming idea
d) A solution to import modules

Reply: a

Clarification: A decorator is a particular sort of perform that modifies different features, usually by including further performance.

Q20. What would be the output of the next code snippet?

def decorator(func):
    def wrapper():
        print("Earlier than perform execution")
        func()
        print("After perform execution")
    return wrapper

@decorator
def greet():
    print("Hi there!")

greet()

a) Earlier than perform execution
Hi there!
After perform execution
b) Earlier than perform execution
After perform execution
Hi there!
c) Hi there!
Earlier than perform execution
After perform execution
d) Hi there!

Reply: a

Clarification: The decorator perform provides further performance earlier than and after the execution of the greet perform.

Q21. What’s the function of the nonlocal key phrase in Python?

a) To outline a variable inside a perform
b) To entry a variable outdoors a perform
c) To switch a variable outlined within the world scope from inside a perform
d) To switch a variable outlined in an outer perform’s scope from inside a nested perform

Reply: d

Clarification: The nonlocal key phrase is used to change a variable outlined in an outer perform’s scope from inside a nested perform.

Q22. What would be the output of the next code snippet?

def countdown(n):
    if n <= 0:
        print("Executed!")
    else:
        print(n)
        countdown(n - 1)

countdown(3)


a) 3 2 1 Executed!
b) 1 2 3 Executed!
c) Executed! 1 2 3
d) Executed!

Reply: a

Clarification: The countdown perform recursively prints numbers from n right down to 1, then prints “Executed!” when n reaches 0.

Q23. What’s a recursive perform’s base case?

a) The perform name that begins the recursion
b) The perform name that ends the recursion
c) The utmost variety of recursive calls allowed
d) The perform’s return worth

Reply: b

Clarification: The bottom case of a recursive perform is the situation that ends the recursion and prevents infinite recursion.

Q24. What would be the output of the next code snippet?

def squares(n):
    for i in vary(n):
        yield i ** 2

for num in squares(3):
    print(num)

a) 0 1 2
b) 1 4 9
c) 0 2 4
d) 0 1 4

Reply: d

Clarification: The squares generator yields the sq. of every quantity as much as n, so the output is 0, 1, and 4.

Q25. What’s a generator in Python?

a) A perform that generates random numbers
b) A perform that returns a sequence of values lazily
c) A perform that takes one other perform as an argument
d) A perform that raises an exception

Reply: b

Clarification: A generator in Python is a perform that returns a sequence of values lazily, permitting for environment friendly reminiscence utilization.

Q26. What would be the output of the next code snippet?

x = 10

def modify_global():
    world x
    x += 5

modify_global()
print(x)

a) 10
b) 15
c) 5
d) 20

Reply: b

Clarification: The modify_global perform modifies the worldwide variable x, including 5 to its worth.

Q27. Which of the next is true about variable scope in Python?

a) Native variables may be accessed outdoors the perform through which they’re outlined
b) International variables take priority over native variables
c) Variables outlined inside a perform have world scope
d) Variables outlined inside a perform have native scope

Reply: d

Clarification: Variables outlined inside a perform have native scope and may solely be accessed inside that perform.

Q28. What’s a closure in Python?

a) A perform outlined inside one other perform
b) A solution to modify world variables from inside a perform
c) A perform that returns one other perform
d) A solution to deal with exceptions

Reply: a

Clarification: A closure in Python refers to a perform outlined inside one other perform that retains the scope of the enclosing perform.

Q29. What would be the output of the next code snippet?

def energy(base, exponent=2):
    return base ** exponent

result1 = energy(2)
result2 = energy(2, 3)
print(result1, result2)

a) 4 8
b) 4 6
c) 8 4
d) 6 8

Reply: a

Clarification: The perform energy raises the bottom to the exponent, with the default exponent being 2.

Q30. What would be the output of the next code snippet?

def sq.(x):
    return x ** 2

numbers = [1, 2, 3, 4]
squared_numbers = map(sq., numbers)
print(checklist(squared_numbers))

a) [1, 4, 9, 16]
b) [1, 2, 3, 4]
c) [2, 4, 6, 8]
d) [1, 3, 5, 7]

Reply: a

Clarification: The map perform applies the sq. perform to every ingredient within the numbers checklist.

Q31. What would be the output of the next code snippet?

def add(a, b):
    return a + b

def subtract(a, b):
    return a - b

operations = {'add': add, 'subtract': subtract}
result1 = operations['add'](5, 3)
result2 = operations['subtract'](7, 2)
print(result1, result2)

a) 8 5
b) 2 5
c) 5 2
d) 8 7

Reply: a

Clarification: The dictionary operations maps operation names to their corresponding features, that are then invoked with arguments.

Q32. What would be the output of the next code snippet?

def multiply(*args):
    outcome = 1
    for num in args:
        outcome *= num
    return outcome

print(multiply(2, 3, 4))

a) 24
b) 9
c) 10
d) None

Reply: a

Clarification: The perform multiply takes a variable variety of arguments and returns their product.

Q33. What would be the output of the next code snippet?

def is_even(n):
    return n % 2 == 0

numbers = [1, 2, 3, 4, 5, 6]
even_numbers = filter(is_even, numbers)
print(checklist(even_numbers))

a) [1, 3, 5]
b) [2, 4, 6]
c) [1, 2, 3, 4, 5, 6]
d) []

Reply: b

Clarification: The filter perform applies the is_even perform to every ingredient within the numbers checklist and returns these for which the perform returns True.

Congratulations on finishing the Python Features quiz! Features are the cornerstone of Python programming, enabling you to encapsulate logic, promote reusability, and improve code group. By mastering perform definition, parameter dealing with, return values, and scope administration, you’re well-equipped to put in writing clear, modular, and environment friendly Python code. Preserve honing your expertise and exploring superior perform ideas to develop into a proficient Python programmer. Effectively accomplished, and maintain coding!

You can too enroll in out free Python Course Immediately!

Learn our extra articles associated to MCQs in Python:

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles