PCEP-30-02 Exam — Questions and Answers
Question 1: What is the output? x = 5 def show(): print(x) show() x = 10 show()
- 5 10 (Correct answer)
- 5 5
- Error
- 10 10
Correct answer: 5 10
Each call to show() reads x from global scope at the time of the call: first 5, then 10.
Question 2: What is the result of `list(range(0, 10, 3))`?
- [3, 6, 9]
- [0, 3, 6, 9, 12]
- [0, 3, 6, 9] (Correct answer)
- [0, 3, 6]
Correct answer: [0, 3, 6, 9]
`range(0, 10, 3)` generates 0, 3, 6, 9 — stopping before 10.
Question 3: What does `'key' in {'key': 1, 'val': 2}` evaluate to?
- False
- Error
- 1
- True (Correct answer)
Correct answer: True
The `in` operator checks for key membership in a dictionary.
Question 4: What is the result of `2 ** -1`?
- 0
- Error
- -2
- 0.5 (Correct answer)
Correct answer: 0.5
Python evaluates `2 ** -1` as `1/2 = 0.5` since `**` accepts negative exponents.
Question 5: Which statement correctly describes the LEGB rule in Python?
- Local, Enclosing, Global, Built-in — the order Python searches for names (Correct answer)
- Local, External, Global, Binary — the order of variable declarations
- Local, Evaluated, Generated, Bound — the order Python assigns values
- Loop, Enclosing, Global, Block — scoping rules for loops
Correct answer: Local, Enclosing, Global, Built-in — the order Python searches for names
LEGB stands for Local, Enclosing, Global, Built-in — Python's name resolution order.
Question 6: What is the result of the following expression? `print(2 + 3 * 4)`
- 20
- 14 (Correct answer)
- A syntax error occurs
- 9
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 7: What does `'Hello World'.count('l')` return?
- 3 (Correct answer)
- 4
- 1
- 2
Correct answer: 3
`count()` returns the number of non-overlapping occurrences; 'l' appears 3 times in 'Hello World'.
Question 8: Which of the following is a valid way to copy a list without aliasing it?
- b = a.copy()
- b = list(a)
- Both b and c (Correct answer)
- b = a
Correct answer: Both b and c
Both `.copy()` and `list()` create a new shallow copy of the list.
Question 9: Which of the following statements about variable shadowing is TRUE?
- A local variable replaces the global variable permanently
- Python raises a NameError when local and global names conflict
- A local variable with the same name as a global variable hides the global inside that function (Correct answer)
- The global variable is deleted when a local variable shadows it
Correct answer: A local variable with the same name as a global variable hides the global inside that function
Shadowing means the local name takes precedence inside the function; the global is unaffected.
Question 10: What is the result of `list({'a': 1, 'b': 2})`?
- ['a', 1, 'b', 2]
- [('a', 1), ('b', 2)]
- ['a', 'b'] (Correct answer)
- [1, 2]
Correct answer: ['a', 'b']
Iterating over a dictionary (or converting it with `list()`) yields its keys.
Question 11: What is the output of `print(2 ** 3)` in Python?
- 5
- 9
- 6
- 8 (Correct answer)
Correct answer: 8
The `**` operator is exponentiation, so `2 ** 3` equals 2 to the power of 3, which is 8.
Question 12: What is the output of the following code? ```python for i in range(3): print(i) else: print('Done!') ```
- Done!
- A syntax error occurs.
- 0 1 2
- 0 1 2 Done! (Correct answer)
Correct answer: 0 1 2 Done!
The `else` block in a `for` loop executes only if the loop completes its entire iteration without being terminated by a `break` statement. In this case, the loop finishes normally, so 'Done!' is printed after the numbers 0, 1, and 2.
Question 13: What is the output of `'abc' * 0`?
- 0
- abc
- Error
- '' (Correct answer)
Correct answer: ''
Multiplying a string by 0 produces an empty string because the string is repeated zero times.
Question 14: Under which circumstance is the `else` block of a loop NOT executed?
- When the loop iterates over an empty sequence.
- When a `continue` statement is executed.
- When the loop condition becomes false.
- When a `break` statement is executed. (Correct answer)
Correct answer: When a `break` statement is executed.
The primary purpose of the `else` block in a loop is to run code when the loop finishes its iterations naturally. If the loop is exited prematurely using a `break` statement, the `else` block is skipped.
Question 15: What is the output of `print('cat', 'dog', sep=' and ')`?
- cat dog
- cat, dog
- cat and dog (Correct answer)
- catdog
Correct answer: cat and dog
The separator ' and ' is placed between the two string arguments, producing 'cat and dog'.
Question 16: What is the result of `1 << 4`?
- 8
- 32
- 16 (Correct answer)
- 4
Correct answer: 16
Left-shifting 1 by 4 positions is equivalent to 2^4 = 16.
Question 17: What does the `global` keyword do inside a function?
- Makes the variable private to the module
- Allows the function to read and modify a global variable (Correct answer)
- Creates a new local variable
- Deletes the global variable
Correct answer: Allows the function to read and modify a global variable
The `global` keyword declares that a name inside a function refers to the module-level variable rather than creating a local one.
Question 18: What is the output of the following code? ```python value = 1 def change_value(): print(value) change_value() ```
- A NameError occurs.
- An UnboundLocalError occurs.
- 1 (Correct answer)
- None
Correct answer: 1
Functions can read or access global variables without needing the `global` keyword. The `global` keyword is only required when you need to modify or assign a new value to the global variable from within the function.
Question 19: What is the output of `print(10 / 2)` in Python 3?
- 5.0 (Correct answer)
- 5.00
- 5
- Error
Correct answer: 5.0
In Python 3, `/` always performs true division and returns a float, so `10 / 2` is `5.0`.
Question 20: Which built-in exception is raised when an operation receives the correct type but an inappropriate value?
- TypeError
- IndexError
- ValueError (Correct answer)
- AttributeError
Correct answer: ValueError
`ValueError` is raised when a function receives an argument of the correct type but an invalid value, such as `int('abc')`.
Question 21: What is the base class for all built-in exceptions in Python?
- RuntimeError
- Exception
- BaseException (Correct answer)
- Error
Correct answer: BaseException
`BaseException` is the base class for all built-in exceptions, while `Exception` is a subclass of `BaseException` used for most non-system-exiting exceptions.
Question 22: What is the output of the following? def greet(name, msg='Hello'): print(msg, name) greet('Bob') greet('Alice', 'Hi')
- Bob Hello\nAlice Hi
- Hi Bob\nHi Alice
- Hello Bob\nHello Alice
- Hello Bob\nHi Alice (Correct answer)
Correct answer: Hello Bob\nHi Alice
First call uses the default msg='Hello' with name='Bob'; second call overrides msg with 'Hi' and name='Alice'.
Question 23: What is the output of `print((1, 2) * 2)`?
- (1, 2, 1, 2) (Correct answer)
- (2, 4)
- Error
- ((1, 2), (1, 2))
Correct answer: (1, 2, 1, 2)
Multiplying a tuple by an integer repeats its contents in a new tuple.
Question 24: Which loop is guaranteed to execute its body at least once?
- while loop with a False condition
- None — Python has no do-while, so neither guarantees one execution (Correct answer)
- A while loop cannot guarantee that
- for loop
Correct answer: None — Python has no do-while, so neither guarantees one execution
Python does not have a do-while loop, so neither `for` nor `while` guarantees at least one execution; the condition is checked before the first iteration.
Question 25: Which method would you use to remove an item from a list by its index in Python?
- delete()
- pop() (Correct answer)
- discard()
- remove()
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 26: What does bool("False") return in Python?
- A ValueError is raised
- False
- None
- 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.
Question 27: 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 4
- 0 1
- 0 1 2 3
- 0 1 2 (Correct answer)
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 28: What is `12 & 10`?
- 14
- 8 (Correct answer)
- 0
- 2
Correct answer: 8
12=1100, 10=1010; AND gives 1000=8.
Question 29: What does float("3.14") return in Python?
- 3.14 (a float) (Correct answer)
- "3.14" (a string)
- A ValueError is raised
- 3 (an integer)
Correct answer: 3.14 (a float)
float() converts a valid string representation of a decimal number into a floating-point value, returning 3.14.
Question 30: Which operation clears (sets to 0) bit 1 (value 2) of variable `x`?
- x = x ^ 2
- x = x & ~2 (Correct answer)
- x = x >> 2
- x = x | 2
Correct answer: x = x & ~2
ANDing with the complement of the mask (~2) forces the target bit to 0.
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