PCEP Certification Practice Test: User-Defined Functions and Generators
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 "defining and invoking user-defined functions and generators." 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 correct syntax for defining a user-defined function in Python?
- def my_function:
- def my_function():
- def my_function:()
- my_function()
Answer: B) def my_function():
Explanation: A function is defined in Python using the def keyword followed by the function name and parentheses ().
Question 2: Which of the following are true about Python functions? (Choose all that apply)
- Functions can return a value using the return statement.
- Functions can take arguments as input.
- Functions must always return a value.
- Functions can be invoked without any arguments.
Answer: A) Functions can return a value using the return statement.
B) Functions can take arguments as input.
D) Functions can be invoked without any arguments.
Explanation: Functions can take arguments, return values, or return nothing (if the return statement is omitted). Functions can also be called without arguments if defined that way.
Question 3: Complete the code to define a function greet that prints "Hello, World!".
def greet(): ______ "Hello, World!"▼
Answer: print
Explanation: The print() function outputs "Hello, World!" when the greet() function is called.
Question 4: What will be the output of the following code?
def add(a, b): return a + b result = add(3, 4) print(result)
- 7
- 34
- Error
- None
Answer: A) 7
Explanation: The add() function takes two arguments, a and b, and returns their sum. The output of add(3, 4) is 7.
Question 5: Insert the correct code to call the function greet and display "Hello".
def greet(): print("Hello") ______▼
Answer: greet()
Explanation: To invoke the function greet(), you simply call it by its name followed by parentheses.
Question 6: What will the following code return?
def square(x): return x ** 2 result = square(5) print(result)
- 10
- 25
- 5
- None
Answer: B) 25
Explanation: The square() function returns the square of the argument x. For x = 5, the function returns 5 ** 2 = 25.
Question 7: Which of the following are valid ways to define a function in Python? (Choose all that apply)
- def my_function(): pass
- def my_function():
- def my_function(a, b): return a + b
- def my_function(): print("Hello")
Answer: A) def my_function(): pass
C) def my_function(a, b): return a + b
D) def my_function(): print("Hello")
Explanation:A function can be defined using the def keyword. The body of the function can either contain statements (like print("Hello")) or the pass keyword as a placeholder for future code.
Question 8: Arrange the steps to correctly define and call a function that multiplies two numbers.
- Print the result.
- Define the function multiply().
- Return the product of two numbers.
- Call the multiply() function with two arguments.
Answer: 2, 3, 4, 1,
Explanation: First, define the function, then return the product, call the function with arguments, and finally print the result.
Question 9: Complete the code to define a function divide that returns the division of two numbers a and b.
def divide(a, b): ______ a / b▼
Answer: return
Explanation: The return statement is used to return the result of a / b from the divide() function.
Question 10: What will be the output of the following code?
def say_hello(name): return f"Hello, {name}!" message = say_hello("Alice") print(message)
- Hello, name!
- Hello, Alice!
- name
- Alice
Answer: B) Hello, Alice!
Explanation: The function say_hello() takes an argument name and returns the string "Hello, Alice!" when passed the argument "Alice".
Question 11: Insert the correct code to define and call a generator function count_up_to that yields numbers from 1 to n.
def count_up_to(n): for i in range(1, n+1): ______ i gen = count_up_to(5) print(list(gen))▼
Answer: yield
Explanation: The yield keyword is used to create a generator function, which yields values one at a time. The output will be [1, 2, 3, 4, 5].
Question 12: Which of the following correctly defines a generator function in Python?
- def gen(): return 1
- def gen(): yield 1
- def gen(): return yield 1
- def gen(): return
Answer: B) def gen(): yield 1
Explanation: The yield statement is used to create a generator function, which returns a generator object that produces values one at a time.
Question 13: Which of the following are characteristics of a generator function? (Choose all that apply)
- Uses the yield keyword.
- Returns a generator object.
- Can only return one value.
- Produces values one at a time
Answer:A) Uses the yield keyword.
B) Returns a generator object.
D) Produces values one at a time.
Explanation: Generator functions use yield to return a sequence of values one at a time, and they return a generator object. They do not return a single value like regular functions.
Question 14: Arrange the steps to correctly define and call a generator function that yields even numbers up to n.
- Yield each even number.
- Define the generator function even_numbers().
- Use a loop to generate even numbers.
- Call the generator function and print the results.
Answer: 2, 3, 1, 4
Explanation: First, define the generator function, then use a loop to generate even numbers, yield each number, and finally call the generator and print the results.
Question 15: Complete the code to define a function sum_numbers that returns the sum of two numbers x and y.
def sum_numbers(x, y): ______ x + y▼
Answer: return
Explanation: The return statement is used to return the result of x + y from the sum_numbers() function.
Question 16: What will be the output of the following code?
def generator_example(): yield "First" yield "Second" yield "Third" gen = generator_example() print(next(gen)) print(next(gen))
- First Second
- First Third
- Second Third
- Error
Answer: A) First Second
Explanation: The next() function retrieves the next value from the generator. The first call returns "First", and the second call returns "Second".
17. Insert the correct code to define a function multiply that multiplies two numbers and returns the result.
def multiply(x, y): ______ x * y result = multiply(3, 4) print(result)▼
Answer: return
Explanation: The return statement returns the product of x and y.
Question 18: Which of the following statements correctly describes a generator?
- A generator produces all values at once.
- A generator produces values one at a time.
- A generator must return a list.
- A generator is called like a regular function.
Answer: B) A generator produces values one at a time.
Explanation: Generators produce values one at a time using the yield keyword.
Question 19: Which of the following are valid ways to invoke a user-defined function? (Choose all that apply)
- my_function()
- my_function(1, 2)
- my_function(a=10, b=20)
- def my_function()
Answer: A) my_function()
B) my_function(1, 2)
C) my_function(a=10, b=20)
Explanation: Functions are invoked by calling them with parentheses (). The function can take positional arguments, keyword arguments, or no arguments.
Question 20: Arrange the steps to correctly define and invoke a function that subtracts two numbers.
- Print the result.
- Define the function subtract().
- Use the return statement to return the result of a - b.
- Call the function with arguments.
Answer: 2, 3, 4, 1,
Explanation: First, define the function, use the return statement, call the function with arguments, and finally print the result.
Question 21: Complete the code to define and invoke a function greet_user that takes a name as an argument and prints a greeting.
def greet_user(name): print("Hello, " + name) greet_user(______)▼
Answer: "John"
Explanation: The greet_user() function takes an argument name and prints a greeting. The argument "John" is passed when calling the function.
Question 22: What will be the output of the following code?
def print_numbers(n): for i in range(1, n+1): print(i) print_numbers(3)
- 1 2 3
- 1 2
- 1 2 3 4
- None
Answer: A) 1 2 3
Explanation: The print_numbers() function prints numbers from 1 to n. When called with n = 3, it prints 1 2 3.
Question 23: Insert the correct code to define a generator count_down that counts down from n to 1.
def count_down(n): while n > 0: ______ n n -= 1 gen = count_down(3) print(list(gen))▼
Answer: yield
Explanation: The yield keyword is used to produce values from the generator one at a time. The output will be [3, 2, 1].
Question 24: What will be the result of the following code?
def add(a, b): return a + b print(add(2, 3))
- 2
- 3
- 5
- Error
Answer: C) 5
Explanation: The add() function returns the sum of 2 and 3, which is 5.
Question 25: Which of the following statements about generators are true? (Choose all that apply)
- Generators use the yield keyword.
- Generators return all values at once.
- Generators return values one at a time.
- Generators can be iterated using next().
Answer: A) Generators use the yield keyword.
C) Generators return values one at a time.
D) Generators can be iterated using next().
Explanation: Generators use the yield keyword to return values one at a time and can be iterated using the next() function.
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics