PCEP sample questions with answers

10 free PCEP sample questions across the exam's domains, each with its answer and an explanation. No account needed.

PCEP sample questions

PCEP – Certified Entry-Level Python Programmer (PCEP-30-02), Python Institute (OpenEDG).

  1. Question 1

    Domain: Computer Programming and Python Fundamentals

    Which of these statements causes a SyntaxError in Python 3?

    1. klass = "3A"
    2. class = "3A"
    3. class_ = "3A"
    4. _class = "3A"
    Show the answer

    Answer: B. class = "3A"

    class is a reserved keyword and cannot be used as an identifier, so assigning to it is a syntax error. class_ is tempting to reject too, but adding an underscore makes it a different, perfectly legal identifier (PEP 8 even suggests a trailing underscore to avoid keyword clashes), as are klass and _class.

    Checked against: https://docs.python.org/3/reference/lexical_analysis.html#keywords

  2. Question 2

    Domain: Computer Programming and Python Fundamentals

    When CPython runs a .py source file, what does it do with the source code?

    1. It compiles the source into bytecode and then executes that bytecode on the CPython virtual machine
    2. It translates the source into native machine code and saves an executable next to the file
    3. It executes each line of raw text directly, without any intermediate representation
    4. It passes the source to a separately installed C compiler such as gcc before running it
    Show the answer

    Answer: A. It compiles the source into bytecode and then executes that bytecode on the CPython virtual machine

    The Python glossary describes bytecode as the internal representation of a program in CPython: source is compiled to bytecode (and may be cached in .pyc files) which the interpreter's virtual machine then executes. CPython does not produce native executables, so the native-executable option describes a traditional compiler rather than the reference interpreter.

    Checked against: https://docs.python.org/3/glossary.html#term-bytecode

  3. Question 3

    Domain: Control Flow – Conditional Blocks and Loops

    items = [4, 9, 7, 7]
    for i in range(len(items)):
        if items[i] == 7:
            print("found at", i)
            break
    else:
        print("not found")

    What is printed when this code runs?

    1. found at 3
    2. found at 2
    3. found at 2 not found
    4. not found
    Show the answer

    Answer: B. found at 2

    Indexes start at 0, so the 7 is at index 2; break then exits the loop, so the loop's else (not found) is skipped. found at 3 counts positions from 1 instead of 0.

    Checked against: https://docs.python.org/3/tutorial/controlflow.html#else-clauses-on-loops

  4. Question 4

    Domain: Control Flow – Conditional Blocks and Loops

    a = 3
    b = 7
    if a > 5:
        if b > 5:
            print("X")
        else:
            print("Y")
    else:
        if b > 5:
            print("Z")
        else:
            print("W")

    What is printed when this code runs?

    1. Z
    2. X
    3. Y
    4. W
    Show the answer

    Answer: A. Z

    The outer condition a > 5 is false for 3, so control goes to the outer else; inside it, b > 5 is true for 7, so Z is printed. X would need both conditions to be true, but the inner if under the outer if is never reached.

    Checked against: https://docs.python.org/3/reference/compound_stmts.html#the-if-statement

  5. Question 5

    Domain: Control Flow – Conditional Blocks and Loops

    x = 3
    print(not x == 5)

    What is printed when this code runs?

    1. False
    2. An error, because not cannot be applied to a comparison
    3. True
    4. 3
    Show the answer

    Answer: C. True

    not has lower priority than comparison operators, so not x == 5 means not (x == 5); x == 5 is False for 3, so the result is True. Reading it as (not x) == 5 would give False == 5, which is False, but that is not how Python groups it.

    Checked against: https://docs.python.org/3/reference/expressions.html#operator-precedence

  6. Question 6

    Domain: Data Collections – Tuples, Dictionaries, Lists, and Strings

    d = {"a": 1, "b": 2}
    print(list(d.keys()), list(d.values()))

    What is printed when this code runs?

    1. ['a', 'b'] ['1', '2']
    2. [1, 2] ['a', 'b']
    3. ['a', 'b'] [1, 2]
    4. [('a', 1), ('b', 2)] [1, 2]
    Show the answer

    Answer: C. ['a', 'b'] [1, 2]

    keys() and values() return views of the dictionary's keys and values in insertion order, and list() turns each view into a list. The list of (key, value) tuples would come from items().

    Checked against: https://docs.python.org/3/library/stdtypes.html#dict.keys

  7. Question 7

    Domain: Data Collections – Tuples, Dictionaries, Lists, and Strings

    a = (5)
    b = (5,)
    print(type(a), type(b))

    What is printed when this code runs?

    1. <class 'tuple'> <class 'tuple'>
    2. <class 'int'> <class 'int'>
    3. <class 'tuple'> <class 'int'>
    4. <class 'int'> <class 'tuple'>
    Show the answer

    Answer: D. <class 'int'> <class 'tuple'>

    Parentheses on their own only group an expression, so (5) is just the int 5. It is the trailing comma that makes a one-element tuple, which is why (5,) is a tuple.

    Checked against: https://docs.python.org/3/tutorial/datastructures.html#tuples-and-sequences

  8. Question 8

    Domain: Functions and Exceptions

    def show():
        print("first")
    
    def show():
        print("second")
    
    show()

    What is printed when this code runs?

    1. first
    2. first second
    3. A SyntaxError, because a function is defined twice
    4. second
    Show the answer

    Answer: D. second

    Each def statement binds the name when it is executed, so the second definition rebinds show to a new function object and the first is no longer reachable. Redefining a name is legal, so there is no error.

    Checked against: https://docs.python.org/3/reference/compound_stmts.html#function-definitions

  9. Question 9

    Domain: Functions and Exceptions

    def area(w, h):
        result = w * h
        return result
    
    print(area(2, 4))
    print(result)

    What is printed when this code runs?

    1. 8 8
    2. 8, then a NameError is raised
    3. 8, then None
    4. A NameError is raised before anything is printed
    Show the answer

    Answer: B. 8, then a NameError is raised

    result is assigned inside area, so it is local to that function and disappears when the call ends; the first print shows the returned 8, and the second line cannot find result in the global scope. The first line runs completely before the failing one, so 8 is printed.

    Checked against: https://docs.python.org/3/reference/executionmodel.html#resolution-of-names

  10. Question 10

    Domain: Functions and Exceptions

    prices = {"tea": 2}
    try:
        print(prices["coffee"])
    except LookupError:
        print("lookup")
    except KeyError:
        print("key")
    except Exception:
        print("general")

    What is printed when this code runs?

    1. lookup
    2. key
    3. general
    4. A KeyError traceback
    Show the answer

    Answer: A. lookup

    The except clauses are tried in order and the first one whose class matches the exception runs; KeyError is a subclass of LookupError, so the first clause catches it. The KeyError clause is never reached, which is why ordering a base class first hides more specific handlers.

    Checked against: https://docs.python.org/3/library/exceptions.html#KeyError

More practice

A 20-question practice sampler is free with an account; Pro adds the full question bank and timed mock exams.

PCEP course and practice exam: Certified Entry-Level Python Programmer: the exam guide, with the format, cost, pass mark and domains from the vendor.