PCEP Control Flow, Loops, Lists and Logic Practice Question
A developer needs to iterate over a list of network interfaces and print only the names that start with 'eth'. Which code should be used?
⚠ Common exam trap
Python Institute often tests the distinction between `in` (substring membership) and `startswith()` (prefix matching), leading candidates to choose Option D because they think 'eth' must appear at the start, but `in` would also match names like 'xeth0'.
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
✓
for iface in interfaces: if iface.startswith('eth'): print(iface)
The `startswith()` string method is the most direct and readable way to check if each interface name begins with the substring 'eth'. This approach avoids unnecessary slicing or substring membership checks, making the intent clear and the code efficient.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✗
for i in range(len(interfaces)): if 'eth' in interfaces[i]: print(interfaces[i])
Why it's wrong here
Indexing with `range(len(...))` still applies the `in` containment test, matching 'eth' anywhere in the name rather than at position zero, so non-prefixed interfaces print. It is tempting because explicit indexing is familiar from other languages; `startswith('eth')` or slicing `[:3] == 'eth'` gives the required prefix check.
- ✗
for iface in interfaces: if iface[:3] is 'eth': print(iface)
Why it's wrong here
The identity operator `is` compares object identity, not string value, so `iface[:3] is 'eth'` fails for any slice not interned; `==` is required for value equality. It is tempting because slicing the first three characters correctly isolates the prefix, and the construct would work only where CPython happens to intern that exact literal.
- ✓
for iface in interfaces: if iface.startswith('eth'): print(iface)
Why this is correct
Iterating directly over `interfaces` yields each element, and `str.startswith('eth')` performs the required prefix test, printing only matching names. This satisfies the stem's constraint of filtering by the 'eth' prefix without indexing or slicing, keeping the loop concise and readable.
- ✗
for iface in interfaces: if 'eth' in iface: print(iface)
Why it's wrong here
The `in` operator tests substring containment anywhere in the string, so 'eth0' matches but so would 'wlan_eth' or 'myeth', printing interfaces that do not start with 'eth'. It is tempting because it is concise and correct for membership tests; `startswith('eth')` is the precise method for prefix matching.
Go deeper
Related to this question
About these practice questions
One of 482 original PCEP practice questions on Courseiva, each with a full explanation and wrong-answer analysis — not exam dumps or protected exam content. 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.