Courseiva
Strings →hardMultiple Choice

PCAP Strings Practice Question

Exhibit

Refer to the exhibit.

Configuration block:
```python
import re
pattern = r'\b[A-Z][a-z]*\b'
text = "Alice and Bob are friends."
matches = re.findall(pattern, text)
```

What is the value of matches?

⚠ Common exam trap

Python Institute often tests the misconception that `[a-z]*` matches any sequence of letters, but the pattern requires the first character to be uppercase, causing candidates to incorrectly include all words or miss the second capitalized word.

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

✓

['Alice', 'Bob']

The `re.findall(r'[A-Z][a-z]*', 'Alice and Bob are friends')` call matches all sequences starting with an uppercase letter followed by zero or more lowercase letters. This yields 'Alice' and 'Bob', as they are the only words beginning with a capital letter. The result is a list of those two strings.

Answer analysis

Option-by-option breakdown

For each option: why learners choose it and why it is or isn't the right answer here.

  • ✓

    ['Alice', 'Bob']

    Why this is correct

    This is correct because the regex pattern likely uses a character class such as [A-Z] to require an initial uppercase letter followed by word characters, and `re.findall()` returns every non-overlapping match in the string. Both 'Alice' and 'Bob' begin with uppercase letters and consist entirely of alphabetic characters, so they are the complete set of matching substrings. No other word in the input starts with an uppercase letter, so no additional tokens qualify.

  • ✗

    ['Alice']

    Why it's wrong here

    This is wrong because it lists only 'Alice', thereby omitting 'Bob' even though 'Bob' also starts with an uppercase letter and fully satisfies the same matching criteria. Such a result would occur if the code used `re.search()` or `re.match()` instead of `re.findall()`, since those functions return at most the first match rather than all matches. Since the question asks for the value of all matches, all qualifying substrings must be included.

  • ✗

    ['Alice', 'and', 'Bob', 'are', 'friends']

    Why it's wrong here

    This is wrong because it includes 'and', 'are', and 'friends', none of which begin with an uppercase letter. A pattern that requires an uppercase starting character, for example `[A-Z][a-z]*`, will not match those lowercase-initial words, so they should be excluded from the list of matches. This result would be expected only if the pattern were case-insensitive or if the string were simply split into tokens without applying the uppercase filter.

  • ✗

    ['Alice', 'Bob', 'friends']

    Why it's wrong here

    This is wrong because, although 'Alice' and 'Bob' are valid matches, 'friends' is not: its first character is a lowercase 'f', so it fails the uppercase-start requirement that the pattern enforces. Consequently, the result set includes one non-matching token while still capturing the two correct matches. This is over-inclusive and does not represent the value returned by `re.findall()` with a case-sensitive pattern.

About these practice questions

One of 421 original PCAP 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 →

How Courseiva writes practice questions · Editorial policy

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.