Python Programming - Functions

Exercise : Functions - General Questions
  • Functions - General Questions
76.
What does the zip(*iterables) function do?
It compresses files in a directory.
It combines multiple iterables element-wise into tuples.
It filters elements from an iterable based on a condition.
It creates a zip archive of files.
Answer: Option
Explanation:
The zip(*iterables) function in Python combines multiple iterables element-wise into tuples.

77.
How can you define a lambda function that adds two numbers?
lambda x, y: x + y
def add(x, y): return x + y
lambda add(x, y): x + y
lambda add: x + y
Answer: Option
Explanation:
The correct syntax for a lambda function that adds two numbers is lambda x, y: x + y.

78.
How can you use the filter function to filter out even numbers from a list?
filter(lambda x: x % 2 != 0, my_list)
filter(lambda x: x % 2 == 0, my_list)
filter(is_even, my_list)
filter(is_odd, my_list)
Answer: Option
Explanation:
The correct usage of the filter function to filter out even numbers is filter(lambda x: x % 2 != 0, my_list).

79.
How can you define a generator function that yields squares of numbers up to a given limit?
def square_generator(limit):
    for i in range(limit):
        yield i**2
generator square_generator(limit):
    for i in range(limit):
        return i**2
def square_generator(limit):
    return (i**2 for i in range(limit))
generator square_generator(limit):
    return [i**2 for i in range(limit)]
Answer: Option
Explanation:
The correct definition for a generator function that yields squares of numbers up to a given limit is shown in option A.

80.
What is the purpose of the functools.partial function?
To create partial functions with default arguments.
To create decorators for functions.
To create anonymous functions.
To create static methods in a class.
Answer: Option
Explanation:
The functools.partial function is used to create partial functions with default arguments.