PCEP-30-02 Exam — Questions and Answers
Question 1: Which of the following is the correct syntax for an "if" statement in Python?
- if condition: statement (Correct answer)
- if condition { statement }
- if (condition) then statement
- 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.
Question 2: What is the result of `8 / 2 * 2`?
- 2.0
- 1.0
- 16.0
- 8.0 (Correct answer)
Correct answer: 8.0
`/` and `*` have the same precedence and are left-associative: `(8/2)*2 = 4.0*2 = 8.0`.
Question 3: What does float(False) return in Python?
- False (boolean)
- 0.0 (float) (Correct answer)
- 0 (integer)
- "False" (string)
Correct answer: 0.0 (float)
False has an integer value of 0, and float() converts that to 0.0.
Question 4: What does the following code print? ```python i = 0 while i < 5: print(i, end=' ') i += 1 if i == 3: break ```
- 0 1 2 3
- 0 1
- 0 1 2 (Correct answer)
- 0 1 2 3 4
Correct answer: 0 1 2
The `while` loop starts with `i = 0`. It prints 0, then increments `i` to 1. It prints 1, increments `i` to 2. It prints 2, increments `i` to 3. Now the condition `i == 3` is true, and the `break` statement terminates the loop.
Question 5: What is the output of: ```python for i in range(5): if i == 3: break else: print('no break') print('after') ```
- after (Correct answer)
- Nothing is printed
- no break after
- no break
Correct answer: after
The `break` at i==3 prevents the `else` from running, but `print('after')` outside the loop executes.
Question 6: What type does Python assign to the literal `0b1010`?
- int (Correct answer)
- float
- str
- bin
Correct answer: int
Binary literals prefixed with `0b` are stored as `int` in Python; their type is `int`.
Question 7: The `else` block of a loop is executed when:
- The loop variable equals None at the end
- No break statement terminated the loop (Correct answer)
- The loop body raised a StopIteration
- Any exception is caught inside the loop
Correct answer: No break statement terminated the loop
The else block runs whenever the loop exits without a break, regardless of whether the body ran zero or many times.
Question 8: Which of the following is TRUE about the loop `else` clause?
- It runs after every complete loop, whether the iterable was empty or not (Correct answer)
- It only runs when the loop body executed at least once
- It is only valid for while loops, not for loops
- It runs after break exits the loop
Correct answer: It runs after every complete loop, whether the iterable was empty or not
The else clause runs after any complete loop exit (including an empty iterable) as long as break was not used.
Question 9: Which of the following correctly describes positional arguments?
- Arguments prefixed with `*`
- Arguments with default values
- Arguments matched by parameter name
- Arguments matched by their position in the function call (Correct answer)
Correct answer: Arguments matched by their position in the function call
Positional arguments are matched to parameters based on the order they appear in the function call.
Question 10: Which data structure in Python is mutable and ordered?
- set
- tuple
- dict
- 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 11: What is the result of: ``` print('A', end='B') print('C') ```
- AB\nC
- ABC (Correct answer)
- A\nBC
- A BC
Correct answer: ABC
The first print ends with 'B' instead of '\n', so 'C' from the second print immediately follows, producing 'ABC' on one line.
Question 12: Which method replaces all occurrences of a substring in a string?
- change()
- replace() (Correct answer)
- swap()
- sub()
Correct answer: replace()
`replace(old, new)` returns a new string with all occurrences of `old` replaced by `new`.
Question 13: What does min([10, 3, 7, 1, 5]) return?
- 1 (Correct answer)
- 5
- 10
- 3
Correct answer: 1
min() returns the smallest element in the iterable, which is 1 in this list.
Question 14: Which of the following will raise an `IndexError`?
- my_str = 'hi'; my_str.upper()
- my_dict = {}; my_dict['key']
- my_list = [1, 2]; my_list[5] (Correct answer)
- my_list = []; my_list.append(1)
Correct answer: my_list = [1, 2]; my_list[5]
Accessing `my_list[5]` on a list with only 2 elements raises `IndexError` because the index is out of the valid range.
Question 15: What is the output of `[0] * 4`?
- [0, 1, 2, 3]
- [0, 0, 0, 0] (Correct answer)
- 0
- [4]
Correct answer: [0, 0, 0, 0]
Multiplying a list by an integer repeats its elements that many times.
Question 16: 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
- Error
- 1 2 (Correct answer)
- 0 1
Correct answer: 1 2
nonlocal count lets inc() modify make_counter's count, which persists between calls, yielding 1 then 2.
Question 17: What is the output of this code? for i in range(5): if i == 2: continue if i == 4: break else: print('no break') print('done')
- no break done
- no break
- done (Correct answer)
- Nothing
Correct answer: done
When i==4, break exits the loop, so the else block is skipped and only 'done' is printed.
Question 18: What does `sorted({3, 1, 2})` return?
- (1, 2, 3)
- {1, 2, 3}
- Error
- [1, 2, 3] (Correct answer)
Correct answer: [1, 2, 3]
`sorted()` always returns a new list, regardless of the input type.
Question 19: What does `'Hello World'.count('l')` return?
- 3 (Correct answer)
- 1
- 2
- 4
Correct answer: 3
`count()` returns the number of non-overlapping occurrences; 'l' appears 3 times in 'Hello World'.
Question 20: What does `tuple([1, 2, 3])` return?
- [1, 2, 3]
- Error
- {1, 2, 3}
- (1, 2, 3) (Correct answer)
Correct answer: (1, 2, 3)
`tuple()` converts any iterable into a tuple.
Question 21: What is the output of `'Python'[-1]`?
- P
- o
- y
- n (Correct answer)
Correct answer: n
Negative indexing starts from the end; `-1` refers to the last character, which is 'n'.
Question 22: What is the result of `list(range(0, 10, 3))`?
- [3, 6, 9]
- [0, 3, 6, 9, 12]
- [0, 3, 6]
- [0, 3, 6, 9] (Correct answer)
Correct answer: [0, 3, 6, 9]
`range(0, 10, 3)` generates 0, 3, 6, 9 — stopping before 10.
Question 23: What does the "try...except" block in Python do?
- Executes code only if no errors occur
- Catches and handles exceptions or errors that occur during execution (Correct answer)
- Terminates the program if an error occurs
- Ignores all errors during execution
Correct answer: Catches and handles exceptions or errors that occur during execution
The `try...except` block in Python is a mechanism for handling runtime errors, known as exceptions, gracefully. Code within the `try` block is executed, and if an exception occurs, execution immediately jumps to the `except` block. This allows the program to handle the error without crashing, ensuring more robust and stable execution.
Question 24: What is the output? x = 5 def show(): print(x) show() x = 10 show()
- 10 10
- 5 10 (Correct answer)
- Error
- 5 5
Correct answer: 5 10
Each call to show() reads x from global scope at the time of the call: first 5, then 10.
Question 25: Which of the following is used to start a comment in Python?
- <!--
- /*
- //
- # (Correct answer)
Correct answer: #
In Python, the hash symbol `#` is used to denote a single-line comment. Any text following `#` on that line is ignored by the Python interpreter and serves as documentation for human readers. This allows programmers to add explanatory notes, clarify complex logic, or temporarily disable code sections without affecting program execution.
Question 26: What is the output of the following code? for i in range(3): if i == 1: continue print(i)
- 0 1
- 0 2 (Correct answer)
- 1 2
- 0 1 2
Correct answer: 0 2
The `continue` statement skips the rest of the loop body when `i == 1`, so 1 is never printed.
Question 27: If a name is not found in local, enclosing, or global scopes, where does Python look next?
- External modules
- sys.path
- The heap
- Built-in scope (Correct answer)
Correct answer: Built-in scope
The final step in LEGB is the built-in scope, which contains names like print, len, and range.
Question 28: What is `~(-1)` in Python?
- 1
- -2
- -1
- 0 (Correct answer)
Correct answer: 0
~n = -(n+1), so ~(-1) = -(-1+1) = 0.
Question 29: Which of the following correctly uses the `print()` function to display text without a newline at the end?
- print('text', end='') (Correct answer)
- print('text'\n)
- print('text', newline=False)
- print('text', nl=False)
Correct answer: print('text', end='')
The `end` parameter of `print()` controls what character is printed at the end; `end=''` suppresses the newline.
Question 30: What does bool("False") return in Python?
- A ValueError is raised
- None
- False
- True (Correct answer)
Correct answer: True
Any non-empty string is truthy in Python, so bool("False") returns True regardless of the string's content.
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