Pcep – Certified Entry-Level Python Programmer Free Sample Questions

Description

Covers Python fundamentals and literals, conditional branching and loops, lists, tuples and dictionaries, and decomposing code into functions and the built-in exceptions hierarchy.

20 free sample questions192 in the full practice test

Try simulator

PCEP-30-02 Sample Questions

  1. Question 1

    Beginner

    Computer Programming and Python Fundamentals · Introduce literals and variables into code and use different numeral systems

    A developer is writing a script to process sensor data. The script receives a hexadecimal string representing a temperature reading, such as '0x1A'. The goal is to convert this string into its decimal integer equivalent. Which line of code correctly performs this conversion?

    Answer and explanation

    Correct answer: A

    The int() function can take a second argument for the base. When the base is set to 0, Python automatically infers the base from the string's prefix. '0x' indicates hexadecimal (base 16), '0o' indicates octal (base 8), and '0b' indicates binary (base 2). This is the most robust way to handle prefixed numeral strings.

  2. Question 2

    Intermediate

    Functions and Exceptions · Python Built-In Exceptions Hierarchy

    A Python script is designed to run indefinitely, polling a network service every 10 seconds. An engineer wants to ensure that if the user presses Ctrl+C, the script prints a friendly 'Shutting down...' message instead of crashing with a traceback. Which exception should be caught to handle this specific interruption?

    Answer and explanation

    Correct answer: B

    KeyboardInterrupt is the specific exception raised when the user hits the interrupt key, which is typically Ctrl+C. Catching this exception allows for graceful shutdown logic.

  3. Question 3

    Intermediate

    Control Flow – Conditional Blocks and Loops · Controlling loop execution with break and continue

    What is the final value of result after the following code is executed?

    items = [10, 20, 30, 40, 50]
    result = 0
    for i in range(len(items)):
    if items[i] > 30:
    break
    if items[i] % 20 == 0:
    continue
    result += items[i]
    
    Answer and explanation

    Correct answer: D

    The loop proceeds as follows:

    • items[0] is 10. It's not > 30. It's not divisible by 20. result becomes 10.
    • items[1] is 20. It's not > 30. It IS divisible by 20, so continue is executed, skipping the rest of the loop body. result remains 10.
    • items[2] is 30. It's not > 30. It's not divisible by 20. result becomes 10 + 30 = 40.
    • items[3] is 40. It IS > 30, so break is executed, terminating the loop.
      The final value of result is 40.
  4. Question 4

    Advanced

    Data Collections – Tuples, Dictionaries, Lists, and Strings · Lists inside tuples and tuples inside lists

    A programmer creates a tuple containing a list. They then attempt to modify an element within that list. What is the outcome of this operation?

    data_tuple = (1, 2, [3, 4])
    data_tuple[2][0] = 99
    print(data_tuple)
    
    Answer and explanation

    Correct answer: C

    This is correct. Tuples are immutable, meaning you cannot change the references to the objects they contain. However, if one of those objects is mutable (like a list), you can modify the internal state of that object. The tuple still holds a reference to the same list, but the list's content has changed.

  5. Question 5

    Intermediate

    Functions and Exceptions · Default parameter values

    A developer needs to create a function calculate_price that accepts a base price and an optional discount percentage. If the discount is not provided, it should default to 10%. Which function signature correctly implements this requirement?

    Answer and explanation

    Correct answer: B

    This is the correct syntax. The required parameter base_price is listed first, followed by the optional parameter discount with its default value. This allows the function to be called as calculate_price(100) or calculate_price(100, 0.2).

  6. Question 6

    Advanced

    Data Collections – Tuples, Dictionaries, Lists, and Strings · List comprehensions

    A data processing script needs to create a new list containing only the lengths of strings from an existing list, but only for strings that have more than 3 characters. Which of the following is the most 'Pythonic' way to achieve this using a single line of code?

    words = ['cat', 'window', 'defenestrate', 'dog']

    Answer and explanation

    Correct answer: A

    This list comprehension is the most concise and Pythonic solution. It iterates through the words list, applies a filter (if len(w) > 3), and then applies a transformation (len(w)) to each item that passes the filter, all in one readable line.

  7. Question 7

    Intermediate

    Computer Programming and Python Fundamentals · the accuracy of floating-point numbers

    True or False: In Python, the expression 0.1 + 0.2 == 0.3 evaluates to True.

    Answer and explanation

    Correct answer: B

    This statement is False. Due to the nature of binary floating-point representation, numbers like 0.1 and 0.2 cannot be stored with perfect precision. The result of 0.1 + 0.2 is a value extremely close to, but not exactly, 0.3. This is a fundamental concept in computer science regarding floating-point arithmetic.

  8. Question 8

    Beginner

    Computer Programming and Python Fundamentals · Boolean expressions

    A developer is writing a validation function that checks if a user's input is within a specific numerical range (inclusive). The function should return True if the number is between 50 and 100, inclusive. Which boolean expression correctly and most concisely implements this logic?

    Answer and explanation

    Correct answer: B

    Python supports chained comparisons, which allows for a more readable and mathematically intuitive way to check if a value falls within a range. This expression is equivalent to (50 <= number) and (number <= 100) and is the most concise and 'Pythonic' way to write the check.

  9. Question 9

    Beginner

    Control Flow – Conditional Blocks and Loops · Controlling loop execution with break and continue

    A script needs to iterate through a sequence of numbers from 0 to 9. During the iteration, it should print 'Found it!' and stop processing immediately when it encounters the number 7. For all other numbers before 7, it should just print the number itself. What is the expected output of a correctly written loop for this task?

    Answer and explanation

    Correct answer: B

    This is the correct output. The loop iterates from 0 up to 6, printing each number. When the loop variable becomes 7, the condition if number == 7 is met, 'Found it!' is printed, and the break statement terminates the loop before it can process any further numbers.

  10. Question 10

    Intermediate

    Data Collections – Tuples, Dictionaries, Lists, and Strings · Checking the existence of keys

    A Python dictionary is used to store user preferences. A function needs to retrieve the 'theme' preference. If the 'theme' key does not exist, the function should return 'light' without causing an error and without modifying the original dictionary. Which line of code achieves this safely?

    Answer and explanation

    Correct answer: C

    The .get() method is the correct and safest way to achieve this. It attempts to retrieve the value for the given key. If the key is not found, it returns the default value provided as the second argument ('light' in this case) instead of raising a KeyError. It does not modify the dictionary.

Register free for 10 more questions

Or unlock all 192 PCEP-30-02 questions with explanations, timed mode and flashcards.