Try 10 focused PCEP-30-02 questions on Data Collections, with explanations, then continue with IT Mastery.
Open the matching IT Mastery practice page for timed mocks, topic drills, progress tracking, explanations, and full practice.
| Field | Detail |
|---|---|
| Exam route | PCEP-30-02 |
| Topic area | Block 3: Data Collections - Tuples, Dictionaries, Lists, and Strings |
| Blueprint weight | 25% |
| Page purpose | Focused sample questions before returning to mixed practice |
Use this page to isolate Block 3: Data Collections - Tuples, Dictionaries, Lists, and Strings for PCEP-30-02. Work through the 10 questions first, then review the explanations and return to mixed practice in IT Mastery.
| Pass | What to do | What to record |
|---|---|---|
| First attempt | Answer without checking the explanation first. | The fact, rule, calculation, or judgment point that controlled your answer. |
| Review | Read the explanation even when you were correct. | Why the best answer is stronger than the closest distractor. |
| Repair | Repeat only missed or uncertain items after a short break. | The pattern behind misses, not the answer letter. |
| Transfer | Return to mixed practice once the topic feels stable. | Whether the same skill holds up when the topic is no longer obvious. |
Blueprint context: 25% of the practice outline. A focused topic score can overstate readiness if you recognize the pattern too quickly, so use it as repair work before timed mixed sets.
These questions are original IT Mastery practice items aligned to this topic area. They are designed for self-assessment and are not official exam questions.
Topic: Block 3: Data Collections - Tuples, Dictionaries, Lists, and Strings
A student runs this code to build a short tag from a product code. What is the output?
code = "AB12C"
print(code[0] + code[3] + code[4])
Options:
A. A2C
B. B2C
C. AB1
D. A1C
Best answer: A
Explanation: Python strings are zero-indexed. In "AB12C", index 0 is A, index 3 is 2, and index 4 is C, so concatenating those characters prints A2C. The program uses exact character positions, not the first three characters.
Python strings use zero-based indexing, so the first character is at position 0, not 1. For code = "AB12C", the characters are A at index 0, B at 1, 1 at 2, 2 at 3, and C at 4. The print() call joins the characters taken from indexes 0, 3, and 4.
code[0] -> Acode[3] -> 2code[4] -> CPutting them together produces A2C. Wrong choices usually come from counting positions starting at 1 or selecting the wrong middle index.
AB1 uses the first three characters, but the code selects positions 0, 3, and 4.B2C treats indexing as if it starts at 1, but Python strings start at 0.A1C chooses index 2 for the middle character, while the code uses index 3.Topic: Block 3: Data Collections - Tuples, Dictionaries, Lists, and Strings
A beginner wants to remove the "city" entry from a dictionary, but the program raises TypeError: 'dict' object is not callable.
student = {"name": "Mia", "city": "Paris", "age": 19}
del student("city")
print(student)
Assume the key exists. What is the best fix?
Options:
A. Change it to student["city"] = None
B. Change it to del student["Paris"]
C. Change it to del student["city"]
D. Change it to student.remove("city")
Best answer: C
Explanation: The error happens because parentheses after a dictionary name mean a function call. A dictionary key is deleted with del and square brackets around the key, so del student["city"] is the correct fix.
Dictionaries are accessed and changed by key using square brackets. In the shown code, student("city") uses parentheses, so Python treats student like a function call. Because student is a dictionary, that causes TypeError: 'dict' object is not callable.
To remove an existing key with del, write del student["city"]. That tells Python to delete the entry whose key is "city". Assigning None would keep the key, and using a list method like remove() does not work on dictionaries.
The key takeaway is that dictionary deletion uses a key in brackets, not parentheses.
None changes the value but does not remove the key from the dictionary.remove() fails because remove() is a list method, not a dictionary method."Paris" fails because that is a value in the dictionary, not the key being removed.Topic: Block 3: Data Collections - Tuples, Dictionaries, Lists, and Strings
The list words is already defined:
words = ["cat", "dog", "bird"]
A student wants to use a for loop to print the length of each word on a separate line. Which code fragment does this correctly?
Options:
- B. ```python
for word in words:
print(len(word))
- D. ```python
for word in words:
print(len(words))
Best answer: B
Explanation: A for loop over a list gives you each element directly, one at a time. Since each element in words is a string, calling len(word) inside the loop prints each word’s length on its own line. That matches the requirement exactly.
When you write for word in words:, Python assigns each list element to word during the loop. In this case, word becomes "cat", then "dog", then "bird". Because each element is a string, len(word) correctly measures that specific string and prints its length.
A common beginner mistake is confusing the current element with the whole list or with an index. Direct list iteration gives you the value itself, so the loop variable should be treated as one item from the list. That is why using len(word) works, while using the list length or trying to index the list with a string does not. The key idea is to process each list element inside the loop body.
len(words) as the iterable fails because it is a single integer, not a list or other iterable.len(words) inside the loop repeats the total number of items, not each word’s length.words[word] is incorrect because word is a string element, not a numeric list index.Topic: Block 3: Data Collections - Tuples, Dictionaries, Lists, and Strings
A beginner is updating a shopping-list program. The list items may contain any number of products, but it is guaranteed to be non-empty. One part of the program must print the last item, and another part must loop through every valid index. Which TWO code snippets correctly use len() for those tasks?
Options:
A. print(items[len(items)])
B. for i in range(len(items)): print(items[i])
C. for i in range(1, len(items)): print(items[i])
D. for i in range(len(items) + 1): print(items[i])
E. print(items[len(items) - 1])
Correct answers: B and E
Explanation: len(items) returns how many elements are in the list, not the last index. Because Python lists use zero-based indexing, the last item is at len(items) - 1, and range(len(items)) gives all valid indices.
The key idea is the difference between a list’s length and its index positions. If a list has len(items) elements, its valid indices start at 0 and end at len(items) - 1. That is why the last element is accessed with items[len(items) - 1].
For loop logic, range(len(items)) is useful because it generates exactly these valid indices: 0, 1, 2, ... , len(items) - 1. Using those values as items[i] keeps every access in bounds. If you try items[len(items)], you go one position past the end. If you loop to len(items) + 1, the same out-of-range problem appears on the last iteration. The main takeaway is that len() gives a count, so you usually subtract 1 when you need the final index.
items[len(items)] fails because the index equal to the list length is out of range.1 skips index 0, so it does not process every item.range(len(items) + 1) eventually reaches an invalid index equal to the list length.Topic: Block 3: Data Collections - Tuples, Dictionaries, Lists, and Strings
A student wants to replace a list comprehension with a regular for loop without changing the final value of result.
nums = [1, 2, 3, 4]
result = [n * 2 for n in nums if n > 2]
Which TWO code fragments are equivalent replacements?
Options:
- B. ```python
result = []
for n in nums:
result.append(n * 2)
- D. ```python
result = []
for n in nums:
if n * 2 > 2:
result.append(n)
- F. ```python
result = []
for n in nums:
if n > 2:
result.append(n * 2)
Correct answers: C and F
Explanation: A list comprehension with a trailing if both filters and transforms values. An equivalent loop must start with an empty list, check n > 2, and then add n * 2 for each matching item in the original order.
The core idea is that a basic list comprehension can be rewritten as a for loop that builds a list step by step. In this case, the comprehension keeps only values where n > 2 and stores the transformed value n * 2.
result = []
for n in nums:
if n > 2:
result.append(n * 2)
Using result += [n * 2] inside the same if block also works, because it extends the list by one matching transformed item at a time. The wrong choices either ignore the filter, append the original value instead of the doubled one, change the condition, or overwrite the whole list during the loop.
n * 2 for every item ignores the if n > 2 filter.n * 2 > 2 and appends n changes both the condition and the stored value.n after the test keeps the filter but misses the transformation.result = [n * 2] inside the loop replaces earlier results instead of building the full list.Topic: Block 3: Data Collections - Tuples, Dictionaries, Lists, and Strings
A student writes this function to build a 3 x 2 grid. They expect only the first row to change, but the program prints [[0, 7], [0, 7], [0, 7]].
def build_grid(rows, cols):
row = [0] * cols
grid = []
for _ in range(rows):
grid.append(row)
grid[0][1] = 7
return grid
print(build_grid(3, 2))
Which change correctly fixes the function?
Options:
A. Replace grid.append(row) with grid.append([0] * cols).
B. Replace grid.append(row) with grid.append(row * rows).
C. Replace grid[0][1] = 7 with grid[1][0] = 7.
D. Replace return grid with return grid[:].
Best answer: A
Explanation: The problem is that every row in grid refers to the same inner list. Creating a fresh list for each loop iteration fixes the shared-row issue, so only the targeted element in the first row changes.
With nested lists, grid.append(row) adds a reference to the existing row list, not a brand-new row. Because the loop appends that same inner list multiple times, all rows in grid point to the same object. When grid[0][1] = 7 runs, that single shared list changes, so every displayed row appears updated.
grid.append([0] * cols) creates a separate inner list each time.grid[0][1] = 7 affects only the first row.grid[:] copies only the outer list, not the inner lists.The key fix is to stop reusing the same inner list when building the nested list.
Topic: Block 3: Data Collections - Tuples, Dictionaries, Lists, and Strings
A classroom app stores supply counts in a dictionary:
items = {"notebooks": 15, "markers": 6, "folders": 9}
The key "markers" already exists. You need to change its value to 10. Which TWO statements correctly do that?
Options:
A. items["markers"] == 10
B. items.append({"markers": 10})
C. items.get("markers") = 10
D. items["markers"] = 10
E. items.add("markers", 10)
F. items.update({"markers": 10})
Correct answers: D and F
Explanation: A dictionary is mutable, so an existing key’s value can be changed in place. Direct key assignment and the update() method both replace the value for "markers" with 10.
To update a value in a dictionary, you must target the key. In Python, assigning to items["markers"] changes the value stored for that key. The update() method can also change it when you pass another dictionary containing the same key.
items["markers"] = 10
items.update({"markers": 10})
A comparison such as == only tests whether the current value matches 10; it does not modify anything. Methods like append() and add() belong to other collection types, not dictionaries. The main idea is that dictionary mutation happens through key assignment or dictionary methods designed for updates.
== only compares values and leaves the dictionary unchanged.append() fails because dictionaries do not have a list-style append method.add() fails because add() is associated with sets, not dictionaries.get(...) is invalid because you cannot assign to a returned value from a function call.Topic: Block 3: Data Collections - Tuples, Dictionaries, Lists, and Strings
A student is testing a list loop in Python 3. What is the result when this code runs?
items = ["pen", "pencil", "eraser"]
i = 0
while i <= len(items):
print(items[i])
i += 1
Options:
A. It prints pen, pencil, then raises IndexError.
B. It prints pen, pencil, eraser, then raises IndexError.
C. It prints pen, pencil, eraser, and ends normally.
D. It raises IndexError before printing anything.
Best answer: B
Explanation: Lists use zero-based indexing, so a 3-item list has valid indices 0, 1, and 2. Because the loop uses i <= len(items), it prints the three items and then tries items[3], which raises IndexError.
In a list, len(items) gives the number of elements, not the last valid index. Here, len(items) is 3, but the valid indices are 0, 1, and 2. The condition i <= len(items) allows i to become 0, 1, 2, and then 3.
The loop runs like this:
i = 0 prints peni = 1 prints pencili = 2 prints eraseri = 3 tries items[3] and failsPython raises IndexError when direct indexing uses a position outside the list range. A common fix is to use i < len(items) instead.
pencil is wrong because index 2 is valid and prints eraser.i becomes 3.0, 1, and 2 are accessed successfully first.Topic: Block 3: Data Collections - Tuples, Dictionaries, Lists, and Strings
What is the output of the following code?
data = [4, 2]
backup = data
data = sorted(data)
backup.append(6)
print(data)
print(backup)
Options:
A. [2, 4] then [4, 2, 6]
B. [2, 4] then [2, 4, 6]
C. [4, 2] then [4, 2, 6]
D. [2, 4, 6] then [2, 4, 6]
Best answer: A
Explanation: backup = data makes both names refer to the same original list at first. Then sorted(data) returns a new sorted list for data, while backup.append(6) changes the old list, so the two printed lists are different.
This tests the difference between creating a new list and mutating an existing one. backup = data means both variables initially reference the same list object, [4, 2]. But data = sorted(data) does not sort that list in place; it creates a new list [2, 4] and assigns that new list to data.
data starts as [4, 2]backup points to the same original listsorted(data) makes a new list [2, 4]backup.append(6) mutates the original list to [4, 2, 6]So print(data) shows the new sorted list, while print(backup) shows the mutated original list. The key takeaway is that sorted() creates a new list, but append() changes an existing one.
[2, 4, 6] assumes sorted() changed the original list in place.[4, 2] first ignores that data was reassigned to the new sorted list.[2, 4, 6] for backup assumes backup automatically followed data after reassignment.Topic: Block 3: Data Collections - Tuples, Dictionaries, Lists, and Strings
A student wants to remove the city key from a dictionary so the program prints only the remaining entries.
profile = {"name": "Ana", "age": 21, "city": "Lima"}
profile.remove("city")
print(profile)
Which replacement for profile.remove("city") is the best fix?
Options:
A. profile.delete("city")
B. profile["city"] = None
C. del profile["Lima"]
D. del profile["city"]
Best answer: D
Explanation: To remove an entry from a dictionary, Python needs the key, not the value. del profile["city"] correctly deletes the whole "city": "Lima" pair from the dictionary.
A dictionary stores data as key-value pairs, so removing one entry means removing it by its key. In this program, city is the key and Lima is the value. The statement del profile["city"] deletes that entire pair from the dictionary.
Setting profile["city"] to None does not remove the key; it only changes the value. Using Lima with del is incorrect because Lima is not a key in the dictionary. Also, dictionaries do not have a delete() method. The main idea is to use the correct key-based deletion syntax for dictionaries.
None changes the stored value but leaves the city key in the dictionary.Lima is a value, not a dictionary key..delete() fails because delete() is not a valid dictionary method in Python.Use the PCEP-30-02 Practice Test page for the full IT Mastery route, mixed-topic practice, timed mock exams, explanations, and web/mobile app access.
Try PCEP-30-02 on Web View PCEP-30-02 Practice Test
Read the PCEP-30-02 Cheat Sheet on Tech Exam Lexicon, then return to IT Mastery for timed practice.