w3resource

PCEP Certification Practice Test: Understanding the None Keyword in Python

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 "the None keyword." 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 does the None keyword represent in Python?

  1. It represents a value of zero.
  2. It represents a string "None".
  3. It represents the absence of a value.
  4. It represents a boolean True.

Answer: C) It represents the absence of a value.

Explanation: In Python, None represents the absence of a value or a null value, similar to null in other programming languages.

Question 2: Which of the following are true about the None keyword? (Choose all that apply)

  • It is used to represent the absence of a value.
  • It is a built-in constant in Python.
  • Functions that do not return anything implicitly return None.
  • None can be used as a placeholder value in variables or functions.

Answer:

  • It is a built-in constant in Python.
  • It is used to represent the absence of a value.
  • Functions that do not return anything implicitly return None.
  • None can be used as a placeholder value in variables or functions.

Explanation: None is a built-in constant used to indicate the absence of a value and is commonly returned by functions that do not have a return statement.

Question 3: Complete the code to check if the variable result is equal to None.

result = None
if result ______ None:
    print("Result is None")

Answer: is

Explanation: In Python, is is used to compare an object to None because None is a singleton, meaning there is only one instance of None in memory.

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

def do_nothing():
    pass

result = do_nothing()
print(result)
  1. None
  2. do_nothing
  3. 0
  4. Error

Answer: A) None

Explanation: Since the do_nothing() function does not return anything, it implicitly returns None, which is then printed.

Question 5: Insert the correct code to check if the function check_value returns None.

def check_value():
    pass

if ______:
    print("Function returned None")

Answer: check_value() is None

Explanation: You can check if the return value of check_value() is None using check_value() is None.

Question 6: What will the following code output?

value = None
print(type(value))
  1. <class 'NoneType'>
  2. None
  3. <class 'None'>
  4. Error

Answer: A) <class 'NoneType'>

Explanation: The type of None is NoneType, which is the data type representing the absence of a value in Python.

Question 7: Which of the following situations would result in a function returning None? (Choose all that apply)

  1. A function that has no return statement.
  2. A function with an empty return statement.
  3. A function that returns the value None.
  4. A function that raises an exception.

Answer: A) A function that has no return statement.
B) A function with an empty return statement.
C) A function that returns the value None.

Explanation:Functions with no return statement or an empty return return None implicitly. A function can also explicitly return None.

Question 8: Arrange the steps to correctly define a function that returns None when called.

  1. Use the return keyword without any value.
  2. Define the function.
  3. Call the function.
  4. Check if the return value is None.

Answer: 2, 1, 3, 4

Explanation: First, define the function, then use the return keyword without a value to return None. After calling the function, check if the return value is None.

Question 9: Complete the code to explicitly return None from the function no_result.

def no_result():
    ______ None

Answer: return

Explanation: The return statement can be used to explicitly return None from a function.

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

def check():
    return None

result = check()
print(result is None)
  1. True
  2. False
  3. None
  4. Error

Answer: A) True

Explanation: The function check() returns None, and the expression result is None evaluates to True.

Question 11: Insert the correct code to return None when the condition is false.

def check_condition(condition):
    if not condition:
        ______
    return "Condition is True"

Answer: return None

Explanation: You can explicitly return None when the condition is False by using the return None statement.

Question 12: What is the return type of the following function?

def example():
    pass
  1. None
  2. <class 'NoneType'>
  3. int
  4. str

Answer: B) <class 'NoneType'>

Explanation: The example() function has no return statement, so it returns None. The type of None is NoneType.

Question 13: Which of the following code snippets will explicitly return None? (Choose all that apply)

  1. def f(): return None
  2. def f(): pass
  3. def f(): return
  4. def f(): return 0

Answer:A) def f(): return None
B) def f(): pass
C) def f(): return

Explanation: return None, pass, and an empty return statement all cause the function to return None. Only Option D explicitly returns 0.

Question 14: Arrange the steps to create a function that returns a result if the condition is true, and None if the condition is false.

  1. Check the condition.
  2. Define the function.
  3. Return None if the condition is false.
  4. Return the result if the condition is true.

Answer: 2, 1, 3, 4

Explanation: First, define the function, check the condition, return None if the condition is false, and return the result if the condition is true.

Question 15: Complete the code to return None when the condition is not met.

def evaluate(condition):
    if not condition:
        return ______
    return "Valid condition"

Answer: None

Explanation: The function should return None when the condition is not met, indicating the absence of a value.

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

def return_none():
    return None

print(type(return_none()))
  1. <class 'NoneType'>
  2. None
  3. <class 'int'>
  4. Error

Answer: A) <class 'NoneType'>

Explanation: The return_none() function explicitly returns None, and the type of None is NoneType.

17. Insert the correct code to check if a variable x is None.

x = None
if x ______:
    print("x is None")

Answer: is None

Explanation: Use is None to check if a variable is None, as None is a singleton in Python.

Question 18: What is the value of x after the following code is executed?

x = None
if x is None:
    x = 10
  1. None
  2. 10
  3. Error
  4. 0

Answer: B) 10

Explanation: Since x is initially None, the condition x is None evaluates to True, and x is assigned the value 10.

Question 19: Which of the following are correct ways to check if a variable is None? (Choose all that apply)

  • if x == None:
  • if x is None:
  • if x != None:
  • if x is not None:

Answer: B) if x is None:
D) if x is not None:

Explanation: The correct and recommended way to check if a variable is None is by using is None or is not None. Using == or != is not considered best practice.

Question 20: Arrange the steps to define a function that returns None and verify its type.

  1. Use the return keyword with None.
  2. Define the function.
  3. Call the function.
  4. Check the type of the return value.

Answer: 2, 1, 3, 4,

Explanation: First, define the function, use the return None statement, call the function, and finally check the type of the returned value.

Question 21: Complete the code to assign None to the variable value.

value = ______

Answer: None

Explanation: The keyword None is assigned to value, indicating the absence of a value.

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

def func():
    return

result = func()
print(result is None)
  1. True
  2. False
  3. None
  4. Error

Answer: A) True

Explanation: The function func() has an empty return statement, which implicitly returns None. Therefore, result is None evaluates to True.

Question 23: Insert the correct code to check if the result of the function func is not None.

def func():
    return None

if func() ______:
    print("Function did not return None")

Answer: is not None

Explanation: To check if the function result is not None, use is not None.

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

def do_something():
    pass

result = do_something()
print(result)
  1. None
  2. pass
  3. Error
  4. 0

Answer: A) None

Explanation: The do_something() function contains the pass statement, meaning it does nothing and implicitly returns None.

Question 25: Which of the following statements about None are correct? (Choose all that apply)

  • A function with no return statement returns None.
  • None represents the absence of a value.
  • The type of None is NoneType.
  • You can compare None with is None.

Answer:

  • None represents the absence of a value.
  • The type of None is NoneType.
  • A function with no return statement returns None.
  • You can compare None with is None.

Explanation: All the provided statements about None are correct.



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-the-none-keyword.php