w3resource

PCEP Certification Practice Test: Parameters vs. Arguments

PCEP Certification Practice Test - Questions, Answers and Explanations

Below is a set of questions for the Certified Entry-Level Python Programmer (PCEP) examination focusing on the subtopic "parameters vs. arguments." 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 a parameter in Python?

  1. A value passed to a function when it is called.
  2. A variable defined in the function definition.
  3. A return value from a function.
  4. A built-in Python function.

Answer: B) A variable defined in the function definition.

Explanation: A parameter is a variable specified in the function definition that accepts the value passed when the function is called.

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

  • Arguments are the values passed to a function when it is called.
  • Arguments are the variables defined in the function definition.
  • A function can be called with positional arguments.
  • A function can be called with keyword arguments.

Answer: A) Arguments are the values passed to a function when it is called.
C) A function can be called with positional arguments.
D) A function can be called with keyword arguments.

Explanation: Arguments are the actual values passed to the function when it is called, and they can be passed as positional or keyword arguments.

Question 3: Complete the code to define a function greet that takes a single parameter name.

def greet(______):
    print("Hello, " + name)

Answer: name

Explanation: The parameter name is specified in the function definition, and it is used within the function to display the greeting

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

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

result = add(5, 3)
print(result)
  1. 53
  2. 8
  3. None
  4. Error

Answer: B) 8

Explanation: The function add(a, b) is defined with two parameters a and b. The arguments 5 and 3 are passed, and the result of their sum is 8.

Question 5: Insert the correct arguments to call the function multiply with the values 6 and 7.

def multiply(x, y):
    return x * y

result = multiply(______)
print(result)

Answer: 6, 7

Explanation: The function multiply(x, y) requires two arguments. Passing 6 and 7 as arguments will result in their product.

Question 6: Which of the following correctly defines a function that takes two parameters and returns their sum?

  1. def add(): return a + b
  2. def add(a, b): return a + b
  3. def add(a): return a + b
  4. def add(a, b): return

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

Explanation: The function is correctly defined with two parameters a and b, and it returns their sum.

Question 7: Which of the following are valid ways to pass arguments to a function in Python? (Choose all that apply)

  1. By position.
  2. By keyword.
  3. By reference.
  4. By value.

Answer: A) By position.
B) By keyword.

Explanation:In Python, arguments can be passed by position (positional arguments) or by specifying the parameter name (keyword arguments). Python does not distinguish between passing by value or by reference.

Question 8: Arrange the steps to define a function that takes two parameters and returns their product.

  1. Define the function using def.
  2. Use the return statement to return the product of the parameters.
  3. Specify the parameters in the parentheses.
  4. Call the function with arguments.

Answer: 1, 3, 2, 4

Explanation: First, define the function, then specify the parameters, use the return statement to return the product, and finally call the function with arguments.

Question 9: Complete the code to call the function greet with the argument "Alice".

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

greet(______)

Answer: "Alice"

Explanation: The argument "Alice" is passed to the greet() function, which uses it in the print statement.

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

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

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

Answer: A) 7

Explanation: The function subtract(a, b) subtracts b from a. When called with arguments 10 and 3, the result is 7.

Question 11: Insert the correct keyword arguments to call the function divide with x = 8 and y = 2.

def divide(x, y):
    return x / y

result = divide(______)
print(result)

Answer: x=8, y=2

Explanation: Keyword arguments are passed by specifying the parameter name (x=8 and y=2).

Question 12: What will the following code output?

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

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

Answer: A) Hi, Alice

Explanation: The function greet() is called with two arguments: name="Alice" and greeting="Hi". The default value of greeting is overridden by "Hi".

Question 13: Which of the following statements about function parameters are correct? (Choose all that apply)

  1. Parameters are defined in the function signature.
  2. Parameters are assigned values when the function is called.
  3. Parameters are passed by value in Python.
  4. Parameters are always optional.

Answer:A) Parameters are defined in the function signature.
B) Parameters are assigned values when the function is called.

Explanation: Parameters are defined in the function signature and are assigned values when the function is called. Python passes arguments by object reference, not strictly by value or reference.

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

  1. Use the parameters inside the function.
  2. Define the function with parameters.
  3. Call the function with keyword arguments.
  4. Print the result.

Answer: 2, 1, 4, 3

Explanation: First, define the function with parameters, use the parameters, call the function using keyword arguments, and print the result.

Question 15: Complete the code to define a function divide with two parameters a and b, and return their division.

def divide(a, b):
    ______ a / b

Answer: return

Explanation: The return statement is used to return the result of dividing a by b.

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

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

greet()
  1. Hello, Stranger
  2. Hello,
  3. Hello, None
  4. Error

Answer: A) Hello, Stranger

Explanation: The function greet() is called without any arguments, so the default value "Stranger" is used for the parameter name.

17. Insert the correct code to call the function multiply with positional arguments 4 and 5.

def multiply(a, b):
    return a * b

result = ______
print(result)

Answer: multiply(4, 5)

Explanation: The function multiply(4, 5) is called with positional arguments 4 and 5, returning their product.

Question 18: Which of the following defines a function that takes a default parameter value?

  1. def greet(name, greeting="Hello"):
  2. def greet(name="Hello", greeting):
  3. def greet(name):
  4. def greet(greeting):

Answer: A) def greet(name, greeting="Hello"):

Explanation: The function greet() is defined with a default parameter greeting set to "Hello", which can be overridden when calling the function.

Question 19: Which of the following are valid function calls for the following function? (Choose all that apply)

def add(x, y=5):
    return x + y
  • add(10)
  • add(3, 7)
  • add(x=1)
  • add(4, y=6)

Answer: A) add(10)
B) add(3, 7)
C) add(x=1)
D) add(4, y=6)

Explanation: The function add(x, y=5) can be called with one or two arguments, either using positional or keyword arguments.

Question 20: Arrange the steps to correctly define and call a function with a default parameter.

  1. Use the default parameter inside the function.
  2. Define the function with a parameter and a default value.
  3. Call the function without passing the default parameter.
  4. Call the function with an argument to override the default value.

Answer: 2, 3, 1, 4,

Explanation: First, define the function with a default parameter, use the parameter, call the function without passing the parameter, and finally call the function with an argument to override the default value.

Question 21: Complete the code to define a function greet with a default parameter greeting="Hi".

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

Answer: greating

Explanation: The parameter greeting is assigned the default value "Hi", which can be overridden when the function is called.

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

def display(a, b=2):
    return a * b

result = display(3)
print(result)
  1. 6
  2. 8
  3. 12
  4. Error

Answer: A) 6

Explanation: The function display(3) is called with one argument, and the default value b=2 is used. The result is 3 * 2 = 6.

Question 23: Insert the correct positional arguments to call the function add with the values 5 and 8.

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

result = add(______)
print(result)

Answer: 5, 8

Explanation: The function add(x, y) takes two arguments, and calling it with 5 and 8 returns their sum.

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

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

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

Answer: A) Hello, Bob

Explanation: The function greet("Bob") is called with the default parameter greeting="Hello", resulting in the output "Hello, Bob".

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

  1. Keyword arguments allow you to pass arguments by specifying the parameter name.
  2. Keyword arguments must always be provided in the same order as the parameters.
  3. Keyword arguments can override default values.
  4. Keyword arguments can be mixed with positional arguments.

Answer: A) Keyword arguments allow you to pass arguments by specifying the parameter name.
C) Keyword arguments can override default values.
D) Keyword arguments can be mixed with positional arguments.

Explanation: Keyword arguments allow you to pass values by specifying parameter names and can be used with positional arguments. They also allow you to override default parameter values.



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-parameters-vs-arguments.php