PCEP-30-02 Exam — Questions and Answers
Question 1: What does this code print? primes = [2, 3, 5] for p in primes: if p > 4: break else: print('all small') print('checked')
- all small
- Error
- all small checked
- checked (Correct answer)
Correct answer: checked
p==5 satisfies p>4, triggering break, so the else is skipped and only 'checked' is printed.
Question 2: What is the output of this code? try: raise TypeError('bad type') except ValueError: print('V') except TypeError: print('T') except Exception: print('E')
- T E
- V
- T (Correct answer)
- E
Correct answer: T
Python matches exceptions to the first matching `except` clause; `TypeError` matches the second handler, so 'T' is printed.
Question 3: What does len([1, [2, 3], 4]) return in Python?
- 4
- 3 (Correct answer)
- 5
- 2
Correct answer: 3
len() counts only the top-level elements; the list has 3 items: 1, [2, 3], and 4.
Question 4: What is the output of the following code? for i in range(3): if i == 1: continue print(i)
- 1 2
- 0 2 (Correct answer)
- 0 1 2
- 0 1
Correct answer: 0 2
The `continue` statement skips the rest of the loop body when `i == 1`, so 1 is never printed.
Question 5: What is the output? def f(): global z z = 99 f() print(z)
- Error
- 99 (Correct answer)
- None
- 0
Correct answer: 99
f() creates global z = 99, so it is accessible at module level after the call.
Question 6: What is the result of running this code? ```python a = 10 def func(a): a = 5 return a print(func(a)) print(a) ```
- 5 5
- 10 10
- 10 5
- 5 10 (Correct answer)
Correct answer: 5 10
The `a` in `func(a)` is a function parameter, which is a local variable. When `func(a)` is called, the value of the global `a` (10) is passed to the local `a`. Inside the function, this local `a` is changed to 5 and returned. The global `a` is never modified.
Question 7: In Python, what does the expression `x & (x - 1)` do when x is a power of 2?
- Returns x
- Returns x+1
- Returns x-1
- Returns 0 (Correct answer)
Correct answer: Returns 0
For a power of 2, subtracting 1 flips the trailing bit; ANDing then clears the only set bit, yielding 0.
Question 8: What is the output of the following? def f(x, y=2): return x * y print(f(3))
- Error
- 2
- 3
- 6 (Correct answer)
Correct answer: 6
With only one argument provided, `y` defaults to 2, so `f(3)` computes `3 * 2 = 6`.
Question 9: What is the output of `my_list = [1,2,3]; my_list.extend([4,5]); print(my_list)`?
- [[1, 2, 3], [4, 5]]
- [1, 2, 3, [4, 5]]
- [1, 2, 3, 4, 5] (Correct answer)
- Error
Correct answer: [1, 2, 3, 4, 5]
`extend()` adds each element of the iterable individually to the list.
Question 10: Which string method returns `True` if all characters in the string are alphabetic?
- isdigit()
- isspace()
- isalnum()
- isalpha() (Correct answer)
Correct answer: isalpha()
`isalpha()` returns `True` only when every character in the string is a letter.
Question 11: How many times does the following loop execute? i = 0 while i < 5: i += 2
- 2
- 3 (Correct answer)
- 4
- 5
Correct answer: 3
i takes values 0, 2, 4 before i becomes 6 which fails the condition, so the loop runs 3 times.
Question 12: What is the result of `{1, 2, 3} & {2, 3, 4}`?
- {2, 3} (Correct answer)
- {1, 2, 3, 4}
- Error
- {1, 4}
Correct answer: {2, 3}
The `&` operator returns the intersection — elements common to both sets.
Question 13: What is the decimal value of `0b0101 | 0b1010`?
- 10
- 0
- 5
- 15 (Correct answer)
Correct answer: 15
0101 | 1010 = 1111 in binary, which equals 15 in decimal.
Question 14: Which call produces the output `Hello World` (no newline at the end)?
- print('Hello World', end='')
- print('Hello', 'World', end='')
- Both A and C (Correct answer)
- print('Hello World', sep='')
Correct answer: Both A and C
Both `print('Hello World', end='')` and `print('Hello', 'World', end='')` suppress the newline (the latter also uses default `sep=' '`).
Question 15: Which method replaces all occurrences of a substring in a string?
- replace() (Correct answer)
- swap()
- change()
- sub()
Correct answer: replace()
`replace(old, new)` returns a new string with all occurrences of `old` replaced by `new`.
Question 16: What happens when you try to change a value in a tuple?
- The tuple is converted to a list
- A TypeError is raised (Correct answer)
- The value is updated
- A ValueError is raised
Correct answer: A TypeError is raised
Tuples are immutable; attempting to assign to an index raises a TypeError.
Question 17: What does `True or False and False` evaluate to?
- None
- False
- Error
- True (Correct answer)
Correct answer: True
`and` has higher precedence than `or`, so `False and False` is evaluated first (False), then `True or False` equals True.
Question 18: What is the output of `print(bin(10 >> 1))`?
- 0b100
- 0b1010
- 0b101 (Correct answer)
- 0b10100
Correct answer: 0b101
10 in binary is 1010; shifting right by 1 gives 0101, which is 5, printed as 0b101.
Question 19: What is the result of the bitwise XOR operation `13 ^ 7`?
- 10 (Correct answer)
- 8
- 6
- 15
Correct answer: 10
The binary representation of 13 is `1101` and for 7 is `0111`. The bitwise XOR (`^`) operation compares each bit and returns 1 only if the bits are different. Comparing `1101` and `0111` results in `1010`, which is the decimal number 10.
Question 20: Which of the following creates an empty set?
- ()
- {}
- []
- set() (Correct answer)
Correct answer: set()
`{}` creates an empty dict, so `set()` is the only way to create an empty set.
Question 21: Which operator has the highest precedence in Python?
- ** (Correct answer)
- *
- +
- not
Correct answer: **
The exponentiation operator `**` has the highest precedence among arithmetic operators in Python.
Question 22: What is the output of the following code? ```python value = 1 def change_value(): print(value) change_value() ```
- An UnboundLocalError occurs.
- 1 (Correct answer)
- None
- A NameError occurs.
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 23: What is the output of `a = [1, 2]; b = a; b.append(3); print(a)`?
- [1, 2, 3] (Correct answer)
- Error
- [3]
- [1, 2]
Correct answer: [1, 2, 3]
Both `a` and `b` reference the same list object, so mutating `b` also changes `a`.
Question 24: What is the output of the following expression? `print(10 // 3 % 2)`
- 1.5
- 3
- 0
- 1 (Correct answer)
Correct answer: 1
The floor division (`//`) and modulo (`%`) operators have the same precedence and are evaluated from left to right. First, `10 // 3` is calculated, which results in 3. Then, `3 % 2` is calculated, which results in 1.
Question 25: What is the return value of len("Python")?
- 8
- 7
- 5
- 6 (Correct answer)
Correct answer: 6
len() counts characters in the string; "Python" has exactly 6 characters: P, y, t, h, o, n.
Question 26: What kind of error will this code produce? ```python count = 0 def increment(): count = count + 1 increment() ```
- NameError
- UnboundLocalError (Correct answer)
- TypeError
- No error, the code runs fine.
Correct answer: UnboundLocalError
This code causes an `UnboundLocalError`. When you assign to a variable in a scope (e.g., `count = ...`), Python treats it as a local variable for the entire scope. Therefore, when it tries to read `count` on the right side of the expression, it sees a local variable that hasn't been assigned a value yet.
Question 27: What is the result of `str(42)`?
- 42
- Error
- 42.0
- '42' (Correct answer)
Correct answer: '42'
`str()` converts an integer to its string representation, resulting in the string '42'.
Question 28: Which of the following has the LOWEST precedence in Python?
- and
- ==
- or (Correct answer)
- not
Correct answer: or
Among logical operators and comparisons, `or` has the lowest precedence, evaluated last.
Question 29: What will the following code print? x = 10 def foo(): x = 20 foo() print(x)
- None
- 20
- 10 (Correct answer)
- Error
Correct answer: 10
The assignment inside foo() creates a local variable; the global x remains 10.
Question 30: Is it valid Python syntax to have an `else` block on both an inner and outer `for` loop?
- No, only one else per function
- Yes, but only the outer else can contain break
- Yes, each loop can have its own else block (Correct answer)
- No, nested loops cannot have else
Correct answer: Yes, each loop can have its own else block
Each for or while loop can independently have its own else block; nesting does not restrict this.
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