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]
(a) The key difference is that SwapByValue receives copies of A and B, while SwapByRef receives direct references to the original variables. [4]
After SwapByValue: A=5 B=9 (values unchanged because the swap operated on copies) [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]
Everything you need to excel in your exams