Loading....
|
Press & Hold to Drag Around |
|||
|
Click Here to Close |
|||
Question 1 Report
A home security system uses three sensors:
DoorOpen - TRUE if any door is openWindowOpen - TRUE if any window is openAlarmSet - TRUE if the alarm is armedThe alarm sounds when: the alarm is set AND (a door is open OR a window is open).
(a) Write the Boolean expression for the alarm sounding. [1]
(b) Complete the truth table. [4]
| AlarmSet | DoorOpen | WindowOpen | Alarm sounds? |
|---|---|---|---|
| FALSE | FALSE | FALSE | |
| FALSE | TRUE | FALSE | |
| TRUE | FALSE | FALSE | |
| TRUE | TRUE | FALSE | |
| TRUE | FALSE | TRUE | |
| TRUE | TRUE | TRUE |
(c) Write pseudocode that implements this security system logic and outputs appropriate messages. [2]
(d) Write the Boolean expression for the alarm NOT sounding (using De Morgan's law). [1]
(a) The Boolean expression for the alarm sounding is:
AlarmSounds = AlarmSet AND (DoorOpen OR WindowOpen) [1]
The alarm requires the system to be armed AND at least one entry point to be open.
(b)
| AlarmSet | DoorOpen | WindowOpen | DoorOpen OR WindowOpen | Alarm sounds? |
|---|---|---|---|---|
| FALSE | FALSE | FALSE | FALSE | FALSE |
| FALSE | TRUE | FALSE | TRUE | FALSE |
| TRUE | FALSE | FALSE | FALSE | FALSE |
| TRUE | TRUE | FALSE | TRUE | TRUE |
| TRUE | FALSE | TRUE | TRUE | TRUE |
| TRUE | TRUE | TRUE | TRUE | TRUE |
When AlarmSet is FALSE (rows 1-2), the AND makes the result FALSE regardless of door/window state. [1] When AlarmSet is TRUE but both sensors are FALSE (row 3), the OR evaluates to FALSE. [1] When AlarmSet is TRUE and at least one sensor is TRUE (rows 4-6), the alarm sounds. [1] [1]
(c)
IF AlarmSet AND (DoorOpen OR WindowOpen) THEN
OUTPUT "ALARM: Intruder detected!"
IF DoorOpen THEN
OUTPUT "Door breach detected"
ENDIF
IF WindowOpen THEN
OUTPUT "Window breach detected"
ENDIF
ELSE
OUTPUT "System OK"
ENDIFThe Boolean expression is the IF condition. [1] Nested IFs identify which sensor(s) triggered. [1]
(d) Applying De Morgan's law:
NOT AlarmSet OR (NOT DoorOpen AND NOT WindowOpen) [1]
NOT (A AND B) = (NOT A) OR (NOT B), and NOT (C OR D) = (NOT C) AND (NOT D).
(a) The Boolean expression for the alarm sounding is:
AlarmSounds = AlarmSet AND (DoorOpen OR WindowOpen) [1]
The alarm requires the system to be armed AND at least one entry point to be open.
(b)
| AlarmSet | DoorOpen | WindowOpen | DoorOpen OR WindowOpen | Alarm sounds? |
|---|---|---|---|---|
| FALSE | FALSE | FALSE | FALSE | FALSE |
| FALSE | TRUE | FALSE | TRUE | FALSE |
| TRUE | FALSE | FALSE | FALSE | FALSE |
| TRUE | TRUE | FALSE | TRUE | TRUE |
| TRUE | FALSE | TRUE | TRUE | TRUE |
| TRUE | TRUE | TRUE | TRUE | TRUE |
When AlarmSet is FALSE (rows 1-2), the AND makes the result FALSE regardless of door/window state. [1] When AlarmSet is TRUE but both sensors are FALSE (row 3), the OR evaluates to FALSE. [1] When AlarmSet is TRUE and at least one sensor is TRUE (rows 4-6), the alarm sounds. [1] [1]
(c)
IF AlarmSet AND (DoorOpen OR WindowOpen) THEN
OUTPUT "ALARM: Intruder detected!"
IF DoorOpen THEN
OUTPUT "Door breach detected"
ENDIF
IF WindowOpen THEN
OUTPUT "Window breach detected"
ENDIF
ELSE
OUTPUT "System OK"
ENDIFThe Boolean expression is the IF condition. [1] Nested IFs identify which sensor(s) triggered. [1]
(d) Applying De Morgan's law:
NOT AlarmSet OR (NOT DoorOpen AND NOT WindowOpen) [1]
NOT (A AND B) = (NOT A) OR (NOT B), and NOT (C OR D) = (NOT C) AND (NOT D).
Question 2 Report
A program needs to repeatedly accept temperature readings until a valid reading is entered. A valid temperature is between -50 and 60 degrees Celsius.
(a) Write pseudocode using a WHILE loop to keep asking for input until a valid temperature is entered. Output an error message for invalid input. [4]
(b) Rewrite your solution using a REPEAT...UNTIL loop. [2]
(c) Explain one difference between the WHILE loop and REPEAT...UNTIL loop versions. [2]
(a) The WHILE loop version uses a flag variable to control repetition. The flag is initially FALSE, and the loop body prompts for input, validates it, and sets the flag to TRUE when the temperature is in range: [4]
DECLARE Temperature : REAL
DECLARE Valid : BOOLEAN
Valid ← FALSE
WHILE Valid = FALSE DO
OUTPUT "Enter temperature (-50 to 60): "
INPUT Temperature
IF Temperature >= -50 AND Temperature <= 60 THEN
Valid ← TRUE
ELSE
OUTPUT "Invalid temperature. Try again."
ENDIF
ENDWHILE
OUTPUT "Temperature recorded: ", Temperature[1 mark for WHILE loop with correct condition, 1 mark for input inside loop, 1 mark for validation check, 1 mark for error message and flag update]
(b) The REPEAT...UNTIL version tests the condition after the body, guaranteeing the prompt runs at least once: [2]
REPEAT
OUTPUT "Enter temperature (-50 to 60): "
INPUT Temperature
IF Temperature < -50 OR Temperature > 60 THEN
OUTPUT "Invalid temperature. Try again."
ENDIF
UNTIL Temperature >= -50 AND Temperature <= 60
OUTPUT "Temperature recorded: ", Temperature[1 mark for REPEAT...UNTIL structure, 1 mark for correct exit condition]
(c) The WHILE loop tests its condition before the loop body executes, so if the condition is already FALSE at the start, the body never runs at all. [1] The REPEAT...UNTIL loop always executes the body at least once before testing the condition. For input validation this is more natural because the user must enter a value before it can be checked. [1]
(a) The WHILE loop version uses a flag variable to control repetition. The flag is initially FALSE, and the loop body prompts for input, validates it, and sets the flag to TRUE when the temperature is in range: [4]
DECLARE Temperature : REAL
DECLARE Valid : BOOLEAN
Valid ← FALSE
WHILE Valid = FALSE DO
OUTPUT "Enter temperature (-50 to 60): "
INPUT Temperature
IF Temperature >= -50 AND Temperature <= 60 THEN
Valid ← TRUE
ELSE
OUTPUT "Invalid temperature. Try again."
ENDIF
ENDWHILE
OUTPUT "Temperature recorded: ", Temperature[1 mark for WHILE loop with correct condition, 1 mark for input inside loop, 1 mark for validation check, 1 mark for error message and flag update]
(b) The REPEAT...UNTIL version tests the condition after the body, guaranteeing the prompt runs at least once: [2]
REPEAT
OUTPUT "Enter temperature (-50 to 60): "
INPUT Temperature
IF Temperature < -50 OR Temperature > 60 THEN
OUTPUT "Invalid temperature. Try again."
ENDIF
UNTIL Temperature >= -50 AND Temperature <= 60
OUTPUT "Temperature recorded: ", Temperature[1 mark for REPEAT...UNTIL structure, 1 mark for correct exit condition]
(c) The WHILE loop tests its condition before the loop body executes, so if the condition is already FALSE at the start, the body never runs at all. [1] The REPEAT...UNTIL loop always executes the body at least once before testing the condition. For input validation this is more natural because the user must enter a value before it can be checked. [1]
Question 3 Report
Study the following pseudocode that processes an array of integers.
DECLARE Nums : ARRAY[1:6] OF INTEGER
DECLARE Temp : INTEGER
DECLARE i : INTEGER
DECLARE Swapped : BOOLEAN
Nums[1] ← 8
Nums[2] ← 3
Nums[3] ← 5
Nums[4] ← 1
Nums[5] ← 9
Nums[6] ← 2
REPEAT
Swapped ← FALSE
FOR i ← 1 TO 5
IF Nums[i] > Nums[i + 1] THEN
Temp ← Nums[i]
Nums[i] ← Nums[i + 1]
Nums[i + 1] ← Temp
Swapped ← TRUE
ENDIF
NEXT i
UNTIL Swapped = FALSE(a) Complete the table to show the contents of the array after the first complete pass of the outer loop. [4]
| Nums[1] | Nums[2] | Nums[3] | Nums[4] | Nums[5] | Nums[6] |
|---|---|---|---|---|---|
(b) Name the sorting algorithm being used. [1]
(c) State the purpose of the variable Temp. [1]
(d) Explain why the variable Swapped is used. [2]
(e) State the minimum and maximum number of complete passes needed to sort an array of 6 elements using this algorithm. [2]
(a) The algorithm is a bubble sort. Starting with [8, 3, 5, 1, 9, 2], the inner FOR loop compares each adjacent pair from position 1 to 5:
| Comparison | Elements | Swap? | Array after |
|---|---|---|---|
| i=1: Nums[1] vs Nums[2] | 8 vs 3 | Yes | [3, 8, 5, 1, 9, 2] |
| i=2: Nums[2] vs Nums[3] | 8 vs 5 | Yes | [3, 5, 8, 1, 9, 2] |
| i=3: Nums[3] vs Nums[4] | 8 vs 1 | Yes | [3, 5, 1, 8, 9, 2] |
| i=4: Nums[4] vs Nums[5] | 8 vs 9 | No | [3, 5, 1, 8, 9, 2] |
| i=5: Nums[5] vs Nums[6] | 9 vs 2 | Yes | [3, 5, 1, 8, 2, 9] |
After the first complete pass, the array is: [3, 5, 1, 8, 2, 9]. The largest value (9) has "bubbled" to its correct final position. [4]
(b) The sorting algorithm is a bubble sort. It works by repeatedly comparing adjacent elements and swapping them if they are in the wrong order. [1]
(c) Temp is a temporary variable used to hold one value during the swap of two adjacent elements. Without it, one value would be overwritten and lost when the other is copied into its place. The three-step swap pattern (Temp <- A, A <- B, B <- Temp) preserves both values. [1]
(d) Swapped is a Boolean flag that tracks whether any swaps occurred during a complete pass through the array. [1] If no swaps were made during an entire pass (Swapped remains FALSE), the array is already sorted and the REPEAT loop terminates early. Without this flag, the algorithm would continue making unnecessary passes even after the array is fully sorted. [1] [2]
(e) Minimum: 1 pass. If the array is already sorted, one pass through the inner loop produces no swaps, so Swapped stays FALSE and the algorithm terminates after just that single pass. [1]
Maximum: 5 passes (n - 1 = 6 - 1 = 5). In the worst case (reverse-sorted array), each pass moves only one element to its correct position, requiring 5 passes to sort all 6 elements. [1] [2]
(a) The algorithm is a bubble sort. Starting with [8, 3, 5, 1, 9, 2], the inner FOR loop compares each adjacent pair from position 1 to 5:
| Comparison | Elements | Swap? | Array after |
|---|---|---|---|
| i=1: Nums[1] vs Nums[2] | 8 vs 3 | Yes | [3, 8, 5, 1, 9, 2] |
| i=2: Nums[2] vs Nums[3] | 8 vs 5 | Yes | [3, 5, 8, 1, 9, 2] |
| i=3: Nums[3] vs Nums[4] | 8 vs 1 | Yes | [3, 5, 1, 8, 9, 2] |
| i=4: Nums[4] vs Nums[5] | 8 vs 9 | No | [3, 5, 1, 8, 9, 2] |
| i=5: Nums[5] vs Nums[6] | 9 vs 2 | Yes | [3, 5, 1, 8, 2, 9] |
After the first complete pass, the array is: [3, 5, 1, 8, 2, 9]. The largest value (9) has "bubbled" to its correct final position. [4]
(b) The sorting algorithm is a bubble sort. It works by repeatedly comparing adjacent elements and swapping them if they are in the wrong order. [1]
(c) Temp is a temporary variable used to hold one value during the swap of two adjacent elements. Without it, one value would be overwritten and lost when the other is copied into its place. The three-step swap pattern (Temp <- A, A <- B, B <- Temp) preserves both values. [1]
(d) Swapped is a Boolean flag that tracks whether any swaps occurred during a complete pass through the array. [1] If no swaps were made during an entire pass (Swapped remains FALSE), the array is already sorted and the REPEAT loop terminates early. Without this flag, the algorithm would continue making unnecessary passes even after the array is fully sorted. [1] [2]
(e) Minimum: 1 pass. If the array is already sorted, one pass through the inner loop produces no swaps, so Swapped stays FALSE and the algorithm terminates after just that single pass. [1]
Maximum: 5 passes (n - 1 = 6 - 1 = 5). In the worst case (reverse-sorted array), each pass moves only one element to its correct position, requiring 5 passes to sort all 6 elements. [1] [2]
Question 4 Report
A science class records plant growth experiment data in a table called EXPERIMENT.
| ReadingID | PlantID | Condition | Day | Height | LeafCount |
|---|---|---|---|---|---|
| EX01 | P1 | Sunlight | 1 | 5.0 | 4 |
| EX02 | P2 | Shade | 1 | 5.0 | 4 |
| EX03 | P3 | Sunlight | 1 | 4.5 | 3 |
| EX04 | P1 | Sunlight | 7 | 12.0 | 8 |
| EX05 | P2 | Shade | 7 | 7.5 | 5 |
| EX06 | P3 | Sunlight | 7 | 11.0 | 7 |
| EX07 | P1 | Sunlight | 14 | 18.0 | 12 |
| EX08 | P2 | Shade | 14 | 9.0 | 6 |
(a) Write an SQL query to find the average Height for each Condition on Day 7. [3]
(a) Finding the average height by condition on day 7: [3]
SELECT Condition, AVG(Height)
FROM EXPERIMENT
WHERE Day = 7
GROUP BY Condition[1] for AVG(Height), [1] for WHERE Day = 7, [1] for GROUP BY Condition.
The WHERE clause first filters to only Day 7 readings, then GROUP BY splits them by Condition:
Expected output:
| Condition | AVG(Height) |
|---|---|
| Sunlight | 11.5 |
| Shade | 7.5 |
This result shows that plants grown in sunlight averaged 4.0 cm taller than the shade plant on day 7, which is a meaningful scientific observation. Note that the WHERE filtering happens before the GROUP BY aggregation.
(a) Finding the average height by condition on day 7: [3]
SELECT Condition, AVG(Height)
FROM EXPERIMENT
WHERE Day = 7
GROUP BY Condition[1] for AVG(Height), [1] for WHERE Day = 7, [1] for GROUP BY Condition.
The WHERE clause first filters to only Day 7 readings, then GROUP BY splits them by Condition:
Expected output:
| Condition | AVG(Height) |
|---|---|
| Sunlight | 11.5 |
| Shade | 7.5 |
This result shows that plants grown in sunlight averaged 4.0 cm taller than the shade plant on day 7, which is a meaningful scientific observation. Note that the WHERE filtering happens before the GROUP BY aggregation.
Question 5 Report
A program needs to display a multiplication table for numbers 1 to 5. The output should be neatly aligned in columns.
Expected output:
1 2 3 4 5
2 4 6 8 10
3 6 9 12 15
4 8 12 16 20
5 10 15 20 25(a) Write pseudocode to produce this multiplication table. [4]
(b) Trace the output for the first two rows of the table, showing what is printed at each step. [2]
(c) Explain how you would modify the algorithm to produce a 10x10 multiplication table instead. [1]
(d) State one change needed if the table should show products up to 12 x 12. [1]
(a) The algorithm uses nested FOR loops to generate a multiplication table for numbers 1 to 5. [4]
DECLARE Row : INTEGER
DECLARE Col : INTEGER
DECLARE Product : INTEGER
FOR Row ← 1 TO 5
FOR Col ← 1 TO 5
Product ← Row * Col
OUTPUT Product, " "
NEXT Col
OUTPUT ""
NEXT RowThe outer loop controls the row (the first factor) and the inner loop controls the column (the second factor) [1]. OUTPUT "" after the inner loop moves to a new line [1].
(b) Trace of the first two rows: [2]
| Row | Products | Output line |
|---|---|---|
| 1 | 1*1=1, 1*2=2, 1*3=3, 1*4=4, 1*5=5 | 1 2 3 4 5 |
| 2 | 2*1=2, 2*2=4, 2*3=6, 2*4=8, 2*5=10 | 2 4 6 8 10 |
(c) Change both loop upper bounds from 5 to 10: FOR Row ← 1 TO 10 and FOR Col ← 1 TO 10 [1].
(d) Change both upper bounds to 12 and widen the column spacing since products can reach three digits (up to 12 * 12 = 144) [1].
(a) The algorithm uses nested FOR loops to generate a multiplication table for numbers 1 to 5. [4]
DECLARE Row : INTEGER
DECLARE Col : INTEGER
DECLARE Product : INTEGER
FOR Row ← 1 TO 5
FOR Col ← 1 TO 5
Product ← Row * Col
OUTPUT Product, " "
NEXT Col
OUTPUT ""
NEXT RowThe outer loop controls the row (the first factor) and the inner loop controls the column (the second factor) [1]. OUTPUT "" after the inner loop moves to a new line [1].
(b) Trace of the first two rows: [2]
| Row | Products | Output line |
|---|---|---|
| 1 | 1*1=1, 1*2=2, 1*3=3, 1*4=4, 1*5=5 | 1 2 3 4 5 |
| 2 | 2*1=2, 2*2=4, 2*3=6, 2*4=8, 2*5=10 | 2 4 6 8 10 |
(c) Change both loop upper bounds from 5 to 10: FOR Row ← 1 TO 10 and FOR Col ← 1 TO 10 [1].
(d) Change both upper bounds to 12 and widen the column spacing since products can reach three digits (up to 12 * 12 = 144) [1].
Question 6 Report
The flowchart below shows the control logic for a central heating system.
(a) State the type of sensor needed to measure the temperature. [1]
(b) Describe what happens when the temperature reading is 16 degrees. [2]
(c) Describe what happens when the temperature reading is 20 degrees. [2]
(d) Explain why this is a closed-loop system. [1]
(a) Sensor type: Thermocouple / thermistor / temperature sensor [1]
A thermistor changes its electrical resistance as temperature changes. A thermocouple generates a small voltage proportional to temperature. Both produce analogue signals that can be converted to digital readings.
(b) When the temperature reading is 16 degrees: [2]
The condition Temp < 18 is checked: 16 < 18 is TRUE. [1] The flowchart follows the "Yes" path and the heater is turned ON. The system then loops back to "Read temperature" and continues monitoring. The heater remains on as long as the temperature stays below 18 degrees. [1]
(c) When the temperature reading is 20 degrees: [2]
The condition Temp < 18 is checked: 20 < 18 is FALSE. [1] The flowchart follows the "No" path and the heater is turned OFF. The system then loops back to "Read temperature" and continues monitoring. [1]
(d) Why this is a closed-loop system: [1]
The system continuously reads the temperature (input), compares it to a threshold, and adjusts the heater (output) based on the reading. The heater's effect (warming the room) directly changes the temperature that the sensor reads next, creating a feedback loop. The output influences the input, and the system continuously adjusts itself without human intervention. This continuous monitoring and self-correction through feedback is the defining characteristic of a closed-loop system.
(a) Sensor type: Thermocouple / thermistor / temperature sensor [1]
A thermistor changes its electrical resistance as temperature changes. A thermocouple generates a small voltage proportional to temperature. Both produce analogue signals that can be converted to digital readings.
(b) When the temperature reading is 16 degrees: [2]
The condition Temp < 18 is checked: 16 < 18 is TRUE. [1] The flowchart follows the "Yes" path and the heater is turned ON. The system then loops back to "Read temperature" and continues monitoring. The heater remains on as long as the temperature stays below 18 degrees. [1]
(c) When the temperature reading is 20 degrees: [2]
The condition Temp < 18 is checked: 20 < 18 is FALSE. [1] The flowchart follows the "No" path and the heater is turned OFF. The system then loops back to "Read temperature" and continues monitoring. [1]
(d) Why this is a closed-loop system: [1]
The system continuously reads the temperature (input), compares it to a threshold, and adjusts the heater (output) based on the reading. The heater's effect (warming the room) directly changes the temperature that the sensor reads next, creating a feedback loop. The output influences the input, and the system continuously adjusts itself without human intervention. This continuous monitoring and self-correction through feedback is the defining characteristic of a closed-loop system.
Question 7 Report
The diagram below shows the stages of translating and running a high-level program.
(a) State what is meant by "source code". [1]
(a) "Source code" is the program written by the programmer in a high-level (or assembly) language before it is translated. [1]
It is the human-readable form of the program, containing keywords, variable names, and structures that make sense to the programmer. The source code must be translated by a compiler, interpreter, or assembler into machine code (object code) before the CPU can execute it. The diagram shows this translation pipeline: Source code -> Translator -> Object code (machine code) -> CPU.
(a) "Source code" is the program written by the programmer in a high-level (or assembly) language before it is translated. [1]
It is the human-readable form of the program, containing keywords, variable names, and structures that make sense to the programmer. The source code must be translated by a compiler, interpreter, or assembler into machine code (object code) before the CPU can execute it. The diagram shows this translation pipeline: Source code -> Translator -> Object code (machine code) -> CPU.
Question 8 Report
The flowchart below shows an algorithm that searches an array of 5 elements for a target value.
The array Data contains: [12, 7, 25, 3, 18]
(a) State the type of search shown in this flowchart. [1]
(b) State the output if the Target is 25. [1]
(c) Explain why this flowchart uses a WHILE-style loop rather than a simple FOR loop. [2]
(a) The flowchart shows a linear search. [1]
A linear search checks each element of an array one by one, starting from the first element and moving sequentially through the array until the target value is found or the end of the array is reached.
(b) If the Target is 25: [1]
The search starts at Index 1. Data[1]=12, not 25. Index becomes 2. Data[2]=7, not 25. Index becomes 3. Data[3]=25, which equals the Target. Found is set to TRUE. The loop exits because Found = TRUE. The final decision box checks Found = TRUE, which is true, so the algorithm outputs that the item was found at position 3.
(c) Why the flowchart uses a WHILE-style loop rather than a simple FOR loop: [2]
The WHILE-style loop has two conditions: Index <= 5 AND Found = FALSE. This allows the search to stop early as soon as the target is found. [1] When Found becomes TRUE, the loop condition fails and the algorithm exits immediately without checking the remaining elements.
A simple FOR loop would continue iterating through all remaining elements even after the target has been located. [1] For an array of 5 elements this is a minor inefficiency, but for large arrays with thousands of elements, stopping early when the target is near the beginning saves significant processing time. The WHILE-style loop is therefore more efficient on average.
(a) The flowchart shows a linear search. [1]
A linear search checks each element of an array one by one, starting from the first element and moving sequentially through the array until the target value is found or the end of the array is reached.
(b) If the Target is 25: [1]
The search starts at Index 1. Data[1]=12, not 25. Index becomes 2. Data[2]=7, not 25. Index becomes 3. Data[3]=25, which equals the Target. Found is set to TRUE. The loop exits because Found = TRUE. The final decision box checks Found = TRUE, which is true, so the algorithm outputs that the item was found at position 3.
(c) Why the flowchart uses a WHILE-style loop rather than a simple FOR loop: [2]
The WHILE-style loop has two conditions: Index <= 5 AND Found = FALSE. This allows the search to stop early as soon as the target is found. [1] When Found becomes TRUE, the loop condition fails and the algorithm exits immediately without checking the remaining elements.
A simple FOR loop would continue iterating through all remaining elements even after the target has been located. [1] For an array of 5 elements this is a minor inefficiency, but for large arrays with thousands of elements, stopping early when the target is near the beginning saves significant processing time. The WHILE-style loop is therefore more efficient on average.
Question 9 Report
A palindrome is a word that reads the same forwards and backwards, such as "MADAM" or "RACECAR".
(a) Write pseudocode for a function called IsPalindrome that takes a string as a parameter and returns TRUE if it is a palindrome, or FALSE otherwise. [5]
(b) Trace through your function with the input "LEVEL", showing the comparisons made. [3]
(c) Explain why your function only needs to check half the characters in the string. [1]
(a) The function uses two pointers: Left starting at position 1 and Right starting at the last position. It compares characters at these positions and moves both pointers inward. If any mismatch is found, the string is not a palindrome:
FUNCTION IsPalindrome(Word : STRING) RETURNS BOOLEAN
DECLARE Left : INTEGER
DECLARE Right : INTEGER
Left <- 1
Right <- LENGTH(Word)
WHILE Left < Right DO
IF Word[Left] <> Word[Right] THEN
RETURN FALSE
ENDIF
Left <- Left + 1
Right <- Right - 1
ENDWHILE
RETURN TRUE
ENDFUNCTIONThe function header with parameter and return type [1]. Initialising Left and Right pointers [1]. The WHILE loop condition Left < Right [1]. Comparing characters at both ends [1]. Moving pointers inward and returning the correct result [1]. [5]
(b) Tracing with "LEVEL" (length = 5):
| Step | Left | Right | Word[Left] | Word[Right] | Match? |
|---|---|---|---|---|---|
| 1 | 1 | 5 | 'L' | 'L' | Yes |
| 2 | 2 | 4 | 'E' | 'E' | Yes |
| Check | 3 | 3 | Left is not < Right, loop ends | ||
The function returns TRUE. "LEVEL" is a palindrome. [3]
(c) Each comparison checks a character from the front against its mirror character from the back. Checking beyond the midpoint would repeat the same comparisons already made (comparing position i with position n-i+1 is the same pair as comparing position n-i+1 with position i). Stopping at the midpoint avoids this redundancy. [1]
(a) The function uses two pointers: Left starting at position 1 and Right starting at the last position. It compares characters at these positions and moves both pointers inward. If any mismatch is found, the string is not a palindrome:
FUNCTION IsPalindrome(Word : STRING) RETURNS BOOLEAN
DECLARE Left : INTEGER
DECLARE Right : INTEGER
Left <- 1
Right <- LENGTH(Word)
WHILE Left < Right DO
IF Word[Left] <> Word[Right] THEN
RETURN FALSE
ENDIF
Left <- Left + 1
Right <- Right - 1
ENDWHILE
RETURN TRUE
ENDFUNCTIONThe function header with parameter and return type [1]. Initialising Left and Right pointers [1]. The WHILE loop condition Left < Right [1]. Comparing characters at both ends [1]. Moving pointers inward and returning the correct result [1]. [5]
(b) Tracing with "LEVEL" (length = 5):
| Step | Left | Right | Word[Left] | Word[Right] | Match? |
|---|---|---|---|---|---|
| 1 | 1 | 5 | 'L' | 'L' | Yes |
| 2 | 2 | 4 | 'E' | 'E' | Yes |
| Check | 3 | 3 | Left is not < Right, loop ends | ||
The function returns TRUE. "LEVEL" is a palindrome. [3]
(c) Each comparison checks a character from the front against its mirror character from the back. Checking beyond the midpoint would repeat the same comparisons already made (comparing position i with position n-i+1 is the same pair as comparing position n-i+1 with position i). Stopping at the midpoint avoids this redundancy. [1]
Question 10 Report
The diagram below shows a simplified entity-relationship (ER) model for a school database.
(a) Describe the relationship between TEACHER and CLASS shown in the diagram. [2]
(b) Describe the relationship between CLASS and STUDENT shown in the diagram. [2]
(c) State where the foreign key would be placed to link TEACHER and CLASS. Explain why it goes in that table. [2]
(a) The relationship between TEACHER and CLASS: [2]
This is a one-to-many relationship. One teacher can teach many classes, but each class is taught by exactly one teacher. [1] For example, Mr. Smith might teach Class 10A, 10B, and 11C, but each of those classes has only one teacher assigned. The "1" on the TEACHER side and "M" on the CLASS side in the diagram confirm this. [1]
(b) The relationship between CLASS and STUDENT: [2]
This is also a one-to-many relationship. One class can contain many students, but each student belongs to exactly one class. [1] For example, Class 10A might have 30 students, but each of those students is in only one class. The "1" on the CLASS side and "M" on the STUDENT side confirm this. [1]
(c) Where the foreign key goes to link TEACHER and CLASS: [2]
The foreign key (TeacherID) should be placed in the CLASS table. [1]
In a one-to-many relationship, the foreign key always goes in the table on the "many" side. Since one teacher has many classes, each class record stores the TeacherID of the teacher who teaches it. This way, multiple class records can point to the same teacher. If the foreign key were placed in the TEACHER table instead, each teacher could only reference one class, which contradicts the one-to-many requirement. [1]
(a) The relationship between TEACHER and CLASS: [2]
This is a one-to-many relationship. One teacher can teach many classes, but each class is taught by exactly one teacher. [1] For example, Mr. Smith might teach Class 10A, 10B, and 11C, but each of those classes has only one teacher assigned. The "1" on the TEACHER side and "M" on the CLASS side in the diagram confirm this. [1]
(b) The relationship between CLASS and STUDENT: [2]
This is also a one-to-many relationship. One class can contain many students, but each student belongs to exactly one class. [1] For example, Class 10A might have 30 students, but each of those students is in only one class. The "1" on the CLASS side and "M" on the STUDENT side confirm this. [1]
(c) Where the foreign key goes to link TEACHER and CLASS: [2]
The foreign key (TeacherID) should be placed in the CLASS table. [1]
In a one-to-many relationship, the foreign key always goes in the table on the "many" side. Since one teacher has many classes, each class record stores the TeacherID of the teacher who teaches it. This way, multiple class records can point to the same teacher. If the foreign key were placed in the TEACHER table instead, each teacher could only reference one class, which contradicts the one-to-many requirement. [1]
Question 11 Report
Study the following pseudocode that reverses the order of words in a sentence (not the characters).
FUNCTION ReverseWords(Sentence : STRING) RETURNS STRING
DECLARE Words : ARRAY[1:100] OF STRING
DECLARE WordCount : INTEGER
DECLARE CurrentWord : STRING
DECLARE Result : STRING
DECLARE i : INTEGER
DECLARE Ch : CHAR
WordCount ← 0
CurrentWord ← ""
FOR i ← 1 TO LENGTH(Sentence)
Ch ← Sentence[i]
IF Ch = ' ' THEN
IF CurrentWord <> "" THEN
WordCount ← WordCount + 1
Words[WordCount] ← CurrentWord
CurrentWord ← ""
ENDIF
ELSE
CurrentWord ← CurrentWord & Ch
ENDIF
NEXT i
IF CurrentWord <> "" THEN
WordCount ← WordCount + 1
Words[WordCount] ← CurrentWord
ENDIF
Result ← Words[WordCount]
FOR i ← WordCount - 1 TO 1 STEP -1
Result ← Result & " " & Words[i]
NEXT i
RETURN Result
ENDFUNCTION(a) Trace the word extraction phase for the input "THE CAT SAT". Show the Words array and WordCount after processing. [3]
| Words[1] | Words[2] | Words[3] | WordCount |
|---|---|---|---|
(b) State the return value for the input "THE CAT SAT". [1]
(c) Explain why the code after the FOR loop checks IF CurrentWord <> "". [2]
(d) State the return value for the input "HELLO". [1]
(e) Write pseudocode for a simpler function that reverses the characters (not words) in a string. [7]
(f) The function does not handle multiple consecutive spaces correctly. Explain what would happen with the input "THE CAT" (two spaces) and describe how the algorithm could be modified to handle this. [3]
(g) State two advantages of using a function rather than writing this code directly in the main program. [2]
(h) State the data type returned by the LENGTH function. [1]
(a) The word-extraction loop builds each word character by character until it hits a space, at which point the completed word is stored in the Words array: [3]
| Words[1] | Words[2] | Words[3] | WordCount |
|---|---|---|---|
| THE | CAT | SAT | 3 |
Processing "THE CAT SAT" character by character: T, H, E build CurrentWord = "THE". The space triggers storage into Words[1]. Then C, A, T build "CAT", the next space stores it in Words[2]. Finally S, A, T build "SAT", and the post-loop IF stores it in Words[3]. [1 mark for Words[1] and Words[2], 1 mark for Words[3], 1 mark for WordCount]
(b) The reversal loop starts from Words[3] and prepends each earlier word with a space separator. Result = Words[3] = "SAT", then "SAT" & " " & "CAT" = "SAT CAT", then "SAT CAT" & " " & "THE" = "SAT CAT THE". Return value: "SAT CAT THE". [1]
(c) The FOR loop only stores a word into the array when it encounters a space character. [1] The last word in the sentence is not followed by a space, so it would remain in CurrentWord without being added to the array. The IF check after the loop ensures this final word is also stored correctly. [1]
(d) For input "HELLO", there are no spaces, so the loop never triggers storage. The post-loop IF stores "HELLO" as Words[1] (WordCount = 1). The reversal starts with Result = Words[1] = "HELLO" and the FOR loop from 0 TO 1 STEP -1 does not execute. Return value: "HELLO". [1]
(e) A character-reversal function iterates from the end of the string to the beginning, appending each character to build the reversed result: [7]
FUNCTION ReverseChars(Text : STRING) RETURNS STRING
DECLARE Result : STRING
DECLARE i : INTEGER
Result ← ""
FOR i ← LENGTH(Text) TO 1 STEP -1
Result ← Result & Text[i]
NEXT i
RETURN Result
ENDFUNCTION[1 mark for loop from end to start, 1 mark for building result character by character, 1 mark for returning result]
(f) With input "THE CAT" (two spaces), the algorithm encounters the first space after "THE" and stores it in Words[1]. [1] When it encounters the second space, CurrentWord is empty (""). The IF check IF CurrentWord <> "" prevents this empty string from being added to the array, so the algorithm handles this correctly and produces the same result as single-spaced input. [1] Without this guard, an empty word would be stored, causing the reversed output to contain extra spacing. [1]
(g) Advantage 1: The function can be called multiple times from different parts of the program without rewriting the code (reusability). [1] Advantage 2: The function can be tested independently of the rest of the program, making debugging and maintenance easier (modularity). [1]
(h) The LENGTH function returns an INTEGER, because the number of characters in a string is always a whole number. [1]
(a) The word-extraction loop builds each word character by character until it hits a space, at which point the completed word is stored in the Words array: [3]
| Words[1] | Words[2] | Words[3] | WordCount |
|---|---|---|---|
| THE | CAT | SAT | 3 |
Processing "THE CAT SAT" character by character: T, H, E build CurrentWord = "THE". The space triggers storage into Words[1]. Then C, A, T build "CAT", the next space stores it in Words[2]. Finally S, A, T build "SAT", and the post-loop IF stores it in Words[3]. [1 mark for Words[1] and Words[2], 1 mark for Words[3], 1 mark for WordCount]
(b) The reversal loop starts from Words[3] and prepends each earlier word with a space separator. Result = Words[3] = "SAT", then "SAT" & " " & "CAT" = "SAT CAT", then "SAT CAT" & " " & "THE" = "SAT CAT THE". Return value: "SAT CAT THE". [1]
(c) The FOR loop only stores a word into the array when it encounters a space character. [1] The last word in the sentence is not followed by a space, so it would remain in CurrentWord without being added to the array. The IF check after the loop ensures this final word is also stored correctly. [1]
(d) For input "HELLO", there are no spaces, so the loop never triggers storage. The post-loop IF stores "HELLO" as Words[1] (WordCount = 1). The reversal starts with Result = Words[1] = "HELLO" and the FOR loop from 0 TO 1 STEP -1 does not execute. Return value: "HELLO". [1]
(e) A character-reversal function iterates from the end of the string to the beginning, appending each character to build the reversed result: [7]
FUNCTION ReverseChars(Text : STRING) RETURNS STRING
DECLARE Result : STRING
DECLARE i : INTEGER
Result ← ""
FOR i ← LENGTH(Text) TO 1 STEP -1
Result ← Result & Text[i]
NEXT i
RETURN Result
ENDFUNCTION[1 mark for loop from end to start, 1 mark for building result character by character, 1 mark for returning result]
(f) With input "THE CAT" (two spaces), the algorithm encounters the first space after "THE" and stores it in Words[1]. [1] When it encounters the second space, CurrentWord is empty (""). The IF check IF CurrentWord <> "" prevents this empty string from being added to the array, so the algorithm handles this correctly and produces the same result as single-spaced input. [1] Without this guard, an empty word would be stored, causing the reversed output to contain extra spacing. [1]
(g) Advantage 1: The function can be called multiple times from different parts of the program without rewriting the code (reusability). [1] Advantage 2: The function can be tested independently of the rest of the program, making debugging and maintenance easier (modularity). [1]
(h) The LENGTH function returns an INTEGER, because the number of characters in a string is always a whole number. [1]
Would you like to proceed with this action?