Study the following pseudocode. PROCEDURE SwapByValue(X : INTEGER, Y : INTEGER) DECLARE Temp : INTEGER Temp ← X X ← Y Y ← Temp ENDPROCEDURE ...

Assessment: Computer Science (9-1) 0984 | Paper 2 Mock 01 | Algorithms, Programming and Logic Subject: Computer Science (9-1) - 0984

Question 1 Report

Study the following pseudocode.

PROCEDURE SwapByValue(X : INTEGER, Y : INTEGER)
    DECLARE Temp : INTEGER
    Temp ← X
    X ← Y
    Y ← Temp
ENDPROCEDURE

PROCEDURE SwapByRef(BYREF X : INTEGER, BYREF Y : INTEGER)
    DECLARE Temp : INTEGER
    Temp ← X
    X ← Y
    Y ← Temp
ENDPROCEDURE

DECLARE A : INTEGER
DECLARE B : INTEGER
A ← 5
B ← 9

CALL SwapByValue(A, B)
OUTPUT "After SwapByValue: A=", A, " B=", B

CALL SwapByRef(A, B)
OUTPUT "After SwapByRef: A=", A, " B=", B

(a) State the two lines of output. [4]

(b) Explain why SwapByValue does not change the values of A and B. [2]

Answer Details

(a) The key difference is that SwapByValue receives copies of A and B, while SwapByRef receives direct references to the original variables. [4]

  1. After SwapByValue: A=5 B=9 (values unchanged because the swap operated on copies) [2]
  2. After SwapByRef: A=9 B=5 (values swapped because BYREF modifies the originals) [2]

(b) SwapByValue passes A and B by value, meaning the procedure receives independent copies named X and Y. [1] The swap correctly exchanges X and Y inside the procedure, but these are local copies. When the procedure ends, the copies are discarded, and the original A (still 5) and B (still 9) are unaffected. [1]

Download The App On Google Playstore

Everything you need to excel in your exams

Green Bridge CBT Mobile App
Personalized AI Learning Chat Assistant
200,000+ Exam Questions Across IGCSE, JAMB, WAEC & NECO
Over 3,900 Lesson Notes
Offline Support - Learn Anytime, Anywhere
Green Bridge Timetable
Literature Summaries & Potential Questions
Track Your Performance & Progress
In-depth Explanations for Comprehensive Learning