w3resource

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?

  1. def my_function:
  2. def my_function():
  3. def my_function:()
  4. 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)

  1. Functions can return a value using the return statement.
  2. Functions can take arguments as input.
  3. Functions must always return a value.
  4. 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)
  1. 7
  2. 34
  3. Error
  4. 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)
  1. 10
  2. 25
  3. 5
  4. 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)

  1. def my_function(): pass
  2. def my_function():
  3. def my_function(a, b): return a + b
  4. 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.

  1. Print the result.
  2. Define the function multiply().
  3. Return the product of two numbers.
  4. 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)
  1. Hello, name!
  2. Hello, Alice!
  3. name
  4. 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?

  1. def gen(): return 1
  2. def gen(): yield 1
  3. def gen(): return yield 1
  4. 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)

  1. Uses the yield keyword.
  2. Returns a generator object.
  3. Can only return one value.
  4. 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.

  1. Yield each even number.
  2. Define the generator function even_numbers().
  3. Use a loop to generate even numbers.
  4. 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))
  1. First Second
  2. First Third
  3. Second Third
  4. 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?

  1. A generator produces all values at once.
  2. A generator produces values one at a time.
  3. A generator must return a list.
  4. 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.

  1. Print the result.
  2. Define the function subtract().
  3. Use the return statement to return the result of a - b.
  4. 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. 1 2 3
  2. 1 2
  3. 1 2 3 4
  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))
  1. 2
  2. 3
  3. 5
  4. 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)

  1. Generators use the yield keyword.
  2. Generators return all values at once.
  3. Generators return values one at a time.
  4. 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.



Become a Patron!

Follow us on Facebook and Twitter for latest update.