These eight original questions get harder as you go. Write a trace table for each one before you open the answer, and assume arrays start at index 0.
They test the four skills in advanced programming practice. Use the timed original practice session builder if you want a repeatable session length.
Questions
Question 1. The array nums = [4, 8, 15, 16, 23, 42]. What is nums[3], and what is the last valid index?
Answer
Indexes count from 0, so nums[0] = 4, nums[1] = 8, nums[2] = 15 and nums[3] = 16. The array has 6 elements, so the last valid index is 5, which holds 42.
Question 2. Trace this code for scores = [12, 7, 15, 9]. What is printed?
count = 0
FOR i = 0 TO 3
IF scores[i] > 10 THEN
count = count + 1
ENDIF
NEXT i
PRINT count
Answer
| i | scores[i] | > 10? | count |
|---|---|---|---|
| 0 | 12 | Yes | 1 |
| 1 | 7 | No | 1 |
| 2 | 15 | Yes | 2 |
| 3 | 9 | No | 2 |
The program prints 2, the number of scores above 10.
Question 3. Each item in a list of records has name and price. The prices are Pen 2, Book 18, Ruler 3. What does this print?
FOR i = 0 TO 2
IF item[i].price < 5 THEN
PRINT item[i].name
ENDIF
NEXT i
Answer
The condition is true for Pen (2) and Ruler (3), and false for Book (18). The output is Pen then Ruler, in index order.
Question 4. Use a linear search to find 15 in [9, 3, 15, 8, 15]. Which index is reported, and how many comparisons are made?
Answer
The search compares 9 (no), 3 (no), 15 (yes) and stops. It reports index 2 after 3 comparisons. The second 15 at index 4 is never reached.
Question 5. Trace a binary search for 8 in the sorted array [2, 5, 8, 11, 14, 17, 20], indexes 0 to 6. Show low, high and mid at each step.
Answer
| Step | low | high | mid | data[mid] | Decision |
|---|---|---|---|---|---|
| 1 | 0 | 6 | 3 | 11 | 11 > 8, so high = 2 |
| 2 | 0 | 2 | 1 | 5 | 5 < 8, so low = 2 |
| 3 | 2 | 2 | 2 | 8 | Found at index 2 |
Three comparisons find the value.
Question 6. Show the array after each pass when bubble sort arranges [3, 1, 2] in ascending order. How many swaps are made?
Answer
Pass 1: compare 3 and 1, swap, giving 1, 3, 2. Compare 3 and 2, swap, giving 1, 2, 3.
Pass 2: compare 1 and 2, no swap. Nothing changed, so the list is sorted.
After pass 1 the array is 1, 2, 3, and the total is 2 swaps.
Question 7. Classify each fault as syntax, runtime or logic.
- (a) A loop meant to add the numbers 1 to 5 prints 10, because it starts at 0 and stops before 5. The expected total is 15.
- (b) A line reads PRNT total.
- (c) The program divides by a count that is 0 when the list is empty.
Answer
- (a) Logic error. The code runs and gives a wrong result, because it uses the wrong bounds.
- (b) Syntax error. PRNT is not valid, so the program will not run.
- (c) Runtime error. The code is valid but dividing by zero stops the program for that input.
Question 8. A program accepts a password length of 8 to 12 characters inclusive and prints “OK” if it is valid, “Reject” otherwise. Write a test table with expected outputs that includes boundaries.
Answer
| Length | Type | Expected output |
|---|---|---|
| 10 | Normal | OK |
| 8 | Boundary | OK |
| 12 | Boundary | OK |
| 7 | Boundary | Reject |
| 13 | Boundary | Reject |
| 0 | Invalid | Reject |
The four boundary rows would catch a wrong > or >= in the condition. Expected outputs were worked out from the rule, not from the code.
If you got these wrong
- Index answers off by one: return to working with arrays and structured data.
- Traces that lose a value: return to tracing searching and sorting algorithms.
- Mixed-up error types: return to debugging syntax, logic and runtime errors.
- Missing boundary rows: return to creating a test table.
If you want a teacher to go through your traces with you, see online one-to-one Computer Science tuition.