PCEP-30-02 Exam — Questions and Answers
Question 1: What does the following code demonstrate? pi = 3.14 def area(r): return pi * r * r print(area(2))
- Variable shadowing of pi inside area
- An error because pi is not declared global inside area
- A function reading a global variable without the global keyword (read-only access) (Correct answer)
- A function modifying a global variable without permission
Correct answer: A function reading a global variable without the global keyword (read-only access)
Functions can read global variables freely; global is only required when you want to rebind (assign to) the name inside the function.
Question 2: What is the result of the following combination of bitwise operations? `print((7 | 2) & 10)`
- 9
- 2 (Correct answer)
- 10
- 7
Correct answer: 2
Following operator precedence, the expression in the parentheses is evaluated first. `7 | 2` (binary `0111 | 0010`) results in `0111`, which is 7. The expression then becomes `7 & 10`. `7` in binary is `0111` and `10` is `1010`. The result of `0111 & 1010` is `0010`, which is the decimal number 2.
Question 3: What happens when you execute int("hello") in Python?
- Returns None
- Returns 0 as a default
- Returns "hello" unchanged
- Raises a ValueError (Correct answer)
Correct answer: Raises a ValueError
int() raises a ValueError when given a string that cannot be interpreted as an integer.
Question 4: What will this code print? def make_counter(): count = 0 def inc(): nonlocal count count += 1 return count return inc c = make_counter() print(c(), c())
- 1 1
- 1 2 (Correct answer)
- Error
- 0 1
Correct answer: 1 2
nonlocal count lets inc() modify make_counter's count, which persists between calls, yielding 1 then 2.
Question 5: What does the finally clause do in Python exception handling?
- Only runs if an exception occurs
- Executes code regardless of whether an exception occurred (Correct answer)
- Only runs if no exception occurs
- Prevents all exceptions
Correct answer: Executes code regardless of whether an exception occurred
The finally block always executes, making it ideal for cleanup operations.
Question 6: Which of the following is the correct syntax to output "Hello, World!" in Python?
- echo "Hello, World!"
- printf("Hello, World!")
- console.log("Hello, World!")
- print("Hello, World!") (Correct answer)
Correct answer: print("Hello, World!")
In Python, the `print()` function is the standard way to display output to the console. To output a string like "Hello, World!", you pass the string as an argument inside the parentheses of the `print()` function, enclosed in either single or double quotes. This syntax is universally used for producing console output in Python programs.
Question 7: What is the output of the following code? ```python for num in [1, 2, 3, 4]: if num == 3: break print(num) else: print('Finished.') ```
- 1 2 (Correct answer)
- 1 2 3 Finished.
- 1 2 Finished.
- 1 2 3
Correct answer: 1 2
The `break` statement terminates the loop when `num` is 3. Because the loop did not complete its full iteration, the `else` block is skipped entirely. Only the numbers before the `break` are printed.
Question 8: What is the result of `-7 % 3` in Python?
- -2
- -1
- 1
- 2 (Correct answer)
Correct answer: 2
Python's modulo follows floor division; `-7 // 3` is `-3`, and `-7 - (-3 * 3)` = 2.
Question 9: Which of the following data structures is used to store unique elements in Python?
- tuple
- list
- set (Correct answer)
- dict
Correct answer: set
In Python, a `set` is an unordered collection of unique elements. This means that a set automatically prevents duplicate values from being stored, ensuring every element within it is distinct. Sets are highly efficient for operations like membership testing, removing duplicates from a sequence, and performing mathematical set operations.
Question 10: Which of the following is the correct syntax for an "if" statement in Python?
- if condition { statement }
- if condition: statement (Correct answer)
- if condition: { statement }
- if (condition) then statement
Correct answer: if condition: statement
Python's `if` statement uses a specific and clear syntax: the `if` keyword, followed by a condition, and then a colon `:`. The code block to be executed if the condition is true must be indented below the `if` line. This structure clearly defines the conditional logic, making Python code readable and consistent.
Question 11: What is the output? x = 'global' def test(): global x x = 'local' test() print(x)
- None
- local (Correct answer)
- Error
- global
Correct answer: local
global x inside test() makes the assignment modify the module-level x, so it becomes 'local'.
Question 12: How do you import a specific function from a module in Python?
- from module import function (Correct answer)
- import function from module
- get function from module
- module.import(function)
Correct answer: from module import function
The from...import syntax allows importing specific names from a module.
Question 13: What is the output of `type(3.0)`?
- <class 'double'>
- <class 'int'>
- <class 'float'> (Correct answer)
- <class 'num'>
Correct answer: <class 'float'>
`3.0` is a floating-point literal, so `type()` returns `<class 'float'>`.
Question 14: What will be printed to the console? ```python for char in 'PCEP': print(char) break else: print('End of string') ```
- End of string
- P End of string
- P C E P End of string
- P (Correct answer)
Correct answer: P
The loop starts with the first character 'P' and prints it. Immediately after, the `break` statement is encountered, which terminates the loop. Since the loop was terminated by `break`, the `else` block is not executed.
Question 15: Which string method returns `True` if all characters in the string are alphabetic?
- isalpha() (Correct answer)
- isdigit()
- isspace()
- isalnum()
Correct answer: isalpha()
`isalpha()` returns `True` only when every character in the string is a letter.
Question 16: What is the output of this `while` loop? ```python i = 5 while i > 0: i -= 1 if i == 2: continue print(i) else: print('Loop over.') ```
- 4 3 2 1 0 Loop over.
- 4 3 1 0
- 4 3 1
- 4 3 1 0 Loop over. (Correct answer)
Correct answer: 4 3 1 0 Loop over.
The `continue` statement skips the current iteration but does not terminate the loop. The loop continues until `i` becomes 0, at which point the condition `i > 0` is false. Since the loop completed normally, the `else` block is executed.
Question 17: What is the output of this code snippet? ```python n = 3 while n > 0: n -= 1 print(n) else: if n == 0: print('Loop ended.') ```
- 3 2 1 Loop ended.
- 2 1 0 Loop ended. (Correct answer)
- 2 1 0
- 3 2 1
Correct answer: 2 1 0 Loop ended.
The `while` loop runs until `n` becomes 0. The loop prints 2, 1, and 0. When `n` is 0, the condition `n > 0` becomes false, and the loop terminates. Since it terminated normally, the `else` block is executed. Inside the `else` block, the condition `n == 0` is true, so 'Loop ended.' is printed.
Question 18: What does `'apple,banana,cherry'.split(',')` return?
- apple banana cherry
- ['apple', 'banana', 'cherry'] (Correct answer)
- {'apple', 'banana', 'cherry'}
- ('apple', 'banana', 'cherry')
Correct answer: ['apple', 'banana', 'cherry']
`split(',')` breaks the string at each comma and returns a list of the resulting substrings.
Question 19: What does `'Python'.find('th')` return?
- 3
- 1
- 2 (Correct answer)
- -1
Correct answer: 2
`find()` returns the index of the first occurrence of the substring; 'th' starts at index 2.
Question 20: What does `'hello' in 'hello world'` evaluate to?
- Error
- None
- True (Correct answer)
- False
Correct answer: True
The `in` operator returns `True` if the left string is a substring of the right string.
Question 21: What is the difference between a list and a tuple in Python?
- Tuples are immutable, while lists are mutable. (Correct answer)
- Lists and tuples are both mutable.
- Lists and tuples are both immutable.
- Lists are immutable, while tuples are mutable.
Correct answer: Tuples are immutable, while lists are mutable.
The fundamental difference between lists and tuples in Python lies in their mutability. Tuples are immutable, meaning their elements cannot be changed, added, or removed after creation, making them suitable for fixed collections. Lists, conversely, are mutable, allowing their elements to be modified, added, or removed, providing flexibility for dynamic data.
Question 22: What happens when you use the global keyword on a name that does not yet exist at module level?
- Python raises a SyntaxError at compile time
- Python creates the variable at module level when it is assigned inside the function (Correct answer)
- Python raises a NameError immediately
- The variable is created locally instead
Correct answer: Python creates the variable at module level when it is assigned inside the function
global just declares intent; the global variable is created upon the first assignment inside the function.
Question 23: What is the result of the following expression? `print(2 + 3 * 4)`
- A syntax error occurs
- 20
- 9
- 14 (Correct answer)
Correct answer: 14
In Python, the multiplication operator (`*`) has higher precedence than the addition operator (`+`). Therefore, `3 * 4` is evaluated first, resulting in 12. Then, `2 + 12` is evaluated, giving the final result of 14.
Question 24: What is the output of the following nested loop? ```python for i in range(2): for j in range(2): if i == j: break print((i, j), end=' ') ```
- No output
- (1, 0) (Correct answer)
- (0, 1)
- (0, 1) (1, 0)
Correct answer: (1, 0)
The `break` statement only affects the innermost loop it is in. When `i` is 0, the inner loop runs for `j=0`. The condition `i == j` is true, so `break` is executed, and nothing is printed. When `i` is 1, the inner loop runs for `j=0`. The condition is false, so `(1, 0)` is printed. Then `j` becomes 1, the condition `i == j` is true, and the inner loop breaks.
Question 25: Consider the following code. What is the output? ```python items = [] for item in items: print(item) else: print('No items found.') ```
- No items found. (Correct answer)
- The program produces no output.
- An error occurs.
- []
Correct answer: No items found.
When a `for` loop is given an empty sequence (like an empty list), it does not execute its body at all. It is considered to have completed 'normally' without any iterations. Therefore, the `else` block is executed.
Question 26: What is the output of the following code? ```python for i in range(3): print(i, end='') ```
- 012 (Correct answer)
- 0 1 2
- 123
- 0 1 2
Correct answer: 012
The loop iterates for `i` values 0, 1, and 2. In each iteration, the `print` function is called with `end=''` which means no character (not even a space or newline) is printed at the end. This causes all the numbers to be printed consecutively on the same line.
Question 27: Which method replaces all occurrences of a substring in a string?
- replace() (Correct answer)
- change()
- sub()
- swap()
Correct answer: replace()
`replace(old, new)` returns a new string with all occurrences of `old` replaced by `new`.
Question 28: What will this code output? total = 0 def add(n): total = total + n return total print(add(5))
- None
- UnboundLocalError (Correct answer)
- 5
- 0
Correct answer: UnboundLocalError
Python sees total = ... and marks it local, so reading total before assignment raises UnboundLocalError.
Question 29: In Python, what does the "else" block of a "for" loop execute for?
- When the loop encounters a break statement
- When the loop has no more items to iterate over (Correct answer)
- When the loop encounters an exception
- When the loop is explicitly terminated by the user
Correct answer: When the loop has no more items to iterate over
In Python, a `for` loop can optionally have an `else` block, which executes only if the loop completes all its iterations without encountering a `break` statement. If the loop is terminated by a `break`, the `else` block is skipped entirely. This provides a way to execute specific code only when a loop finishes naturally.
Question 30: What is the result of `' hello '.strip()`?
- hello (Correct answer)
- hello
- hello
- ' hello '
Correct answer: hello
`strip()` removes leading and trailing whitespace from a string.
PCEP-30-02 Exam
The PCEP – Certified Entry-Level Python Programmer certification validates foundational knowledge of Python programming, including data types, control flow, data collections, functions, and exceptions.
Exam Rules
- You can skip questions and return to them later
- Flag questions for review before submitting
- No feedback shown until you submit the entire exam
- Unanswered questions count as wrong — answer everything
- 10 pretest questions are mixed in and don't affect your score
- Timer auto-submits when time runs out
- Your progress is auto-saved every 30 seconds
These practice questions were written with AI tools. They are not real exam questions. Exam facts are checked against the official sources listed below. Not reviewed by a subject-matter expert. Editorial policy
Sources & references
Exam facts on this page (fees, format, dates, passing scores) are checked against these official sources; the date is our last check. Editorial policy
- pythoninstitute.org — Fees, exam format, passing score, retake fee, score release, time limit, score validity, test delivery, eligibility, retake policy · verified Oct 2026