PCEP-30-02 Sample Questions & Answers
Checks your knowledge of branching and looping with conditional blocks, the single biggest weight, plus organizing code into functions, Python's built-in exception hierarchy, collecting data with lists, tuples and dictionaries, and core fundamentals like literals.
Launch the full PCEP-30-02 simulator →Showing 10 of 20 free samples.
- Question 1Advanced
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']Show answer & explanation
Correct answer: A
This list comprehension is the most concise and Pythonic solution. It iterates through the
wordslist, 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. - Question 2Intermediate
Computer Programming and Python Fundamentals · the accuracy of floating-point numbers
True or False: In Python, the expression
0.1 + 0.2 == 0.3evaluates toTrue.Show answer & 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.2is a value extremely close to, but not exactly, 0.3. This is a fundamental concept in computer science regarding floating-point arithmetic. - Question 3Beginner
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
Trueif the number is between 50 and 100, inclusive. Which boolean expression correctly and most concisely implements this logic?Show answer & 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. - Question 4Beginner
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?
Show answer & 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 == 7is met, 'Found it!' is printed, and thebreakstatement terminates the loop before it can process any further numbers. - Question 5Intermediate
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?
Show answer & 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 aKeyError. It does not modify the dictionary. - Question 6Intermediate
Functions and Exceptions · Name scopes, name hiding (shadowing), and the global keyword
A junior developer is trying to understand variable scope in Python. They have written the following code. What will be the output when this script is executed?
value = 100 def my_func(): value = 50 print(f"Inside function: {value}") my_func() print(f"Outside function: {value}")Show answer & explanation
Correct answer: B
This is correct. The variable
valuedefined outside the function has a global scope. Insidemy_func, the linevalue = 50creates a new local variable namedvaluethat exists only within the function's scope (this is called shadowing). This local variable does not affect the global variable of the same name. Therefore, 50 is printed inside the function, and the original global value of 100 is printed outside. - Question 7IntermediateSelect 2
Data Collections – Tuples, Dictionaries, Lists, and Strings · Collect and process data using lists
A programmer needs to combine two lists into a single list. Which of the following methods will create a new list containing all elements from
list1followed by all elements fromlist2, without modifying the original lists? (Select TWO)Show answer & explanation
Correct answers: A, D
The
+operator, when used with lists, performs concatenation. It creates and returns a completely new list containing the elements of both operands, leaving the original lists unchanged.The iterable unpacking operator (
*) can be used within a list literal to create a new list.*list1unpacks all elements oflist1, and*list2unpacks all elements oflist2, creating a new combined list without altering the originals. - Question 8Advanced
Functions and Exceptions · Recursion
What is the output of the
print()function in the following code snippet?def recursive_sum(n): if n <= 1: return 1 else: return n + recursive_sum(n - 2) print(recursive_sum(5))flowchart TD A(recursive_sum(5)) --> B{5 <= 1? No}; B --> C[return 5 + recursive_sum(3)]; C --> D(recursive_sum(3)); D --> E{3 <= 1? No}; E --> F[return 3 + recursive_sum(1)]; F --> G(recursive_sum(1)); G --> H{1 <= 1? Yes}; H --> I[return 1]; F -- 1 --> C; C -- 1+3=4 --> B; B -- 4+5=9 --> Z([Final Return: 9]);Show answer & explanation
Correct answer: B
The function execution unfolds as follows:
recursive_sum(5)is called. Since 5 > 1, it returns5 + recursive_sum(3).recursive_sum(3)is called. Since 3 > 1, it returns3 + recursive_sum(1).recursive_sum(1)is called. Since 1 <= 1, it returns1(the base case).- The result
1is returned to therecursive_sum(3)call, which computes3 + 1 = 4. - The result
4is returned to therecursive_sum(5)call, which computes5 + 4 = 9.
The final result is 9.
- Question 9Beginner
Computer Programming and Python Fundamentals · Perform Input/Output console operations
A Python script needs to print a formatted string that includes a user's name and age. The desired output is:
User: Alice, Age: 30. Givenname = 'Alice'andage = 30, which of the followingprintstatements will produce the exact output, including the spacing?Show answer & explanation
Correct answer: C
Formatted string literals (f-strings) are the modern, recommended way to format strings in Python. They allow embedding expressions inside string literals using curly braces
{}. This code is readable, concise, and produces the exact desired output. - Question 10Intermediate
Control Flow – Conditional Blocks and Loops · expanding loops with while-else and for-else
Consider the following
for-elseloop. What is the final output of this code snippet?for i in range(5): if i == 5: break else: print("Loop completed without a break.") print("Done.")Show answer & explanation
Correct answer: B
The
elseblock associated with aforloop executes if and only if the loop terminates naturally (i.e., it was not exited via abreakstatement). In this case,range(5)iterates from 0 to 4. The conditioni == 5is never met, sobreakis never called. The loop finishes its full course, triggering theelseblock. Finally, the statement after the loop is executed.
Ready for the real thing?
The full PCEP-30-02 simulator has every exam-style question, timed mode, and instant scoring.