PCAP · domain
Strings
Practise Certified Associate Python Programmer PCAP Strings practice questions — original exam-style scenarios with answer choices, explanations, and analysis of common mistakes.
Focused practice
Practice Strings questions
Scored sessions drawing only from this domain — pick a length below.
Start 20-question practice test →What this domain covers
What to know about Strings
Strings questions test whether you can apply the concept in context, not just recognise a definition.
How the topic appears in realistic exam-style scenarios.
Which detail in the question changes the correct answer.
How to eliminate plausible but wrong options.
How to connect the question back to the wider exam objective.
Watch out for
Common Strings exam traps
- ▸Answering from memory before reading the full scenario.
- ▸Missing a constraint such as cost, availability, security, scope or command context.
- ▸Choosing a broad answer when the question asks for the most specific fix.
- ▸Ignoring why the wrong options are tempting.
Question index
All Strings questions (50)
Click any question to see the full explanation, or start a practice session above.
Refer to the exhibit. What happens when the code is executed?
Medium2A QA engineer needs to verify that a user input string contains at least one uppercase letter, one lowercase letter, and one digit. Which regex pattern can be used with re.search() to achieve this?
Hard3Which THREE of the following are immutable types in Python?
Medium4Drag and drop the steps to handle an exception in Python using try-except-finally into the correct order.
Medium5Which THREE of the following are valid ways to create a string in Python?
Hard6A log processing script receives a multiline string log. The script needs to check if the string ends with the substring 'ERROR'. Which method should be used?
Medium7You are a developer at a company that processes customer feedback. Each feedback entry is stored as a string containing a rating (1-5) followed by a colon and then the comment. For example: '4: Great service'. You need to extract only the comments from feedback that have a rating of 4 or 5. You have a list of feedback strings. Which code snippet correctly implements this?
Hard8A data pipeline processes CSV lines that may contain quoted fields with commas inside double quotes. For example: 'John, "Doe, Jr.", 35'. The team needs to split such a line correctly. Which approach is best?
Hard9Which of the following is the BEST practice for building a large string by concatenating many smaller strings in Python?
Easy10Refer to the exhibit. Which of the following fixes the error?
Medium11A data analyst is cleaning a CSV file. They have a string variable containing a row of data: 'John,Doe,30,New York'. They need to extract the last name 'Doe' using string methods. The analyst writes: name = row.split(',')[1]. However, they are concerned about performance because the file contains millions of rows. They want to use a more efficient method that extracts the substring without creating a full list. Which approach should the analyst use?
Medium12A developer writes code to display a floating-point number with exactly two decimal places. Which f-string expression is correct for value = 3.14159?
Medium13A function receives a file path like '/home/user/docs/file.txt' and needs to return the path without the file extension, e.g., '/home/user/docs/file'. Which code reliably removes only the last dot extension, even if the directory names contain dots?
Easy14You are developing a high-performance logging module that must handle thousands of log entries per second. Each entry is built by concatenating a timestamp, level, and message. Currently, your code uses a loop that repeatedly appends to a string using the += operator. This results in high memory usage and sluggish performance because each concatenation creates a new string object. The module must run on systems with limited memory and cannot rely on external libraries. Which course of action would best resolve the performance issue while maintaining readability and standard library compliance?
Hard15Refer to the exhibit. What is printed?
Medium16A Python script reads a file containing text with non-ASCII characters like 'é' and 'ü'. The script must encode the string as UTF-8 then decode it back. Which of the following correctly handles this without error?
Hard17A team is using f-strings to format a report. They have a variable `value = 0.123456789` and want to display it with exactly 3 significant digits. They write `f"{value:.3g}"`. The output is '0.123'. They expected '0.123'. Is the output correct? If not, what change would produce '0.123'?
Hard18A network engineer processes a configuration file containing MAC addresses in the format 'aa:bb:cc:dd:ee:ff'. They need to convert each MAC address into a 6-byte bytes object for use in packet crafting. The current code is: mac_bytes = bytes([int(x, 16) for x in mac_str.split(':')]). This works correctly, but they need to process thousands of MAC addresses and want to optimize performance. They also need to handle invalid MAC addresses (e.g., non-hex characters) without crashing. Which of the following approaches is the most efficient and robust?
Medium19A developer generates a report where numbers must be right-aligned in a 10-character column using f-strings: f'{value:>10}'. However, some values may be None, causing a TypeError. Which is the most robust way to handle None values without affecting other falsy values like 0?
Medium20What is the result of 'abcdef'[::-2]?
Hard21A developer wants to convert a string 'Python' to all uppercase letters. Which string method should be used?
Easy22A cloud infrastructure engineer is developing a Python script to parse large configuration files from a fleet of servers. Each file can be up to 500 MB. The script reads the file line by line using a file object, strips comment lines (those starting with '#'), and accumulates only the configuration directives into a single string for further processing. The current code is: ```python result = '' with open('config.cfg') as f: for line in f: if not line.startswith('#'): result += line.strip() ``` After processing just a few hundred lines of a large file, the script becomes extremely slow and consumes an excessive amount of memory. The engineer identifies that string concatenation using `+=` is inefficient because strings are immutable, causing repeated memory reallocation. Which approach should the engineer implement to resolve the performance issue without changing the final output?
Hard23Match each exception to its cause.
Medium24A developer needs to parse a log file where each line contains a timestamp followed by a message. The timestamp format is 'YYYY-MM-DD HH:MM:SS'. Which string method is most appropriate to split the timestamp from the message?
Medium25A developer needs to combine a list of 10,000 strings into a single string. Which approach is most efficient in terms of memory and performance?
Medium26Which method returns the lowest index where a specified substring is found, or -1 if not found?
Medium27A developer needs to check if a string contains only alphanumeric characters. Which string method should be used?
Easy28A developer is tasked with validating user input that must be a 10-digit phone number. The input may contain spaces, dashes, and parentheses. Which approach best ensures the input contains exactly 10 digits?
Hard29You are a data analyst working with a dataset of customer reviews. Each review is stored as a string in a list. You need to count how many reviews contain the word 'excellent' (case-insensitive). However, the word might appear as 'Excellent', 'EXCELLENT', or even with punctuation like 'excellent!'. The current code uses 'excellent' in review.lower(), but this fails if 'excellent' is part of another word like 'unexcellent'. You need to ensure that only the whole word 'excellent' is counted. Which code modification will correctly count whole word occurrences?
Medium30A developer needs to count the number of occurrences of the substring 'is' in the string 'This is a test. Is this a test?'. Which code correctly performs the count?
Medium31Refer to the exhibit. What is the output?
Hard32Which THREE are valid ways to create a multiline string in Python?
Medium33Given s = 'a1b2c3', which TWO of the following expressions return the string '123'?
Hard34Which TWO of the following can be used to remove leading whitespace (spaces, tabs, newlines) from a string? (Choose exactly 2 correct answers.)
Medium35You are a developer for an e-commerce platform. The system receives product descriptions from suppliers in various formats. One supplier sends descriptions with inconsistent capitalization, extra whitespace, and occasional leading/trailing punctuation. Your task is to write a function that normalizes these descriptions: convert to lowercase, remove leading/trailing whitespace and punctuation (.,!?;:), and replace multiple spaces with a single space. The function should return the cleaned string. Which implementation correctly performs all these steps?
Medium36A logging module receives a message that may contain sensitive data. To comply with data privacy, all digits in the message should be replaced with 'X' before logging. Which approach correctly achieves this?
Medium37Which of the following demonstrates that strings are immutable?
Medium38Which THREE methods return a boolean value?
Hard39A developer is working on a logging system where dynamic values are inserted into a template string. The template is 'User %s logged in at %s'. The developer has the username and timestamp as separate variables. Which approach is most Pythonic (PEP 498) and recommended for new code?
Medium40A developer writes: s = 'abc'; s[0] = 'x'. What happens?
Hard41Which THREE of the following escape sequences are valid in a Python string and represent a single character? (Select exactly three.)
Hard42Consider the following code snippet: s = 'abcdefgh'; result = s[7:3:-2]; print(result). What is the output?
Hard43A developer needs to extract the file extension from a filename like 'document.pdf'. Which expression returns 'pdf'?
Hard44What is the result of the expression '12345'[:10]?
Easy45Consider the following code: print('"age": 30,')
Hard46Which TWO of the following expressions yield the substring 'Py' from the string s = 'Python'?
Hard47Which TWO of the following string methods modify the string in place? (Note: Python strings are immutable.)
Medium48A developer wants to check if a string ends with a specific suffix. Which method should be used?
Easy49A developer tries to modify a string: s = 'hello'; s[0] = 'H'. What happens when this code runs?
Medium50A programmer writes a function that expects a string and returns it reversed. Which code snippet correctly reverses the string 'stressed' to 'desserts'?
EasyOther domains
All PCAP exam domains
Frequently asked questions
- What does the Strings domain cover on the PCAP exam?
- Strings questions test whether you can apply the concept in context, not just recognise a definition.
- How many questions are in this domain?
- This page lists all 50 Strings questions in the PCAP question bank. The actual exam draws from this domain proportionally to its weighting in the official exam blueprint.
- What is the best way to practise this domain?
- Start with a short focused session (10 questions) to identify gaps, then work through explanations. Repeat with a longer session once the weak areas feel solid.
- Can I practise only Strings questions?
- Yes — the session launcher on this page filters questions to this domain only. Choose any session length for inline explanations and scoring.