Earning a Certified Entry-Level Python Programmer (PCEP) credential is a great way to validate your foundational knowledge in Python programming and stand out in the competitive tech industry. To help you get ready for the PCEP exam, we have prepared a set of 25 practice multiple-choice questions along with answers and explanations.
Before diving into the practice test below, you can also check out 50+ Python MCQ for Beginners.
1. What is the output of the following Python code?
print(5 // 2)
Answer:
Explanation:
The // operator performs floor division, returning the largest possible integer.
2. Which of the following data types is immutable in Python?
Answer:
Explanation:
Tuples in Python are immutable, meaning their elements cannot be changed after creation.
3. What is the purpose of the pass statement in Python?
Answer:
Explanation:
The pass statement is a null operation; nothing happens when it executes. It is typically used as a placeholder.
4. How do you create a lambda function in Python?
Answer:
Explanation:
Lambda functions in Python are defined using the lambda keyword, followed by the parameters, a colon, and the expression.
5. Which of the following methods will remove the last item from a Python list and return it?
Answer:
Explanation:
The pop() method removes the last item from the list and returns it, whereas remove() deletes the first occurrence of a specified value.
6. Which of the following operators is used for exponentiation in Python?
Answer:
Explanation:
The ** operator in Python is used for exponentiation.
7. What is the output of the following Python code?
my_str = "Hello, World!"
print(my_str[-1])
Answer:
Explanation:
Negative indexing starts from the end of the string. my_str[-1] will output the last character !.
8. How can you get the length of a list my_list in Python?
Answer:
Explanation:
The len() function is used to get the length of a list in Python.
9. Which of the following statements is True about Python sets?
Answer:
Explanation:
Sets in Python are mutable, meaning you can change their content, but they are unordered, so they do not record element position.
10. Which Python library is specifically designed for data manipulation and analysis?
Answer:
Explanation:
Pandas is a fast, powerful, and flexible open-source data analysis and manipulation tool built on top of the Python programming language.
11. What will the following code snippet output in Python?
for i in range(1, 5, 2):
print(i, end=" ")
Answer:
Explanation:
The range(1, 5, 2) generates numbers from 1 to 4 with a step of 2. Hence the output will be 1 and 3.
12. How can you handle exceptions in Python?
Answer:
Explanation:
Python uses try and except blocks to handle exceptions. Code that might cause an exception is placed in the try block, and the handling of the exception is implemented in the except block.
13. Which of the following is the correct way to read a file myfile.txt in Python?
Answer:
Explanation:
The open() function is used to open a file in Python. The first argument is the filename, and the second argument is the mode, with 'r' representing reading mode.
14. What is the purpose of the __init__ method in Python classes?
Answer:
Explanation:
The __init__ method is called when an object is instantiated and is used to initialize the attributes of the object.
15. How do you define a function my_function in Python?
Answer:
Explanation:
Functions in Python are defined using the def keyword followed by the function name and parentheses.
16. Which of the following will correctly create a dictionary in Python?
Answer:
Explanation:
Dictionaries in Python are created by placing elements inside curly braces{}`, separated by commas.
17. What is the default value of the step parameter in Python's range() function?
Answer:
Explanation:
The range() function in Python has a default step value of 1.
18. How can you concatenate two lists list1 and list2 in Python?
Answer:
Explanation:
The + operator can be used to concatenate two lists in Python.
19. What will the enumerate() function return for a given list ['apple', 'banana', 'cherry']?
Answer:
Explanation:
The enumerate() function returns a sequence of tuples representing index and value pairs.
20. Which of the following methods can be used to add an element to a set in Python?
Answer:
Explanation:
The add() method is used to add a single element to a set in Python.
21. Which Python library provides functions to work with dates and times?
Answer:
Explanation:
The datetime module supplies classes for working with dates and times, such as date arithmetic.
22. Which of the following Python code will correctly match an email address pattern using regular expressions?
import re
pattern = r'\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b'
result = re.match(pattern, 'test@example.com')
Answer:
Explanation:
re.match() checks for a match only at the beginning of the string. re.search() or re.fullmatch() should be used to search throughout the string.
23. How can you convert a bytes object b'Python' to a str object in Python?
Answer:
Explanation:
To convert a bytes object to a str object, you should use the decode() method.
24. Which of the following functions will return the ASCII value of a character?
Answer:
Explanation:
The ord() function returns an integer representing the Unicode character, which is the ASCII value for ASCII characters.
25. What will be the output of the following code snippet?
print(isinstance(1, object))
Answer:
Explanation:
In Python, everything is an object, so isinstance(1, object) will return True as 1 is an instance of object.