w3resource

PCEP Certification Practice Test: Positional, Keyword, and Mixed Arguments

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 "positional, keyword, and mixed argument passing." 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 positional argument passing in Python?

  1. Passing arguments without specifying the parameter names.
  2. Passing arguments by specifying the parameter names.
  3. Passing arguments in any order.
  4. Passing arguments using default values.

Answer: A) Passing arguments without specifying the parameter names.

Explanation: Positional argument passing means arguments are passed based on their position in the function call, without specifying parameter names.

Question 2: Which of the following are true about keyword argument passing in Python? (Choose all that apply)

  • You specify the argument name along with the value.
  • You must pass arguments in the exact order defined in the function.
  • You can pass arguments in any order as long as the parameter names are specified.
  • Keyword arguments must always come after positional arguments.

Answer: A) You specify the argument name along with the value.
C) You can pass arguments in any order as long as the parameter names are specified.
D) Keyword arguments must always come after positional arguments.

Explanation: In keyword argument passing, the parameter name is specified, allowing arguments to be passed in any order. However, keyword arguments must follow positional arguments.

Question 3: Complete the code to define a function display_info that accepts two positional arguments name and age.

def display_info(______, ______):
    print(name + " is " + str(age) + " years old")

Answer: name, age

Explanation: The function accepts two positional arguments name and age, which are used in the print statement.

Question 4: What will be the output of the following code?

def add(x, y):
    return x + y

result = add(4, 5)
print(result)
  1. 45
  2. 9
  3. 4
  4. Error

Answer: B) 9

Explanation: The function add(x, y) is called with positional arguments 4 and 5, and the result of their sum is 9.

Question 5: Insert the correct keyword arguments to call the function greet with name="Alice" and greeting="Hi".

def greet(name, greeting="Hello"):
    print(greeting + ", " + name)

greet(______)

Answer: name="Alice", greeting="Hi"

Explanation: Keyword arguments are passed by specifying the parameter name and its value (name="Alice", greeting="Hi").

Question 6: Which of the following defines a function that takes two positional arguments and one keyword argument?

  1. def my_function(a, b=2, c):
  2. def my_function(a, b, c=3):
  3. def my_function(a=1, b, c):
  4. def my_function(a, b, c):

Answer: B) def my_function(a, b, c=3):

Explanation: The function my_function(a, b, c=3) takes two positional arguments (a, b) and one keyword argument (c=3).

Question 7: Which of the following statements about mixed argument passing are true?

  1. Positional arguments must always come before keyword arguments.
  2. Keyword arguments can be passed in any order.
  3. You can mix both positional and keyword arguments in the same function call.
  4. Keyword arguments can come before positional arguments.

Answer: A) Positional arguments must always come before keyword arguments.
B) Keyword arguments can be passed in any order.
C) You can mix both positional and keyword arguments in the same function call.

Explanation: Positional arguments must come before keyword arguments, but keyword arguments can be passed in any order. Mixing positional and keyword arguments is allowed as long as positional arguments are placed first.

Question 8: Arrange the steps to correctly define and call a function using mixed argument passing.

  1. Use keyword arguments for the remaining parameters.
  2. Define the function with both positional and keyword arguments.
  3. Call the function with positional arguments first.
  4. Print the result.

Answer: 2, 3, 1, 4

Explanation: First, define the function with positional and keyword arguments, then call it with positional arguments, followed by keyword arguments, and finally print the result.

Question 9: Complete the code to call the function calculate with positional arguments 5 and keyword argument y=10.

def calculate(x, y=0):
    return x + y

result = calculate(______, ______)
print(result)

Answer: 5, y=10

Explanation: The function calculate(x, y=0) accepts one positional argument and one keyword argument. The call uses 5 for x and y=10.

Question 10: What will be the output of the following code?

def subtract(a, b=5):
    return a - b

result = subtract(10, b=3)
print(result)
  1. 13
  2. 7
  3. 3
  4. Error

Answer: C) 7

Explanation: The function subtract(a, b) is called with a=10 and b=3. Since b is provided as a keyword argument, the result is 10 - 3 = 7.

Question 11: Insert the correct arguments to call the function add with a=3 as a positional argument and b=7 as a keyword argument.

def add(a, b):
    return a + b

result = add(______)
print(result)

Answer: 3, b=7

Explanation: In mixed argument passing, positional arguments come first, followed by keyword arguments (3, b=7).

Question 12: What will the following code output?

def greet(greeting="Hello", name="Bob"):
    print(greeting + ", " + name)

greet(name="Alice")
  1. Hello, Bob
  2. Hello, Alice
  3. Hi, Alice
  4. Error

Answer: B) Hello, Alice

Explanation: The function greet() is called with the keyword argument name="Alice", so the default greeting "Hello" is used, and the name is replaced with "Alice".

Question 13: Which of the following function calls will work for the given function definition? (Choose all that apply)

def display(a, b=2, c=3):
    return a + b + c
  1. display(1)
  2. display(1, 4)
  3. display(1, c=5)
  4. display(a=2, b=4, c=6)

Answer:A) display(1)
B) display(1, 4)
C) display(1, c=5)
D) display(a=2, b=4, c=6)

Explanation: All these calls are valid. Default values are provided for b and c, so they can be overridden or skipped.

Question 14: Arrange the steps to define and call a function using keyword arguments.

  1. Use keyword arguments when calling the function.
  2. Define the function with default values for some parameters.
  3. Print the result of the function call.
  4. Call the function with keyword arguments in any order.

Answer: 2, 1, 4, 3

Explanation: First, define the function with default parameters, then call the function using keyword arguments in any order, and finally print the result.

Question 15: Complete the code to call the function print_info with name="John" as a positional argument and age=30 as a keyword argument.

def print_info(name, age=25):
    print(name + " is " + str(age) + " years old")

print_info(______)

Answer: "John", age=30

Explanation: In mixed argument passing, "John" is a positional argument, and age=30 is a keyword argument.

Question 16: What will be the output of the following code?

def calculate_area(length, width=10):
    return length * width

result = calculate_area(5)
print(result)
  1. 15
  2. 50
  3. 5
  4. Error

Answer: B) 50

Explanation: The function calculate_area(5) is called with length=5 and uses the default value width=10, resulting in an area of 5 * 10 = 50.

17. Insert the correct keyword arguments to call the function power with base=2 and exp=3.

def power(base, exp):
    return base ** exp

result = power(______)
print(result)

Answer: base=2, exp=3

Explanation: In the function call, both arguments are passed as keyword arguments (base=2, exp=3).

Question 18: Which of the following function calls will result in an error?

def multiply(x, y=2):
    return x * y
  1. multiply(5)
  2. multiply(3, y=4)
  3. multiply(x=6)
  4. All the above are false

Answer: D) All the above are false

Explanation: All the function call are valid and no error will occur. So option D will be the result.

Question 19: Which of the following calls use mixed argument passing for the function def add(a, b, c=3):? (Choose all that apply)

  • add(1, b=2)
  • add(1, 2)
  • add(a=4, b=5)
  • add(1, 2, c=4)

Answer: A) add(1, b=2)
D) add(1, 2, c=4)

Explanation: Mixed argument passing involves using both positional and keyword arguments. Options A and D use positional arguments for a and keyword arguments for b or c.

Question 20: Arrange the steps to define and call a function using positional and keyword arguments.

  1. Call the function with positional arguments first.
  2. Define the function with positional and keyword arguments.
  3. Call the function with keyword arguments for other parameters.
  4. Print the result.

Answer: 2, 1, 3, 4,

Explanation: First, define the function with positional and keyword arguments, then call it with positional arguments, followed by keyword arguments, and print the result.

Question 21: Complete the code to call the function calculate with length=8 as a positional argument and width=5 as a keyword argument.

def calculate(length, width=10):
    return length * width

calculate(______)

Answer: 8, width=5

Explanation: In mixed argument passing, the first argument is positional (8), and the second is a keyword argument (width=5).

Question 22: What will be the output of the following code?

def greet(first_name, last_name="Smith"):
    print("Hello, " + first_name + " " + last_name)

greet("John")
  1. Hello, John
  2. Hello, John Smith
  3. Hello,
  4. Error

Answer: B) Hello, John Smith

Explanation: The function greet() is called with one positional argument ("John"), and the default value for last_name is used, resulting in "Hello, John Smith".

Question 23: Insert the correct arguments to call the function display_sum with positional arguments 4, 5 and a keyword argument z=6.

def display_sum(x, y, z=0):
    return x + y + z

result = display_sum(______)
print(result)

Answer: 4, 5, z=6

Explanation: In mixed argument passing, 4 and 5 are positional arguments, and z=6 is a keyword argument.

Question 24: What will be the result of the following code?

def add(x, y=5):
    return x + y

print(add(3))
  1. 5
  2. 3
  3. 8
  4. Error

Answer: C) 8

Explanation: The function add(3) is called with one argument for x, and the default value y=5 is used, resulting in 3 + 5 = 8.

Question 25: Which of the following are true about positional and keyword arguments? (Choose all that apply)

  1. Positional arguments are passed without specifying the parameter name.
  2. Keyword arguments must follow positional arguments.
  3. Keyword arguments must be passed in the order defined in the function.
  4. Positional arguments must be passed before keyword arguments.

Answer: A) Positional arguments are passed without specifying the parameter name.
B) Keyword arguments must follow positional arguments.
D) Positional arguments must be passed before keyword arguments.

Explanation: Positional arguments are passed without specifying the parameter name and must be passed before keyword arguments, which can be passed in any order.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

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-positional-keyword-and-mixed-argument-passing.php