PCEP-30-02 Exam — Questions and Answers
Question 1: What is the scope of a variable defined inside an if block at module level?
- Block scope — only visible inside the if block
- Local scope — treated like a function variable
- Module (global) scope — if blocks don't create a new scope (Correct answer)
- Enclosing scope — visible to nested functions only
Correct answer: Module (global) scope — if blocks don't create a new scope
Python has no block scope; variables created inside if, for, or with blocks belong to the enclosing function or module scope.
Question 2: What is the output of this code? ```python for i in range(5): if i == 3: continue print(i, end=' ') ```
- 0 1 2 4 (Correct answer)
- 4
- 0 1 2
- 0 1 2 3 4
Correct answer: 0 1 2 4
The loop iterates from 0 to 4. When `i` is 3, the `if` condition is true, and the `continue` statement is executed. This statement skips the rest of the current iteration (the `print` statement) and proceeds to the next iteration (when `i` is 4).
Question 3: Which of the following data structures is used to store unique elements in Python?
- set (Correct answer)
- dict
- list
- tuple
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 4: In Python, what does the "else" block of a "for" loop execute for?
- When the loop is explicitly terminated by the user
- When the loop encounters an exception
- When the loop has no more items to iterate over (Correct answer)
- When the loop encounters a break statement
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 5: What is the output of `len('Python 3')`?
- 8 (Correct answer)
- 6
- 7
- 9
Correct answer: 8
`len()` counts all characters including spaces, so 'Python 3' has 8 characters.
Question 6: What will the following code print? x = 10 def foo(): x = 20 foo() print(x)
- 10 (Correct answer)
- None
- Error
- 20
Correct answer: 10
The assignment inside foo() creates a local variable; the global x remains 10.
Question 7: What does `int(3.9)` return?
- 4
- 3 (Correct answer)
- 3.9
- Error
Correct answer: 3
`int()` truncates toward zero, discarding the fractional part, so `int(3.9)` gives 3.
Question 8: Which data type is used to store textual data in Python?
- int
- float
- bool
- str (Correct answer)
Correct answer: str
In Python, the `str` data type (short for string) is specifically designed to store textual data, which consists of sequences of characters. Strings are immutable, meaning their content cannot be changed after creation. This data type is fundamental for handling any text-based information, such as names, messages, or file paths, within Python programs.
Question 9: 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.') ```
- 2 1 0 Loop ended. (Correct answer)
- 2 1 0
- 3 2 1
- 3 2 1 Loop ended.
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 10: What is the output of `'-'.join(['a', 'b', 'c'])`?
- -a-b-c-
- a-b-c (Correct answer)
- a b c
- abc
Correct answer: a-b-c
`join()` concatenates list elements with the calling string as a separator between each item.
Question 11: Which method would you use to remove an item from a list by its index in Python?
- discard()
- delete()
- remove()
- pop() (Correct answer)
Correct answer: pop()
The `pop()` method in Python lists is specifically designed to remove an item at a specified index. If no index is provided, `pop()` removes and returns the last item in the list. This method is particularly useful when you need to remove an element by its position and potentially use the removed value in subsequent operations.
Question 12: Which data structure in Python is mutable and ordered?
- dict
- set
- tuple
- list (Correct answer)
Correct answer: list
A Python list is an ordered collection of items, meaning elements maintain their insertion order and can be accessed by index. Crucially, lists are mutable, which means you can add, remove, or change elements after the list has been created. This makes them highly flexible for dynamic data storage and manipulation.
Question 13: What will be printed to the console? ```python for char in 'PCEP': print(char) break else: print('End of string') ```
- P C E P End of string
- End of string
- P (Correct answer)
- P End of string
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 14: What will this code output? total = 0 def add(n): total = total + n return total print(add(5))
- 0
- 5
- UnboundLocalError (Correct answer)
- None
Correct answer: UnboundLocalError
Python sees total = ... and marks it local, so reading total before assignment raises UnboundLocalError.
Question 15: How do you import a specific function from a module in Python?
- from module import function (Correct answer)
- module.import(function)
- get function from module
- import function from module
Correct answer: from module import function
The from...import syntax allows importing specific names from a module.
Question 16: What happens when you use the global keyword on a name that does not yet exist at module level?
- The variable is created locally instead
- Python raises a NameError immediately
- Python raises a SyntaxError at compile time
- Python creates the variable at module level when it is assigned inside the function (Correct answer)
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 17: What is the output of the following code? def outer(): x = 1 def inner(): x = 2 inner() print(x) outer()
- None
- 2
- 1 (Correct answer)
- Error
Correct answer: 1
inner() creates its own local x = 2 without nonlocal, so outer()'s x stays 1.
Question 18: What is the difference between a list and a tuple in Python?
- Lists are immutable, while tuples are mutable.
- Lists and tuples are both immutable.
- Lists and tuples are both mutable.
- Tuples are immutable, while lists are mutable. (Correct answer)
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 19: What does bool(0) return in Python?
- True
- False (Correct answer)
- 0
- None
Correct answer: False
In Python, 0 is falsy, so bool(0) returns False.
Question 20: What exception is raised when dividing by zero in Python?
- ArithmeticError
- TypeError
- ZeroDivisionError (Correct answer)
- ValueError
Correct answer: ZeroDivisionError
Python raises ZeroDivisionError specifically for division by zero operations.
Question 21: What is the output of `'abc' * 0`?
- '' (Correct answer)
- 0
- Error
- abc
Correct answer: ''
Multiplying a string by 0 produces an empty string because the string is repeated zero times.
Question 22: What does `'hello'.upper()` return?
- hELLO
- hello
- Hello
- HELLO (Correct answer)
Correct answer: HELLO
The `upper()` method converts all characters in the string to uppercase.
Question 23: 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=' ') ```
- (0, 1)
- No output
- (0, 1) (1, 0)
- (1, 0) (Correct answer)
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 24: What will be printed by this code? ```python for num in range(4): if num == 2: break else: print("Done") print(num) ```
- Done 2
- 3
- Done 3
- 2 (Correct answer)
Correct answer: 2
The `for` loop's `else` block is executed only if the loop completes its iterations without being terminated by a `break` statement. In this case, the loop breaks when `num` is 2. Therefore, the `else` block is skipped. The final `print(num)` statement prints the last value of `num` before the loop was exited, which is 2.
Question 25: What is the output of this code snippet? ```python print('Hello', end=' ') print('World!') ```
- Hello World!
- HelloWorld!
- A syntax error occurs.
- Hello World! (Correct answer)
Correct answer: Hello World!
The `end` keyword argument specifies what to print at the end of the line. By default, it's a newline character (`\n`). Here, it's set to a space for the first `print` call, so the second `print` call will start on the same line, immediately after the space.
Question 26: What is the output of this code? `print(not True or False)`
- False (Correct answer)
- True
- None
- SyntaxError
Correct answer: False
The `not` operator has higher precedence than the `or` operator. First, `not True` is evaluated to `False`. The expression then becomes `False or False`, which evaluates to `False`.
Question 27: What is the output of this `while` loop? ```python i = 0 while i < 5: i += 1 if i == 3: continue print(i, end=' ') ```
- 0 1 2 4
- 1 2 3 4 5
- 1 2 4 5 (Correct answer)
- 0 1 2 3 4
Correct answer: 1 2 4 5
Inside the loop, `i` is incremented first. When `i` becomes 3, the `continue` statement is executed, skipping the `print(i)` for that iteration. The loop continues, printing the values 1, 2, 4, and 5.
Question 28: How would you print the words 'one', 'two', and 'three' each on a new line using a single `print()` call?
- print('one') print('two') print('three')
- print('one', 'two', 'three', sep='\n') (Correct answer)
- print('one', 'two', 'three', end='\n')
- print('one\ntwo\nthree')
Correct answer: print('one', 'two', 'three', sep='\n')
To print each argument on a new line, you need to change the separator between them from the default space to a newline character (`'\n'`). Using `sep='\n'` achieves this result with a single function call.
Question 29: What is the output of the bitwise NOT operation `~5`?
- -6 (Correct answer)
- 6
- -5
- 5
Correct answer: -6
The bitwise NOT (`~`) operator inverts all the bits of its operand. In Python, for an integer `x`, `~x` is equivalent to `-(x + 1)`. Therefore, `~5` is `-(5 + 1)`, which equals -6.
Question 30: Which of the following is the correct syntax for an "if" statement in Python?
- if condition { statement }
- if (condition) then statement
- if condition: statement (Correct answer)
- if condition: { 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.
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