PCEP Exam Practice: Default Parameter Values in Python Functions
PCEP Certification Practice Test - Questions, Answers and Explanations
Below is a set of 25 questions for the Certified Entry-Level Python Programmer (PCEP) examination focusing on the subtopic "default parameter values." The questions use various formats, including single- and multiple-select questions, fill-in-the-gap, code fill, code insertion, sorting, and more.
Question 1: What is the purpose of default parameter values in Python functions?
- To enforce argument passing in the correct order.
- To allow a function to be called without explicitly providing all arguments.
- To restrict the number of arguments a function can accept.
- To allow only positional arguments in a function.
Answer: B) To allow a function to be called without explicitly providing all arguments.
Explanation: Default parameter values allow you to define default values for parameters, so the function can be called without passing all arguments.
Question 2: Which of the following are true about default parameter values? (Choose all that apply)
- Default parameter values must be specified last in the function definition.
- Default parameters can be overridden by passing values during the function call.
- Default parameter values are optional when calling the function.
- All function parameters must have default values.
Answer: A) Default parameter values must be specified last in the function definition.
B) Default parameters can be overridden by passing values during the function call.
C) Default parameter values are optional when calling the function.
Explanation: Default parameters must come after non-default parameters, and they can be overridden when calling the function. Only parameters with default values are optional during the function call.
Question 3: Complete the code to define a function greet with a default parameter name="Guest".
def greet(______="Guest"): print("Hello, " + name)▼
Answer: name
Explanation: The parameter name is assigned a default value of "Guest", so if no argument is passed, the default value is used.
Question 4: What will be the output of the following code?
def add(a, b=5): return a + b result = add(3) print(result)
- 8
- 3
- 5
- Error
Answer: A) 8
Explanation: The function add(a, b=5) is called with one argument (3), and the default value of b is used, resulting in 3 + 5 = 8.
Question 5: Insert the correct default value for the parameter greeting in the function greet.
def greet(name, greeting=______): print(greeting + ", " + name) greet("Alice")▼
Answer: "Hello"
Explanation: The default value for greeting is "Hello", so if no value is passed for greeting, it will default to "Hello".
Question 6: What will the following code output?
def multiply(x, y=2): return x * y print(multiply(4))
- 8
- 4
- 2
- Error
Answer: A) 8
Explanation: The function multiply(x, y=2) is called with x=4, and since no value for y is passed, it defaults to 2. The result is 4 * 2 = 8.
Question 7: Which of the following statements are correct about defining and calling functions with default parameter values? (Choose all that apply)
- You can call the function without providing arguments for parameters with default values.
- Parameters with default values must always be provided during the function call.
- Default values can be overridden by providing arguments during the function call.
- Non-default parameters must come before default parameters in the function definition.
Answer: A) You can call the function without providing arguments for parameters with default values.
C) Default values can be overridden by providing arguments during the function call.
D) Non-default parameters must come before default parameters in the function definition.
Explanation: Parameters with default values are optional during function calls, and their values can be overridden. Non-default parameters must appear before default ones in the function definition.
Question 8: Arrange the steps to define and call a function with default parameter values.
- Print the result.
- Define the function with a default parameter value.
- Call the function without passing the default parameter.
- Override the default parameter by passing a value during the function call.
Answer: 2, 3, 4, 1
Explanation: First, define the function with a default parameter, call it without passing the default parameter, override the default parameter in another call, and finally print the result.
Question 9: Complete the code to define a function subtract with a default parameter b=2.
def subtract(a, ______=2): return a - b▼
Answer: b
Explanation: The parameter b is given a default value of 2, so if no argument is passed for b, the default value is used.
Question 10: What will be the output of the following code?
def greet(name="Guest", greeting="Hello"): print(greeting + ", " + name) greet()
- Hello, Guest
- Guest, Hello
- Hello,
- Error
Answer: A) Hello, Guest
Explanation: The function greet() is called without any arguments, so both default values "Hello" and "Guest" are used in the output.
Question 11: Insert the correct default value for the parameter age in the function person_info.
def person_info(name, age=______): print(name + " is " + str(age) + " years old") person_info("Alice")▼
Answer: 30
Explanation: The default value for age is set to 30, so if no value is provided for age, the default of 30 is used.
Question 12: What will the following code output?
def calculate_area(length, width=10): return length * width print(calculate_area(5))
- 50
- 10
- 15
- Error
Answer: A) 50
Explanation: The function calculate_area(5) is called with one argument for length, and the default value for width is used. The result is 5 * 10 = 50.
Question 13: Which of the following function calls are valid for the following function definition? (Choose all that apply)
def display_info(name, age=25): print(name + " is " + str(age) + " years old")
- display_info("Bob")
- display_info("Bob", 30)
- display_info(name="Alice", age=35)
- display_info()
Answer:A) display_info("Bob")
B) display_info("Bob", 30)
C) display_info(name="Alice", age=35)
Explanation: All calls except display_info() are valid. The first argument name is mandatory, but the second argument age is optional and defaults to 25 if not provided.
Question 14: Arrange the steps to define and call a function with a default parameter value.
- Define the function with a default parameter.
- Call the function without providing the default parameter.
- Call the function and override the default parameter.
- Print the results of both calls.
Answer: 1, 2, 3, 4
Explanation: First, define the function with a default parameter, then call it without the default parameter and override the default parameter in another call, and finally print the results.
Question 15: Complete the code to define a function multiply with default parameter y=1.
def multiply(x, ______=1): return x * y▼
Answer: y
Explanation: The parameter y is assigned a default value of 1, so if no argument is passed for y, it will use this default value.
Question 16: What will be the output of the following code?
def greet(name, greeting="Hi"): print(greeting + ", " + name) greet("Alice", "Hello")
- Hi, Alice
- Hello, Alice
- Hello,
- Error
Answer: B) Hello, Alice
Explanation: The function greet() is called with both arguments, so the default value "Hi" is overridden by "Hello".
17. Insert the correct default value for the parameter z in the function add.
def add(x, y, z=______): return x + y + z▼
Answer: 0
Explanation: The default value for z is 0, meaning the function will use this value if no argument for z is passed.
Question 18: Which of the following function calls will result in using the default value?
def subtract(x, y=5): return x - y
- subtract(10, 3)
- subtract(x=7)
- subtract(4, y=6)
- subtract(5)
Answer: B) subtract(x=7)
D) subtract(5)
Explanation: The function subtract(x=7),the default value for y is used since only x is provided. The function subtract(5) uses the default value of y=5 because no value for y is provided, resulting in 5 - 5.
Question 19: Which of the following function definitions are valid regarding default parameter values? (Choose all that apply)
- def my_func(a, b=2, c):
- def my_func(a, b=2, c=3):
- def my_func(a, b=1):
- def my_func(a=5, b=6):
Answer: B) def my_func(a, b=2, c=3):
C) def my_func(a, b=1):
D) def my_func(a=5, b=6):
Explanation: In a function, parameters with default values must come after non-default parameters. Option A is invalid because c does not have a default value but comes after b, which does.
Question 20: Arrange the steps to define and call a function that multiplies two numbers with a default value for the second number.
- Call the function with both arguments provided.
- Define the function with the second parameter having a default value.
- Call the function with only one argument.
- Print the results of both calls.
Answer: 2, 1, 3, 4,
Explanation: First, define the function with a default parameter, then call it with both arguments and with one argument, and finally print the results of both calls.
Question 21: Complete the code to call the function greet with a default greeting value.
def greet(name, greeting="Hi"): print(greeting + ", " + name) greet("John")▼
Answer: "Hi, John"
Explanation: Since only the name argument is provided, the function uses the default greeting "Hi".
Question 22: What will be the output of the following code?
def power(base, exp=2): return base ** exp print(power(3))
- 9
- 6
- 3
- Error
Answer: A) 9
Explanation: The function power(base, exp=2) is called with base=3, and the default value of exp=2 is used, resulting in 3^2 = 9.
Question 23: Insert the correct default parameter value to define a function divide where the second argument defaults to 1.
def divide(x, y=______): return x / y▼
Answer: 1
Explanation: The default value for y is set to 1, so if no value is passed for y, the division will still work.
Question 24: What will be the result of the following code?
def greet(name="Guest", greeting="Hi"): print(greeting + ", " + name) greet()
- Hi, Guest
- Guest, Hi
- Hello,
- Error
Answer: A) Hi, Guest
Explanation: The function greet() is called with no arguments, so both default values ("Hi" and "Guest") are used in the output.
Question 25: Which of the following are valid ways to call the function def add(a, b=5):? (Choose all that apply)
- add(2)
- add(3, 4)
- add(a=1)
- add(b=6)
Answer: A) add(2)
B) add(3, 4)
C) add(a=1)
Explanation: The function add(a, b=5) requires at least one argument for a, and b has a default value. Option D is invalid because a is required.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/python/certificate/functions-and-exceptions-default-parameter-values.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics