PCEP-30-02 Exam — Questions and Answers
Question 1: What is the result of `list({'a': 1, 'b': 2})`?
- ['a', 'b'] (Correct answer)
- [('a', 1), ('b', 2)]
- [1, 2]
- ['a', 1, 'b', 2]
Correct answer: ['a', 'b']
Iterating over a dictionary (or converting it with `list()`) yields its keys.
Question 2: Which of the following is the correct output of type(3.14) in Python?
- (float)
- "float"
- float()
- <class 'float'> (Correct answer)
Correct answer: <class 'float'>
type() returns the class object itself, and its repr is <class 'float'> for floating-point values.
Question 3: What is the result of `10 % 3` in Python?
- 3
- 0
- 3.33
- 1 (Correct answer)
Correct answer: 1
The modulo operator `%` returns the remainder of 10 divided by 3, which is 1.
Question 4: Which of the following correctly uses the `print()` function to display text without a newline at the end?
- print('text'\n)
- print('text', nl=False)
- print('text', newline=False)
- print('text', end='') (Correct answer)
Correct answer: print('text', end='')
The `end` parameter of `print()` controls what character is printed at the end; `end=''` suppresses the newline.
Question 5: What is the output of: ```python for i in range(3): for j in range(3): if i == j: continue print(i, j) ```
- Prints all 9 pairs
- Prints nothing
- Prints all pairs where i != j (Correct answer)
- Prints all pairs where i == j
Correct answer: Prints all pairs where i != j
`continue` skips printing when i equals j, so only pairs where i != j are printed.
Question 6: What is the result of `[x**2 for x in range(4)]`?
- [1, 4, 9, 16]
- [0, 1, 2, 3]
- [0, 2, 4, 6]
- [0, 1, 4, 9] (Correct answer)
Correct answer: [0, 1, 4, 9]
The list comprehension squares each value from 0 to 3.
Question 7: What is the result of `0 | 255`?
- 255 (Correct answer)
- 0
- -1
- 1
Correct answer: 255
OR with 0 leaves every bit of the second operand unchanged, so the result is 255.
Question 8: What is printed by this code? x = 10 while x > 0: x -= 3 if x == 1: break else: print('finished')
- Nothing (Correct answer)
- finished
- Error
- 1
Correct answer: Nothing
x goes 10→7→4→1, triggering break at x==1, so the else block is skipped.
Question 9: What is the output of: ```python for i in range(3): pass print(i) ```
- NameError
- 0
- 3
- 2 (Correct answer)
Correct answer: 2
The loop variable `i` retains its last value after the loop ends, which is 2 for `range(3)`.
Question 10: In Python, what is the associativity of the assignment operator `=`?
- Right (Correct answer)
- Left
- None (non-associative)
- Both
Correct answer: Right
The assignment operator is right-associative: `a = b = 5` is evaluated as `a = (b = 5)`.
Question 11: What built-in function can be used to find the maximum value among several numbers?
- max() (Correct answer)
- high()
- maximum()
- largest()
Correct answer: max()
The `max()` built-in function returns the largest value among its arguments or from an iterable.
Question 12: Which of the following is TRUE about the loop `else` clause?
- It is only valid for while loops, not for loops
- It runs after every complete loop, whether the iterable was empty or not (Correct answer)
- It runs after break exits the loop
- It only runs when the loop body executed at least once
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 13: What is printed by this code? for a in range(2): for b in range(2): if a == b: break else: print(b)
- 0
- Nothing (Correct answer)
- 0 1
- 1
Correct answer: Nothing
For a=0: b=0, 0==0 triggers break so inner else is skipped. For a=1: b=0 (0!=1), b=1 (1==1) triggers break so inner else is skipped. Nothing is printed.
Question 14: What does the `traceback` module help with in Python?
- Preventing exceptions from occurring
- Formatting and printing stack trace information (Correct answer)
- Converting exceptions to warnings
- Automatically fixing runtime errors
Correct answer: Formatting and printing stack trace information
The `traceback` module provides utilities for extracting, formatting, and printing Python stack traces for debugging purposes.
Question 15: What will this code print? try: raise ValueError('oops') finally: print('done')
- oops
- done, then the exception propagates (Correct answer)
- Nothing, the exception suppresses output
- done oops
Correct answer: done, then the exception propagates
The `finally` block runs and prints 'done' before the unhandled `ValueError` propagates up and terminates the program.
Question 16: Which operator is used to repeat a string in Python?
- %
- **
- +
- * (Correct answer)
Correct answer: *
The `*` operator repeats a string a given number of times, e.g., `'ab' * 3` gives `'ababab'`.
Question 17: Which of the following statements correctly initializes a dictionary in Python?
- dict = ( 'name': 'Alice', 'age': 25 )
- dict = { 'name': 'Alice', 'age': 25 } (Correct answer)
- dict = [ 'name': 'Alice', 'age': 25 ]
- dict = 'name': 'Alice', 'age': 25
Correct answer: dict = { 'name': 'Alice', 'age': 25 }
In Python, dictionaries are created using curly braces `{}` and store data as key-value pairs. Each key is separated from its value by a colon `:`, and pairs are separated by commas. The syntax `dict = { 'name': 'Alice', 'age': 25 }` correctly initializes a dictionary named `dict` with two key-value entries, making it ready for use.
Question 18: Which method would you use to remove an item from a list by its index in Python?
- pop() (Correct answer)
- discard()
- remove()
- delete()
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 19: Which exception type would be raised by `int(None)`?
- ValueError
- AttributeError
- NameError
- TypeError (Correct answer)
Correct answer: TypeError
`int(None)` raises `TypeError` because `None` is not a valid type to convert to an integer.
Question 20: What does `tuple([1, 2, 3])` return?
- [1, 2, 3]
- Error
- (1, 2, 3) (Correct answer)
- {1, 2, 3}
Correct answer: (1, 2, 3)
`tuple()` converts any iterable into a tuple.
Question 21: What is the result of `{1, 2, 3} & {2, 3, 4}`?
- {1, 4}
- {2, 3} (Correct answer)
- Error
- {1, 2, 3, 4}
Correct answer: {2, 3}
The `&` operator returns the intersection — elements common to both sets.
Question 22: How do you define a function in Python?
- def myFunction(): (Correct answer)
- func myFunction():
- define myFunction():
- function myFunction():
Correct answer: def myFunction():
In Python, functions are defined using the `def` keyword, followed by the function name, a pair of parentheses `()`, and a colon `:`. The code block that constitutes the function's body must then be indented below this definition line. This syntax establishes a new callable function, encapsulating a block of reusable code.
Question 23: Which string method returns `True` if all characters in the string are alphabetic?
- isdigit()
- isspace()
- isalpha() (Correct answer)
- isalnum()
Correct answer: isalpha()
`isalpha()` returns `True` only when every character in the string is a letter.
Question 24: What does round(3.567, 1) return in Python?
- 3.5
- 4.0
- 3.57
- 3.6 (Correct answer)
Correct answer: 3.6
round(3.567, 1) rounds to 1 decimal place; since the second decimal is 6 (≥5), it rounds up to 3.6.
Question 25: What is the scope of a variable defined inside an if block at module level?
- Enclosing scope — visible to nested functions only
- Block scope — only visible inside the if block
- Module (global) scope — if blocks don't create a new scope (Correct answer)
- Local scope — treated like a function variable
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 26: What is the result of `list(range(0, 10, 3))`?
- [0, 3, 6, 9] (Correct answer)
- [3, 6, 9]
- [0, 3, 6, 9, 12]
- [0, 3, 6]
Correct answer: [0, 3, 6, 9]
`range(0, 10, 3)` generates 0, 3, 6, 9 — stopping before 10.
Question 27: What is the output of the following code? def outer(): x = 1 def inner(): x = 2 inner() print(x) outer()
- Error
- None
- 1 (Correct answer)
- 2
Correct answer: 1
inner() creates its own local x = 2 without nonlocal, so outer()'s x stays 1.
Question 28: 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 (Correct answer)
- P C E 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 29: Which exception is raised when a `list.index()` call cannot find the specified value?
- IndexError
- LookupError
- ValueError (Correct answer)
- KeyError
Correct answer: ValueError
`list.index()` raises `ValueError` when the specified value is not present in the list.
Question 30: In Python, can `break` be used outside of a loop?
- Yes, it exits the current function
- No, it raises a RuntimeError
- Yes, it stops the script
- No, it causes a SyntaxError (Correct answer)
Correct answer: No, it causes a SyntaxError
Using `break` outside a loop results in a SyntaxError because it has no loop to break out of.
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