PCEP set() ordering Practice Question
A junior developer is tasked with writing a Python script that reads a list of integers from a file, removes any duplicate numbers, and then writes the unique numbers back to the same file in ascending order. The file 'numbers.txt' currently contains one integer per line. The developer writes the following code:
with open('numbers.txt', 'r') as f:numbers = [int(line.strip()) for line in f] unique = list(set(numbers)) unique.sort()
with open('numbers.txt', 'w') as f:
for num in unique:f.write(str(num) + '\n')
The script runs without errors, but the output file contains the numbers in descending order instead of ascending. The developer checks the sort() method and confirms it sorts in ascending order. What is the MOST likely cause of the issue?
⚠ Common exam trap
The trap is that set() does not guarantee order, and sort() modifies the list in-place. Candidates may assume set() preserves order or that sort() returns a new list, leading them to miss the actual error if the code deviates from the snippet shown.
Answer choices
Why each option matters
Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.
Correct answer & explanation
✓
The set() operation reordered the elements, and the sort() method was called on a different list that was not saved.
The code snippet as shown uses unique.sort() to sort the list in-place and then writes the sorted list, so it would produce ascending order if executed exactly. The discrepancy between the expected ascending order and the reported descending order strongly suggests a deviation from the snippet, such as the developer accidentally calling sort() on a different list (for example, sorting the original numbers list or a temporary copy) and then writing the unsorted unique list. Option B captures this idea: the sort() method was called on a list that was not saved, leading to the unsorted list being written. The other options are not consistent with the provided code: A is incorrect because the numbers are correctly converted to integers; C and D misrepresent file behavior (the file is opened in write mode, not append, and reading does not truncate the file).
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✗
The numbers were read as strings and the sort() method sorted them lexicographically, putting '10' before '2'.
Why it's wrong here
The code converts to int.
- ✓
The set() operation reordered the elements, and the sort() method was called on a different list that was not saved.
Why this is correct
If the developer wrote unique = list(set(numbers)).sort(), unique would be None. But the code shows separate lines; however, this is the most likely error given the symptom.
- ✗
The file was opened in read mode before writing, but the read operation truncated the file.
Why it's wrong here
Reading does not truncate.
- ✗
The file was opened in append mode instead of write mode, causing the sorted numbers to be appended after the original unsorted numbers.
Why it's wrong here
The code uses 'w' mode, so it overwrites.
Go deeper
Related to this question
About these practice questions
This PCEP question is part of Courseiva's 498-question bank — original exam-style content with full explanations and wrong-answer analysis, never real exam questions or exam dumps. Learn why practice questions differ from exam dumps →
JA
Written by Johnson Ajibi, MSc IT Security
Senior Network & Security Engineer · founder of Courseiva
This PCEP practice question is part of Courseiva's free Python Institute certification practice question bank. Courseiva provides original exam-style practice questions with explanations, topic-based practice, mock exams, readiness tracking, and study analytics to help learners prepare for the PCEP exam.