AI InterviewSearch…Search problems, topics, or pages…Ctrl KGet started
Mediumfunctionscall by reference

~90s

One by reference, one by value

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 + q
Show the answer and the worked solution

Correct answer: C.

13

Worked solution

  1. x is bound to p itself, so Set x = x + 10 leaves p = 11.
  2. y is a copy of q, so Set y = y + 10 changes the copy only and q is still 2.
  3. 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.

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.

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 + q

Options

  1. 3
  2. 12
  3. 13
  4. 23
Show the answer and the worked solution

Correct answer: C. 13

Worked solution

  1. x is bound to p itself, so Set x = x + 10 leaves p = 11.
  2. y is a copy of q, so Set y = y + 10 changes the copy only and q is still 2.
  3. 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.

All Pseudocode questions · placement mock tests