PCEP study materials
PCEP study resources

PCEP practice questions

One question at a time. Build your PCEP understanding.

First 50 of 200 questions. Each one lists the choices, the correct answer, and an explanation. The checker at the top of this page saves progress in this browser when it is available.

Continue in the practice tool above to load more questions, 50 at a time.

Question 1Computer Programming and Python Fundamentals

Which of the following is a valid Python identifier?

  1. _total2 Correct answer.
  2. class
  3. total-2
  4. 2nd_place

Explanation

A Python identifier may contain letters, digits, and underscores, but it cannot begin with a digit, contain a hyphen, or be a reserved keyword. The name _total2 satisfies all of these rules.

Question 2Computer Programming and Python Fundamentals

What is the result of the expression "py" * 3?

  1. py3
  2. py py py
  3. TypeError
  4. pypypy Correct answer.

Explanation

Multiplying a string by an integer repeats the string that many times, with no separator inserted. Thus "py" * 3 produces the single string pypypy.

Question 3Computer Programming and Python Fundamentals

What value does int(3.9) produce?

  1. ValueError
  2. 4
  3. 3.9
  4. 3 Correct answer.

Explanation

Calling int() on a float truncates the fractional part toward zero rather than rounding it. Therefore int(3.9) returns the integer 3.

Question 4Computer Programming and Python Fundamentals

What is the value of this expression: 2 + 3 * 4 ** 2 // 8?

  1. 10
  2. 14
  3. 5
  4. 8 Correct answer.

Explanation

Exponentiation is evaluated first, giving 4 ** 2 = 16. Then multiplication and floor division are evaluated left to right: 3 * 16 = 48 and 48 // 8 = 6, and finally 2 + 6 = 8.

Question 5Computer Programming and Python Fundamentals

What is the value of the expression 17 // 5 + 17 % 5?

  1. 6
  2. 5 Correct answer.
  3. 3
  4. 7

Explanation

The // operator gives the floor quotient 3, and the % operator gives the remainder 2. Adding those results produces 5.

Question 6Computer Programming and Python Fundamentals

A program runs name = input("Name: ") and the user types Ada. What type does the variable name hold?

  1. bool
  2. int
  3. float
  4. str Correct answer.

Explanation

The input() function always returns a string containing the characters the user typed. Numeric-looking input must be converted explicitly with int() or float() before arithmetic is performed.

Question 7Computer Programming and Python Fundamentals

What does this program print? x = 5 if x > 0: x = x + 10 elif x == 5: x = x + 20 print(x)

  1. 25
  2. 15 Correct answer.
  3. 5
  4. 20

Explanation

The first condition x > 0 is true, so x becomes 15. Because an if/elif chain stops after the first true branch, the elif test is skipped even though x was originally 5.

Question 8Computer Programming and Python Fundamentals

What output does this code produce? print("a", "b", sep="-", end="!") print("c")

  1. a-b!c Correct answer.
  2. a-b!\nc
  3. a-b! c
  4. a b!c

Explanation

The sep argument places a hyphen between the two printed values, and end="!" replaces the usual newline with an exclamation mark. The second print then continues on the same line and appends c.

Question 9Computer Programming and Python Fundamentals

What does this program print? n = 0 while n < 5: n += 1 if n == 3: break else: print("done") print(n)

  1. done
  2. done\n3
  3. 3 Correct answer.
  4. 5

Explanation

The loop stops with break when n reaches 3. A while loop's else clause runs only when the loop ends normally, so the break skips it and only the final print executes.

Question 10Computer Programming and Python Fundamentals

What is the value of this expression: not 5 % 2 or 3 ** 2 < 10?

  1. False
  2. 9
  3. 1
  4. True Correct answer.

Explanation

Exponentiation and modulo are evaluated before comparison, so 3 ** 2 < 10 is True and 5 % 2 is 1. The not operator turns 1 into False, and False or True evaluates to True.

Question 11Computer Programming and Python Fundamentals

Which of the following built-in Python types is immutable, meaning its contents cannot be changed after the object is created?

  1. tuple Correct answer.
  2. dict
  3. set
  4. list

Explanation

A tuple is immutable: once it is created, its elements cannot be replaced, added, or removed. Lists, dictionaries, and sets are mutable container types whose contents can be modified in place. Attempting to assign to a tuple element raises a TypeError.

Question 12Computer Programming and Python Fundamentals

Which of these values is treated as false when used as a condition in Python?

  1. [0]
  2. 0 Correct answer.
  3. "0"
  4. -1

Explanation

Python treats numeric zero, empty containers, empty strings, None, and False as falsy. The string "0" is non-empty, the list [0] contains one element, and -1 is a nonzero integer, so all three are truthy.

Question 13Computer Programming and Python Fundamentals

What is the result of evaluating the expression "3" + 3 in Python?

  1. 6
  2. "3 3"
  3. "33"
  4. TypeError Correct answer.

Explanation

The + operator requires operands of compatible types. A string and an integer cannot be added, so Python raises a TypeError instead of converting automatically. An explicit conversion such as "3" + str(3) would be required.

Question 14Computer Programming and Python Fundamentals

What does this program print? a = [1, 2, 3] b = a b.append(4) print(a)

  1. [1, 2, 3]
  2. [1, 2, 3, 4] Correct answer.
  3. [1, 2, 3, 4, 4]
  4. [4]

Explanation

Assigning b = a copies the reference to the list, not the list itself, so both names refer to the same object. Calling append through b modifies that shared list. Printing a therefore shows the updated contents [1, 2, 3, 4].

Question 15Computer Programming and Python Fundamentals

What does this program print? s = "programming" print(s[3:7])

  1. "gram" Correct answer.
  2. "pro"
  3. "ram"
  4. "ogra"

Explanation

Slicing uses a start index that is included and a stop index that is excluded, with indices counted from 0. In "programming", index 3 is g and index 6 is m, so s[3:7] returns "gram". The character at index 7 is not included.

Question 16Computer Programming and Python Fundamentals

Which list is produced by list(range(1, 10, 3))?

  1. [1, 4, 7, 10]
  2. [1, 2, 3]
  3. [1, 4, 7] Correct answer.
  4. [3, 6, 9]

Explanation

range(start, stop, step) begins at 1, stops before 10, and increments by 3. The generated values are 1, 4, and 7; the next value, 10, is not less than the stop value. Converting the range with list() materializes those values.

Question 17Computer Programming and Python Fundamentals

What does this program print? d = {"a": 1} print(d.get("b", 0))

  1. 0 Correct answer.
  2. None
  3. KeyError
  4. 1

Explanation

The dict.get method returns the supplied default when the key is absent instead of raising an exception. Because "b" is not in the dictionary, the default 0 is printed. The existing key "a" would have returned 1.

Question 18Computer Programming and Python Fundamentals

What list is produced by this expression? [x * 2 for x in range(4) if x % 2]

  1. [2, 4, 6]
  2. [0, 2, 4, 6]
  3. [2, 6] Correct answer.
  4. [1, 3]

Explanation

The comprehension iterates over 0, 1, 2, and 3 and keeps only values for which x % 2 is nonzero. Those values are 1 and 3, and each is multiplied by 2. The resulting list is [2, 6].

Question 19Computer Programming and Python Fundamentals

What does this program print? def add(item, bag=[]): bag.append(item) return bag print(add(1)) print(add(2))

  1. first [1], then [1, 2] Correct answer.
  2. first [], then [1, 2]
  3. first [1], then [2]
  4. first [2], then [1, 2]

Explanation

Default argument values are evaluated once when the function is defined, so the same list object is reused on every call. The first call appends 1 and returns [1]. The second call appends 2 to that same list, returning [1, 2].

Question 20Computer Programming and Python Fundamentals

What does this program print? nums = [3, 1, 2] result = nums.sort() print(result)

  1. [1, 2, 3]
  2. sorted [1, 2, 3]
  3. [3, 1, 2]
  4. None Correct answer.

Explanation

The list.sort method sorts the list in place and returns None rather than a new list. The variable result therefore receives None. The built-in sorted function would instead return a new sorted list.

Question 21Computer Programming and Python Fundamentals

What is the type of the value produced by the expression 7 / 2 in Python 3?

  1. integer
  2. string
  3. boolean
  4. float Correct answer.

Explanation

In Python 3, the / operator performs true division, so it always returns a float. Even though 7 divided by 2 has no fractional remainder, the result is 3.5 with type float.

Question 22Computer Programming and Python Fundamentals

What does this program print? x = 10 x -= 3 print(x)

  1. 13 Correct answer.
  2. 3
  3. 10
  4. 7

Explanation

The augmented assignment x -= 3 subtracts 3 from the current value of x and rebinds x to the result. Starting from 10, the new value is 7.

Question 23Computer Programming and Python Fundamentals

What does this program print? word = "Python" print(word[1])

  1. y Correct answer.
  2. P
  3. o
  4. t

Explanation

String indexing is zero-based, so word[0] is 'P' and word[1] is the second character, 'y'. The print call displays only that single character.

Question 24Computer Programming and Python Fundamentals

What does this program print? x = 5 def change(): x = 10 change() print(x)

  1. 15
  2. 10
  3. 5 Correct answer.
  4. NameError

Explanation

Assigning x inside change creates a new local variable named x because there is no global declaration. The module-level x remains 5, so the final print shows 5.

Question 25Computer Programming and Python Fundamentals

What does this program print? nums = [1, 2] nums.extend([3, 4]) print(nums)

  1. [1, 2, [3, 4]]
  2. [3, 4, 1, 2]
  3. [1, 2, 3, 4] Correct answer.
  4. [[1, 2], 3, 4]

Explanation

list.extend adds each element of the iterable to the end of the list, so the numbers 3 and 4 become separate items. The original list is mutated to [1, 2, 3, 4].

Question 26Computer Programming and Python Fundamentals

What does this program print? d = {"a": 1, "b": 2} print(list(d))

  1. ['a', 'b'] Correct answer.
  2. [1, 2]
  3. {'a', 'b'}
  4. [('a', 1), ('b', 2)]

Explanation

Iterating over a dictionary yields its keys, not its values or key-value pairs. Wrapping the iteration in list produces a list of the keys in insertion order, ['a', 'b'].

Question 27Computer Programming and Python Fundamentals

What happens when this code runs? s = "abc" s[0] = "A"

  1. The string becomes 'A' and drops the rest
  2. A TypeError is raised Correct answer.
  3. The string becomes 'abcA' in place
  4. The string becomes 'Abc' in place

Explanation

Strings are immutable, so assigning to a single character position is not allowed. The statement s[0] = 'A' raises TypeError before any change can occur.

Question 28Computer Programming and Python Fundamentals

What does this program print? for i, ch in enumerate("ab", start=1): print(i, ch)

  1. a 1 b 2
  2. 1 a 1 b
  3. 1 a 2 b Correct answer.
  4. 0 a 1 b

Explanation

enumerate yields pairs of an index and the corresponding item. With start=1, the first pair is (1, 'a') and the second is (2, 'b'), and print displays each pair with a space.

Question 29Computer Programming and Python Fundamentals

What does this program print? def f(a, b=2, *args): return a + b + len(args) print(f(1, 3, 4, 5))

  1. 5
  2. 10
  3. 6 Correct answer.
  4. 7

Explanation

The first positional argument 1 binds to a, the second argument 3 binds to b, and the remaining arguments 4 and 5 are collected in args. Thus len(args) is 2, and a + b + len(args) equals 1 + 3 + 2 = 6.

Question 30Computer Programming and Python Fundamentals

What does this program print? a = [1, 2] b = [1, 2] print(a == b, a is b)

  1. True False Correct answer.
  2. False True
  3. True True
  4. False False

Explanation

The == operator compares list contents, and both lists contain 1 and 2, so the first result is True. The is operator compares object identity; a and b are two distinct list objects, so the second result is False.

Question 31Computer Programming and Python Fundamentals

Which identifier is accepted by the Python interpreter as a variable name?

  1. user-name
  2. total_sum Correct answer.
  3. class
  4. 2nd_place

Explanation

Python variable names must start with a letter or underscore and may contain letters, digits, and underscores. They cannot begin with a digit, use keywords such as class, or include hyphens.

Question 32Computer Programming and Python Fundamentals

What does print(-7 // 2) output?

  1. -4 Correct answer.
  2. -3
  3. -3.5
  4. 3

Explanation

The // operator performs floor division, so -7 divided by 2 is -3.5 and is rounded down to the next lower integer, -4. It does not truncate toward zero.

Question 33Computer Programming and Python Fundamentals

What does this program print? a, b = 1, 2 a, b = b, a print(a, b)

  1. 2 1 Correct answer.
  2. 2 2
  3. 1 1
  4. 1 2

Explanation

In a multiple assignment, the right-hand side is evaluated first as a tuple and then unpacked. The values b and a are therefore read before either variable is rebound, producing 2 1.

Question 34Computer Programming and Python Fundamentals

What does print(0.1 + 0.2 == 0.3) output?

  1. False Correct answer.
  2. Error
  3. True
  4. None

Explanation

Binary floating-point cannot represent 0.1, 0.2, and 0.3 exactly, so 0.1 + 0.2 is slightly different from 0.3. The comparison therefore evaluates to False, and print displays False.

Question 35Control Flow: Conditional Blocks and Loops

What is printed by this code? x = 10 if x > 5: print("A") elif x > 8: print("B") else: print("C")

  1. A then B
  2. C
  3. B
  4. A Correct answer.

Explanation

An if/elif chain tests conditions in order and executes only the first branch whose condition is true. Since 10 > 5 is true, the elif and else branches are skipped entirely, so only A is printed.

Question 36Control Flow: Conditional Blocks and Loops

What output does this loop produce? for i in range(2, 10, 3): print(i, end=" ")

  1. 3 6 9
  2. 2 4 6 8
  3. 2 5 8 Correct answer.
  4. 2 3 4 5 6 7 8 9

Explanation

range(start, stop, step) begins at start and adds step until it reaches or passes stop, excluding stop itself. Starting at 2 with step 3 gives 2, 5, and 8, so the loop prints those values.

Question 37Control Flow: Conditional Blocks and Loops

What is printed by this code? n = 0 while n < 5: n += 2 print(n)

  1. 4
  2. 7
  3. 6 Correct answer.
  4. 5

Explanation

The condition is checked before each iteration. n takes the values 0, 2, 4, and then 6; when n becomes 6, 6 < 5 is false and the loop stops, leaving n equal to 6.

Question 38Control Flow: Conditional Blocks and Loops

What is printed by this code? for i in range(4): if i == 2: break else: print("else") print(i)

  1. 0
  2. 2 Correct answer.
  3. else 2
  4. else

Explanation

A loop's else clause runs only when the loop terminates normally, not when break exits it. The break occurs when i is 2, so the else block is skipped and the final value of i, 2, is printed.

Question 39Control Flow: Conditional Blocks and Loops

What output does this code produce? i = 0 while i < 5: i += 1 if i == 3: continue print(i, end=" ")

  1. 1 2 4 5 Correct answer.
  2. 1 3 4 5
  3. 1 2 4 5 6
  4. 1 2 3 4 5

Explanation

The increment happens before the if, so the counter still advances when continue skips printing. The printed values are 1, 2, then 4 and 5 after 3 is skipped, and the loop ends when i reaches 6.

Question 40Control Flow: Conditional Blocks and Loops

What is printed by this code? x = 4 if x > 0: if x % 2 == 0: print("even") else: print("non-positive")

  1. even Correct answer.
  2. non-positive
  3. even then non-positive
  4. nothing is printed

Explanation

Python binds an else to the nearest if at the same indentation level, and here that is the outer if. The outer condition is true, the inner condition prints even, and the outer else is skipped.

Question 41Control Flow: Conditional Blocks and Loops

What is printed by this code? values = [0, 1, "", "0", [], [0]] count = 0 for v in values: if v: count += 1 print(count)

  1. 2
  2. 6
  3. 3 Correct answer.
  4. 4

Explanation

A condition is true for non-zero numbers and non-empty containers, including strings and lists. The truthy values are 1, '0', and [0], giving a count of 3.

Question 42Control Flow: Conditional Blocks and Loops

What is printed by this code? total = 0 for i in range(1, 6): if i % 2 == 0: continue total += i print(total)

  1. 3
  2. 9 Correct answer.
  3. 6
  4. 15

Explanation

continue skips the rest of the current iteration, so even values are not added. The odd values from 1 through 5 are 1, 3, and 5, whose sum is 9.

Question 43Control Flow: Conditional Blocks and Loops

What is printed by this code? count = 0 for i in range(3): for j in range(3): if j == i: break count += 1 print(count)

  1. 3 Correct answer.
  2. 0
  3. 6
  4. 9

Explanation

break exits only the innermost loop that contains it, so the outer for loop continues with its next value. Counting increments before each inner break gives 0 + 1 + 2 = 3.

Question 44Control Flow: Conditional Blocks and Loops

What is printed by this code? x = 5 y = 0 if x > 3 and y != 0 or x == 5: print("yes") else: print("no")

  1. yes only if y is nonzero
  2. a TypeError is raised
  3. no
  4. yes Correct answer.

Explanation

The and operator has higher precedence than or, so the expression groups as (x > 3 and y != 0) or (x == 5). The first part is false, but x == 5 is true, so the if block runs and prints yes.

Question 45Control Flow: Conditional Blocks and Loops

What is printed by this code? if "False": print("truthy") else: print("falsy")

  1. truthy Correct answer.
  2. falsy
  3. nothing is printed
  4. False

Explanation

A non-empty string is truthy in Python, and the text inside it does not change that. Because "False" contains characters, the if condition succeeds and the program prints truthy.

Question 46Control Flow: Conditional Blocks and Loops

What output does this loop produce? for i in range(6, 1, -2): print(i, end=" ")

  1. 6 5 4 3 2
  2. 2 4 6
  3. 6 4 2 Correct answer.
  4. 6 4 2 0

Explanation

range(6, 1, -2) starts at 6 and stops before 1, decreasing by 2 each time. The generated values are 6, 4, and 2, printed with spaces.

Question 47Control Flow: Conditional Blocks and Loops

What is printed by this code? x = 7 if x > 10: pass print("end")

  1. 7
  2. nothing
  3. end Correct answer.
  4. pass

Explanation

pass is a null statement: it satisfies Python's need for an indented block but performs no action. The condition 7 > 10 is false, so that block is skipped anyway, and print("end") runs because it is outside the if.

Question 48Control Flow: Conditional Blocks and Loops

What is printed by this code? n = 5 while n < 3: n += 1 else: print("else", n)

  1. nothing
  2. else 3
  3. else 8
  4. else 5 Correct answer.

Explanation

The while condition is checked before the first iteration; since 5 < 3 is false, the loop body never runs. A while loop's else block runs when the loop ends without a break, so it prints else 5 with n unchanged.

Question 49Control Flow: Conditional Blocks and Loops

What is printed by this code? i = 0 while i < 3: i += 1 if i == 2: break else: print("else") print(i)

  1. else 2
  2. 2 Correct answer.
  3. 3
  4. else 3

Explanation

The loop increments i to 1, then to 2 and breaks. break skips the loop's else block, and the final print outputs the current value 2.

Question 50Control Flow: Conditional Blocks and Loops

What output does this code produce? x = 10 if x > 5: print("A") if x > 8: print("B")

  1. only A
  2. A then B Correct answer.
  3. B then A
  4. only B

Explanation

These are two separate if statements, not an if/elif chain. For x = 10 both conditions are true, so A prints first and B prints second.