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?
- It represents a value of zero.
- It represents a string "None".
- It represents the absence of a value.
- 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)
- None
- do_nothing
- 0
- 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))
- <class 'NoneType'>
- None
- <class 'None'>
- 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)
- A function that has no return statement.
- A function with an empty return statement.
- A function that returns the value None.
- 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.
- Use the return keyword without any value.
- Define the function.
- Call the function.
- 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)
- True
- False
- None
- 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
- None
- <class 'NoneType'>
- int
- 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)
- def f(): return None
- def f(): pass
- def f(): return
- 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.
- Check the condition.
- Define the function.
- Return None if the condition is false.
- 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()))
- <class 'NoneType'>
- None
- <class 'int'>
- 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
- None
- 10
- Error
- 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.
- Use the return keyword with None.
- Define the function.
- Call the function.
- 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)
- True
- False
- None
- 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)
- None
- pass
- Error
- 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.
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
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics