Courseiva

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.

50 questions7 easy24 medium19 hard

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.

1

Refer to the exhibit. What happens when the code is executed?

Medium
2

A 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?

Hard
3

Which THREE of the following are immutable types in Python?

Medium
4

Drag and drop the steps to handle an exception in Python using try-except-finally into the correct order.

Medium
5

Which THREE of the following are valid ways to create a string in Python?

Hard
6

A 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?

Medium
7

You 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?

Hard
8

A 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?

Hard
9

Which of the following is the BEST practice for building a large string by concatenating many smaller strings in Python?

Easy
10

Refer to the exhibit. Which of the following fixes the error?

Medium
11

A 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?

Medium
12

A developer writes code to display a floating-point number with exactly two decimal places. Which f-string expression is correct for value = 3.14159?

Medium
13

A 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?

Easy
14

You 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?

Hard
15

Refer to the exhibit. What is printed?

Medium
16

A 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?

Hard
17

A 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'?

Hard
18

A 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?

Medium
19

A 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?

Medium
20

What is the result of 'abcdef'[::-2]?

Hard
21

A developer wants to convert a string 'Python' to all uppercase letters. Which string method should be used?

Easy
22

A 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?

Hard
23

Match each exception to its cause.

Medium
24

A 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?

Medium
25

A 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?

Medium
26

Which method returns the lowest index where a specified substring is found, or -1 if not found?

Medium
27

A developer needs to check if a string contains only alphanumeric characters. Which string method should be used?

Easy
28

A 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?

Hard
29

You 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?

Medium
30

A 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?

Medium
31

Refer to the exhibit. What is the output?

Hard
32

Which THREE are valid ways to create a multiline string in Python?

Medium
33

Given s = 'a1b2c3', which TWO of the following expressions return the string '123'?

Hard
34

Which TWO of the following can be used to remove leading whitespace (spaces, tabs, newlines) from a string? (Choose exactly 2 correct answers.)

Medium
35

You 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?

Medium
36

A 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?

Medium
37

Which of the following demonstrates that strings are immutable?

Medium
38

Which THREE methods return a boolean value?

Hard
39

A 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?

Medium
40

A developer writes: s = 'abc'; s[0] = 'x'. What happens?

Hard
41

Which THREE of the following escape sequences are valid in a Python string and represent a single character? (Select exactly three.)

Hard
42

Consider the following code snippet: s = 'abcdefgh'; result = s[7:3:-2]; print(result). What is the output?

Hard
43

A developer needs to extract the file extension from a filename like 'document.pdf'. Which expression returns 'pdf'?

Hard
44

What is the result of the expression '12345'[:10]?

Easy
45

Consider the following code: print('"age": 30,')

Hard
46

Which TWO of the following expressions yield the substring 'Py' from the string s = 'Python'?

Hard
47

Which TWO of the following string methods modify the string in place? (Note: Python strings are immutable.)

Medium
48

A developer wants to check if a string ends with a specific suffix. Which method should be used?

Easy
49

A developer tries to modify a string: s = 'hello'; s[0] = 'H'. What happens when this code runs?

Medium
50

A programmer writes a function that expects a string and returns it reversed. Which code snippet correctly reverses the string 'stressed' to 'desserts'?

Easy

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.
Certified Associate Python Programmer PCAP Strings Practice Questions