Exercise: Python

Questions for: Functions

What is the purpose of the ord() function?
A:
Returns the ASCII value of a character
B:
Computes the power of a number
C:
Converts a string to uppercase
D:
Checks if a variable is of a certain type
Answer: A
The ord() function returns the ASCII value of a character.
What will be the output of the following code snippet?
def multiply_by_two(x):
    return x * 2

numbers = [1, 2, 3, 4]
squared_numbers = list(map(multiply_by_two, numbers))
print(squared_numbers)
A:
[1, 2, 3, 4]
B:
[2, 4, 6, 8]
C:
[1, 4, 9, 16]
D:
Error
Answer: B
The map() function applies the multiply_by_two function to each element in the numbers list.
What does the term "anonymous function" refer to?
A:
A function with no parameters
B:
A function that doesn't return any value
C:
A function without a name
D:
A function with a single line of code
Answer: C
An anonymous function in Python refers to a function without a name, often created using the lambda keyword.
What does the term "closure" mean?
A:
A function that calls itself
B:
A function that takes a variable number of arguments
C:
A function object that has access to variables in its lexical scope
D:
A function with a return type specified
Answer: C
A closure is a function object that has access to variables in its lexical scope, even when the function is called outside that scope.
What is the purpose of the filter() function?
A:
Filters out odd numbers from a list
B:
Filters out even numbers from a list
C:
Filters elements of an iterable based on a function's result
D:
Checks if a variable is of a certain type
Answer: C
The filter() function is used to filter elements of an iterable based on whether a function returns True or False.
Ad Slot (Above Pagination)
Quiz