PCAP Exceptions and File I/O Practice Question
Exhibit
{
"logging": {
"level": "DEBUG",
"file": "/var/log/app.log"
},
"database": {
"host": "localhost",
"port": 5432
}
}Refer to the exhibit. A developer is writing a script to read this JSON configuration file. The script should write the logging configuration to a separate file called 'logging.conf'. Which file mode should be used to create the file if it doesn't exist, and overwrite it if it does?
⚠ Common exam trap
Python Institute often tests the distinction between 'w' and 'x' modes, where candidates mistakenly choose 'x' thinking it creates a new file, forgetting that 'x' raises an error if the file already exists, thus failing the overwrite requirement.
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
✓
'w'
('w') is correct because the 'w' mode opens a file for writing, truncating it first if it exists, and creating it if it does not. This matches the requirement to overwrite an existing 'logging.conf' file or create a new one if absent.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✗
'x'
Why it's wrong here
Mode 'x' stands for exclusive creation: it attempts to create a new file for writing but immediately raises FileExistsError if the given path already exists. Because the script likely expects to overwrite an existing file (or create it if missing), this mode would abort the operation instead of truncating and rewriting the content, making it incorrect for the intended behavior.
- ✗
'r+'
Why it's wrong here
Mode 'r+' opens the file for both reading and writing without truncating it, and it raises FileNotFoundError if the file does not exist. Since this mode never creates a missing file, it cannot fulfill a requirement to write to a fresh output, and any writes overwrite bytes at the current file position, leaving leftover trailing data from longer prior content rather than producing a clean result.
- ✗
'a'
Why it's wrong here
Mode 'a' opens the file for appending, positioning the file pointer at the end so that all writes go to the end regardless of seek positions, and it never truncates existing content. Using 'a' would cause new data to accumulate after any prior contents, resulting in a file that mixes old and new output, which is not a clean replacement of the file's contents.
- ✓
'w'
Why this is correct
Mode 'w' opens the file for writing, creating the file if it does not exist and truncating it to zero length if it does, thereby discarding all previous contents. This exactly matches the need to write a complete, fresh set of data: any existing content is erased before the first write, ensuring that the final file contains only the current output with no stale data.
Go deeper
Related to this question
About these practice questions
Courseiva writes every PCAP question from scratch — 169 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 →
Same concept, more angles
1 more way this is tested on PCAP
These questions test the same concept from different angles. Work through them to make sure you can recognise it however the exam phrases it.
Variation 1. Which TWO of the following file modes will create a new file if it doesn't exist? (Select exactly 2)
easy- ✓ A.'x'
- ✓ B.'a'
- C.'rb'
- D.'w'
- E.'r'
Why A: ('x') is correct because exclusive creation mode creates a new file and fails if it already exists. Option B ('a') is correct because append mode creates a new file if it does not exist, allowing data to be added at the end. Option D ('w') also creates a file if missing, but it truncates existing content, which may cause data loss; however, the question requires exactly two correct answers, and the non-destructive creation modes are 'x' and 'a'.
JA
Written by Johnson Ajibi, MSc IT Security
Senior Network & Security Engineer · founder of Courseiva
This PCAP 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 PCAP exam.