PCEP Practice Question: Data Types, Variables, Basic I/O and Operators
A system administrator writes a script to monitor disk usage. The script reads a percentage from a file as a string, e.g., "100". The code:
usage = open("usage.txt").read().strip()
if usage > 80:
print("Warning: disk usage high")else:
print("Disk usage OK")Even when usage.txt contains "100", the script prints "Disk usage OK". The admin expected "Warning". What is the problem and how to fix?
⚠ Common exam trap
Python Institute often tests the subtle behavior that Python allows cross-type comparisons (string vs. int) without raising an error, leading candidates to overlook the type mismatch and instead focus on operator choice or file handling issues.
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 file reading returns a string; convert usage to int before comparison.
The `read()` method returns the file content as a string. In Python, comparing a string to an integer with `>` performs lexicographic (character-by-character) comparison, not numeric comparison. For example, `"100" > 80` evaluates to `False` because `"1"` (ASCII 49) is less than `80` (integer), so the condition fails and the script prints "Disk usage OK". Converting the string to an integer with `int(usage)` before the comparison ensures numeric comparison works as intended.
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 comparison operator > is incorrect; use >= instead.
Why it's wrong here
Using >= would still exhibit the same problem.
- ✗
The file is not properly closed; use with statement.
Why it's wrong here
The file is automatically closed after read, but not the core issue.
- ✓
The file reading returns a string; convert usage to int before comparison.
Why this is correct
String comparison can yield unexpected results; convert to int.
- ✗
The strip() method removes newlines but not spaces; usage may have extra spaces.
Why it's wrong here
strip() removes leading/trailing whitespace, but the core issue is string comparison.
Go deeper
Related to this question
About these practice questions
Courseiva writes every PCEP question from scratch — 482 in total, each with an explanation and a wrong-answer breakdown. None are copied from real exams or 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.