One by reference, one by value — Aptitude Question
One by reference, one by value: The parameter marked ref is passed by reference; the other is passed by value. Answer and worked solution on CodeKairo.
- Topic: Pseudocode
- Section: Programming MCQs
- Difficulty: Medium
- Time target: 90 seconds
Question
The parameter marked ref is passed by reference; the other is passed by value. What does the program print?
Function Bump(Integer ref x, Integer y)
Set x = x + 10
Set y = y + 10
End Function
Integer p, q
Set p = 1
Set q = 2
Bump(p, q)
Print p + qOptions
- 3
- 12
- 13
- 23
Show the answer and the worked solution
Correct answer: C. 13
Worked solution
- x is bound to p itself, so Set x = x + 10 leaves p = 11.
- y is a copy of q, so Set y = y + 10 changes the copy only and q is still 2.
- The sum printed is 11 + 2 = 13.
Answer: 13.
The idea
A reference parameter is another name for the caller's variable; a value parameter is a private copy. The distractors 3 and 23 are what you get by treating both parameters the same way.
Shortcut. Only the ref parameter keeps its +10, so the sum is the original 1 + 2 plus a single 10: 13.
Common trap. 23 adds 10 to both p and q, treating the value parameter y as if it were also passed by reference.