Exercise: Python

Questions for: Functions

How do you define a recursive function?
A:
Using the rec() keyword
B:
Declaring a function inside another function
C:
Specifying a return statement within a loop
D:
A function calling itself
Answer: D
A recursive function is defined by having the function call itself within its own definition.
What will be the result of the following code snippet?
def concatenate_strings(*args):
    return ''.join(args)

result = concatenate_strings('Hello', ' ', 'World!')
print(result)
A:
'Hello World!'
B:
'Hello, World!'
C:
('Hello', ' ', 'World!')
D:
Error
Answer: A
The function concatenate_strings takes variable positional arguments and joins them into a single string.
What is the purpose of the zip() function?
A:
Combines two lists into a dictionary
B:
Combines two lists into a tuple
C:
Iterates over multiple iterables in parallel
D:
Checks if a variable is of a certain type
Answer: C
The zip() function is used to iterate over multiple iterables (e.g., lists) in parallel, creating tuples containing elements from each iterable.
What does the term "docstring" refer to?
A:
A variable that stores documentation
B:
A string containing documentation for a function or module
C:
A string used for debugging purposes
D:
A reserved keyword in Python
Answer: B
A docstring is a string literal that occurs as the first statement in a module, function, class, or method definition and is used for documentation.
What is the purpose of the map() function?
A:
Iterates over elements of a list
B:
Applies a function to each item in an iterable
C:
Checks if a variable is of a certain type
D:
Creates a new list with specified values
Answer: B
The map() function applies a given function to each item in an iterable (e.g., a list).
Ad Slot (Above Pagination)
Quiz