The answer is 'w' mode. This is correct because the 'w' mode opens a file for writing, truncating any existing content to zero length, and automatically creates the file if it does not already exist, perfectly matching the requirement to overwrite or create 'logging.conf'. In contrast, 'a' mode would append to an existing file rather than overwrite it, and 'r+' would raise an error if the file is missing. On the Certified Associate Python Programmer PCAP exam, this question tests your understanding of file modes and their behavior with nonexistent files, often appearing in scenarios involving configuration or log file handling. A common trap is confusing 'w' with 'a' when the intent is to overwrite; remember that 'w' stands for "write and wipe," while 'a' stands for "append only." For a quick memory tip: think of 'w' as "write from scratch" — it always starts fresh, whether the file exists or not.
PCAP Exceptions and File I/O Practice Question
This PCAP practice question tests your understanding of exceptions and file i/o. This is a configuration task: choose the command set that satisfies every stated requirement. Small differences — like 'secret' vs 'password' or 'transport input ssh' vs 'all' — change whether the answer is correct. After answering, compare your reasoning against the explanation and wrong-answer breakdown below. Once you have made your selection, read the full explanation to reinforce the concept and understand why each distractor is designed to mislead on exam day.
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?
Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.
Correct answer & explanation
✓
'w'
Option D ('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.
Key principle: Answer the scenario, not the keyword: identify the specific constraint before choosing the most familiar-sounding option.
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' exclusively creates the file, fails if it already exists.
✗
'r+'
Why it's wrong here
Mode 'r+' opens for updating (reading and writing) but does not create the file if it doesn't exist.
✗
'a'
Why it's wrong here
Mode 'a' opens for appending; does not truncate, so content would accumulate.
✓
'w'
Why this is correct
Mode 'w' opens for writing, truncating existing file or creating new one.
Related concept
Read the scenario before looking for a memorised answer.
Common exam traps
Common exam trap: answer the scenario, not the keyword
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.
Detailed technical explanation
How to think about this question
In Python, file modes are strings passed to the built-in open() function, and 'w' corresponds to write mode with truncation (O_WRONLY | O_CREAT | O_TRUNC on POSIX systems). A subtle behavior is that 'w' will overwrite the file even if it contains important data, so developers should ensure backups or use 'x' for safety when creating new files. In real-world scenarios, configuration file generation often uses 'w' to ensure a clean state, but logging handlers may use 'a' to preserve logs across runs.
KKey Concepts to Remember
Read the scenario before looking for a memorised answer.
Find the constraint that changes the correct option.
Eliminate answers that are true in general but not in this case.
TExam Day Tips
→Watch for words such as best, first, most likely and least administrative effort.
→Review why wrong options are wrong, not only why the correct option is correct.
Key takeaway
Answer the scenario, not the keyword: identify the specific constraint before choosing the most familiar-sounding option.
Real-world example
How this comes up in practice
A cloud solutions architect for a retail company is evaluating services for a new workload. The correct answer here reflects best practice for the specific scenario described — not a general cloud recommendation. Answer the scenario, not the keyword: identify the specific constraint before choosing the most familiar-sounding option. Cloud exam questions reward reading the constraint carefully: the same technology can be right or wrong depending on the use case.
What to study next
Got this wrong? Here's your next step.
Identify which exam domain this question belongs to, review the core concept, then practise similar questions from the same domain.
Exceptions and File I/O — This question tests Exceptions and File I/O — Read the scenario before looking for a memorised answer..
What is the correct answer to this question?
The correct answer is: 'w' — Option D ('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.
What should I do if I get this PCAP question wrong?
Identify which exam domain this question belongs to, review the core concept, then practise similar questions from the same domain.
What is the key concept behind this question?
Read the scenario before looking for a memorised answer.
About these practice questions
Courseiva creates original exam-style practice questions with explanations and wrong-answer analysis. It does not publish real exam questions, exam dumps, or protected exam content. Learn why practice questions differ from exam dumps →
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 B: Option B ('a') is correct because the append mode opens a file for writing at the end; if the file does not exist, Python creates it automatically. Option D ('w') is correct because write mode truncates the file if it exists, but also creates a new file if it does not exist.
Share a tip, memory trick, or ask about the reasoning behind this question. Do not post real exam questions, leaked content, braindumps, or copyrighted exam material. Comments are moderated and may be removed without notice.
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.
Question Discussion
Share a tip, memory trick, or ask about the reasoning behind this question. Do not post real exam questions, leaked content, braindumps, or copyrighted exam material. Comments are moderated and may be removed without notice.
Sign in to join the discussion.