Exercise: Python

Questions for: Functions

How do you define a default value for a function parameter?
A:
Using the default() keyword
B:
Using the set_default method
C:
Using the default_value parameter
D:
Using the parameter=default_value syntax
Answer: D
Default values for function parameters are specified using the parameter=default_value syntax.
What will be the output of the following code snippet?
def square(x):
    return x ** 2

numbers = [1, 2, 3, 4]
squared_numbers = list(map(square, numbers))
print(squared_numbers)
A:
[1, 4, 9, 16]
B:
[1, 2, 3, 4]
C:
[2, 4, 6, 8]
D:
Error
Answer: A
The map() function applies the square function to each element in the numbers list.
What is the purpose of the min() function?
A:
Finds the minimum value in a list
B:
Rounds a floating-point number down to the nearest integer
C:
Checks if a variable is of a certain type
D:
Converts a string to lowercase
Answer: A
The min() function is used to find the minimum value among the elements of an iterable, such as a list.
What does the term "variable scope" refer to?
A:
The lifetime of a variable
B:
The region of the program where a variable can be accessed
C:
The data type of a variable
D:
The value assigned to a variable
Answer: B
Variable scope refers to the part of the program where a variable is accessible.
What is the purpose of the __init__() method in Python classes?
A:
Initializes the class object
B:
Declares a new instance variable
C:
Defines the class constructor
D:
Prints the class attributes
Answer: C
The __init__() method is a special method in Python classes that is automatically called when a new instance of the class is created. It is used to initialize the object's attributes.
Ad Slot (Above Pagination)
Quiz