PCEP practice test
Practise for the PCEP exam with fresh questions in its style, timed, then review every answer.
30 questions · 40 minutes · 70% to pass. Scored in your browser, then every answer is explained.
What it covers
- Fundamentals
- Control flow
- Collections
- Functions and exceptions
Every question with its answer
Question 1 · Fundamentals
What is the output of this code?
print(0b101 + 0o10 + 0x1F)
- A46
- B44
- C28
- D140
Run this code in the editor0b101is binary 5,0o10is octal 8 and0x1Fis hexadecimal 31, so the sum is 44. Reading0o10as decimal ten gives the tempting 46.Question 2 · Fundamentals
What is the output of this code?
print(-7 // 2, -7 % 2, 7 // -2.0)
- A-3 -1 -3.5
- B-4 1 -4
- C-4 1 -4.0
- D-3 1 -3.0
Run this code in the editor//rounds down toward minus infinity, so-7 // 2is -4, and the result of%takes the sign of the divisor, giving 1. When one operand is a float,//returns a float, so the last value is -4.0.Question 3 · Fundamentals
What is the output of this code?
base = 2 print(base ** 3 ** 2 // 100, -base ** 2)
- A0 -4
- B5 4
- C0 4
- D5 -4
Run this code in the editor**groups from right to left, sobase ** 3 ** 2is2 ** 9, which is 512, and512 // 100is 5.**also binds tighter than the unary minus, so-base ** 2is-(2 ** 2), which is -4.Question 4 · Fundamentals
The variable
answerholds"7", the valueinput()returns when a user types 7. What does this code print?answer = "7" print(answer * 2, int(answer) * 2, sep=" / ", end=".\n")
- A77 / 14.
- B14 / 14.
- C77 14.
- D7 7 / 14.
Run this code in the editorinput()always returns a string, soanswer * 2repeats the text to give77, whileint(answer) * 2is the number 14.sepgoes between the printed values andendreplaces the usual newline.Question 5 · Fundamentals
Which two of these are valid variable names in Python? Select two.
- A2nd_place
- B_total
- Cclass
- DPrice2
- Emy-score
A name starts with a letter or underscore and continues with letters, digits and underscores, so
_totalandPrice2are valid.2nd_placestarts with a digit,my-scorecontains a minus sign, andclassis a reserved keyword.Question 6 · Fundamentals
What is the output of this code?
flags = 0b1100 mask = 0b1010 print(flags & mask, flags | mask, flags ^ mask, flags >> 2)
- A14 8 6 3
- B8 14 6 48
- C8 14 6 3
- D8 22 6 3
Run this code in the editor&keeps the bits set in both (1000, 8),|keeps bits set in either (1110, 14) and^keeps bits that differ (0110, 6).>> 2shifts right by two places, dividing by 4 to give 3; 48 would be a left shift.Question 7 · Control flow
What is the output of this code?
temperature = 25 if temperature > 30: print("hot") elif temperature > 20: print("warm") elif temperature > 10: print("mild") else: print("cold")
- Ahot
- Bwarm mild
- Cmild
- Dwarm
Python tests the conditions from the top and runs only the first branch whose condition is true.
Run this code in the editor25 > 20is the first true test, so the later branches are skipped even though25 > 10is also true.Question 8 · Control flow
Which expression creates the list
[0, 2, 4, 6, 8]?- Alist(range(2, 10, 2))
- Blist(range(0, 10, 2))
- Clist(range(0, 8, 2))
- D[n for n in range(10) if n % 2]
range(start, stop, step)stops beforestop, sorange(0, 10, 2)counts 0 to 8 in steps of 2.range(0, 8, 2)stops at 6,range(2, 10, 2)misses 0, andn % 2is true for the odd numbers.Question 9 · Control flow
What is the output of this code?
attempts = 0 while attempts < 5: attempts += 1 if attempts == 3: break else: attempts = 100 print(attempts)
- A3
- B100
- C5
- D4
A loop's
Run this code in the editorelseblock runs only when the loop ends because its condition became false. Herebreakends the loop whenattemptsis 3, so theelseblock is skipped and 3 is printed.Question 10 · Control flow
What is the output of this code?
total = 0 for value in [4, -1, 6, -3, 2]: if value < 0: continue total += value else: total *= 10 print(total)
- A120
- B12
- C80
- D40
Run this code in the editorcontinueskips only the rest of the current pass, so the negative values are left out andtotalreaches 4 + 6 + 2 = 12. The loop ends withoutbreak, so itselseblock runs and multiplies the total by 10.Question 11 · Control flow
What is the output of this code?
count = 0 for row in range(3): for col in range(row): count += 1 print(count)
- A9
- B6
- C3
- D2
The inner loop runs
Run this code in the editorrowtimes: not at all for row 0, once for row 1 and twice for row 2, socountends at 3. 9 would be the result if the inner loop also usedrange(3).Question 12 · Control flow
Which two statements about loops are true? Select two.
- A
continueends the loop and runs itselseblock. - B
breakexits only the innermost loop that contains it. - C
passskips the rest of the current iteration. - D
breakmay appear in anifstatement outside any loop. - EA loop's
elseblock runs when the loop finishes withoutbreak.
breakleaves the nearest enclosing loop only, and a loop'selseblock runs when nobreakended the loop.continuemoves on to the next pass,passdoes nothing at all, andbreakoutside a loop is a SyntaxError.- A
Question 13 · Control flow
What is the output of this code?
limit = 3 for i in range(limit): limit += 1 i += 10 print(limit, i)
- A6 2
- B4 11
- C3 12
- D6 12
Run this code in the editorrange(limit)is built once, from the value 3, so the loop runs three times whilelimitgrows to 6. Changingiin the body does not affect the next pass, but after the last passikeeps its changed value, 2 + 10.Question 14 · Control flow
What is the output of this code?
values = [0, "", "0", [], None, 0.0] truthy = 0 for item in values: if item: truthy += 1 print(truthy)
- A1
- B0
- C2
- D6
Zero of any number type, empty strings, empty lists and
Run this code in the editorNoneall count as false in a condition. The string"0"is not empty, so it is the only true value in the list.Question 15 · Collections
What is the output of this code?
letters = ["a", "b", "c", "d", "e", "f"] print(letters[1:-1:2])
- A['b', 'd', 'f']
- B['a', 'c', 'e']
- C['b', 'd']
- D['b', 'c', 'd', 'e']
The slice starts at index 1 (
Run this code in the editor'b'), stops before index -1 (the last item,'f') and takes every second item, giving'b'and'd'.'f'would be included only if the stop were left out.Question 16 · Collections
What happens when this code runs?
colors = ["red", "green", "blue"] colors.remove("green") print(colors[2])
- AValueError
- BKeyError
- CTypeError
- DIndexError
Run this code in the editorremove("green")deletes that value, so the list becomes['red', 'blue']with indexes 0 and 1. Index 2 no longer exists, so Python raises IndexError. ValueError would come from removing a value that is not in the list.Question 17 · Collections
What is the output of this code?
original = [1, 2, 3] alias = original clone = original[:] alias.append(4) clone.append(5) print(original)
- A[1, 2, 3, 4]
- B[1, 2, 3]
- C[1, 2, 3, 4, 5]
- D[1, 2, 3, 5]
Run this code in the editoralias = originalgives the same list a second name, so appending throughaliaschangesoriginal. The sliceoriginal[:]makes a new list, so appending 5 tocloneleavesoriginalalone.Question 18 · Collections
What is the output of this code?
record = ("Ana", [90, 85]) record[1].append(70) print(record)
- A('Ana', [90, 85])
- B('Ana', [90, 85, 70])
- C('Ana', [90, 85], 70)
- D('Ana', 90, 85, 70)
A tuple's items cannot be replaced, but an item can be a mutable object.
Run this code in the editorrecord[1]is a list, and appending to it changes that list in place while the tuple still holds the same two objects.Question 19 · Collections
What is the output of this code?
stock = {"pens": 10, "pads": 4} stock["clips"] = 25 stock["pens"] = 7 print(len(stock), stock["pens"], stock.get("tape", 0))
- A4 7 0
- B3 10 0
- C3 7 0
- D3 7 None
Assigning to a new key adds it and assigning to an existing key replaces its value, so there are 3 keys and
Run this code in the editor"pens"maps to 7.get()returns its second argument, 0, for a missing key instead of raising KeyError.Question 20 · Collections
What is the output of this code?
title = " data Science " clean = title.strip() clean.upper() print(clean.title(), clean.find("S"), len(title) - len(clean))
- ADATA SCIENCE 5 4
- BData Science 6 4
- CData Science 5 2
- DData Science 5 4
Strings cannot change, so
Run this code in the editorclean.upper()returns a new string that is thrown away.title()capitalizes each word,find("S")gives index 5 (counting from 0), andstrip()removed two spaces from each end.Question 21 · Collections
After which two assignments is
ta tuple? Select two.- At = (5)
- Bt = [5]
- Ct = 5,
- Dt = {5}
- Et = tuple([5])
The comma is what makes a tuple, so
5,is a one-item tuple, andtuple()converts the list into one.(5)is the number 5 in parentheses,[5]is a list and{5}is a set.Question 22 · Collections
What is the output of this code?
grid = [[0] * 3] * 2 grid[0][1] = 7 print(grid)
- A[[0, 7, 0], [0, 0, 0]]
- B[[0, 7, 0], [0, 7, 0]]
- C[[7, 7, 7], [0, 0, 0]]
- D[[0, 0, 0], [0, 7, 0]]
Run this code in the editor* 2repeats a reference to one inner list, so both rows are the same list object and a change throughgrid[0]shows in both. A comprehension such as[[0] * 3 for row in range(2)]builds separate rows.Question 23 · Functions and exceptions
What is the output of this code?
def greet(name, greeting="Hello", mark="!"): return greeting + ", " + name + mark print(greet("Mia", mark="?"))
- AHello, Mia!
- B?, Mia!
- CHello, Mia?
- DHello Mia?
Run this code in the editor"Mia"fillsnameby position,markis set by keyword, andgreetingkeeps its default value. Passing"?"as a second positional argument would have replacedgreetinginstead.Question 24 · Functions and exceptions
What is the output of this code?
def countdown(n): if n == 0: return print(n, end=" ") countdown(n - 1) print(countdown(3))
- A3 2 1 None
- B3 2 1
- C3 2 1 0 None
- DNone 3 2 1
The recursive calls print 3, 2 and 1, and the call with 0 returns before printing.
Run this code in the editorcountdownnever returns a value, so the outer call returnsNone, which the lastprintshows after the numbers.Question 25 · Functions and exceptions
What is the output of this code?
counter = 10 def bump(): global counter counter += 1 step = 5 return counter + step def shadow(): counter = 0 return counter print(bump(), shadow(), counter)
- A16 0 10
- B16 0 0
- C15 0 11
- D16 0 11
Run this code in the editorglobal counterletsbumpchange the module-level name, so it becomes 11 andbumpreturns 16. Inshadow, the assignment creates a localcounterthat hides the global one without changing it.Question 26 · Functions and exceptions
Which exception does this code raise?
def average(first, second): return (first + second) / 2 print(average(4))
- ATypeError
- BValueError
- CNameError
- DZeroDivisionError
Calling a function with fewer arguments than it has parameters without defaults raises TypeError: missing 1 required positional argument: 'second'. Python never fills a missing argument with 0 or
Run this code in the editorNoneby itself.Question 27 · Functions and exceptions
What is the output of this code?
try: values = [1, 2, 3] print(values[3]) except LookupError: print("lookup") except IndexError: print("index") except Exception: print("other")
- Aindex
- Blookup
- Cother
- Dlookup index
Python runs only the first
Run this code in the editorexceptbranch that matches. IndexError is a subclass of LookupError, so the first branch catches it and the more specificexcept IndexErrornever runs. Put specific exceptions before general ones.Question 28 · Functions and exceptions
What is the output of this code?
def parse(text): return int(text) def safe_total(items): total = 0 for item in items: try: total += parse(item) except ValueError: total -= 1 return total print(safe_total(["4", "x", "10", "2.5"]))
- A15
- B3
- C14
- D12
The ValueError raised inside
Run this code in the editorparsetravels up to the caller, whereexcepthandles it and the loop carries on.int("2.5")also raises ValueError, sinceint()does not accept a decimal string, so the total is 4 - 1 + 10 - 1.Question 29 · Functions and exceptions
What is the output of this code?
def add_item(item, basket=[]): basket.append(item) return basket add_item("egg") print(add_item("milk"), add_item("tea", []))
- A['milk'] ['tea']
- B['egg', 'milk'] ['egg', 'milk', 'tea']
- C['egg', 'milk'] ['tea']
- D['milk'] ['milk', 'tea']
The default list is created once, when
Run this code in the editordefruns, and every call that leaves outbasketshares it. So the second call appends to the list that already holds'egg', while the call that passes[]gets a list of its own.Question 30 · Functions and exceptions
Which two statements about Python's built-in exceptions are true? Select two.
- A
KeyboardInterruptis a subclass ofException. - B
ValueErroris a subclass ofTypeError. - C
ZeroDivisionErroris a subclass ofArithmeticError. - D
except Exception:also catchesSystemExit. - E
KeyErrorandIndexErrorare both subclasses ofLookupError.
ZeroDivisionError comes under ArithmeticError, and KeyError and IndexError come under LookupError. KeyboardInterrupt and SystemExit derive from BaseException directly, so
except Exceptionmisses them, and ValueError and TypeError are separate branches.- A
Next: the PCAP practice test, a free Python certification exam with a certificate anyone can check, or MCQ practice by topic.
PCEP and PCAP are certifications of the Python Institute (OpenEDG), which is not affiliated with this website and does not endorse it. These practice questions are written for this site in the style of those exams; they are not the exams’ own questions.