ProgrammingVery hardProduct company OA
What will be the output of the following pseudocode? ('ref' parameters are passed by reference; others by value.)
What will be the output of the following pseudocode? ('ref' parameters are passed by reference; others by value.)
Procedure p(ref Integer a, Integer b)
a = a + b
b = a - b
End Procedure
Integer x = 3, y = 4
p(x, y)
p(y, x)
Print x, y- A. 7 11
- B. 4 3
- C. 7 4
- D. 11 7
Correct answer: A. 7 11
p(x, y): x (by reference) becomes 3 + 4 = 7; the local copy b changes, but y stays 4. p(y, x): now y is the reference: y = 4 + 7 = 11; again only the local b changes, so x stays 7. Output 7 11. Treating both parameters as references turns p into a partial swap and gives 4 3-style results.
Practise more Pseudocode and Code Tracing questionsPseudocode and Code Tracing: notes and key points
More Pseudocode and Code Tracing questions
- What will be the output of the following pseudocode? (/ is integer division.) Function A(Integer n) if (n <= 1) return n end if return B(n…
- What will be the output of the following pseudocode? (x is an 8-bit unsigned value; & is bitwise AND, | is OR, << and >> are shifts.)…
- What will be the output of the following pseudocode? (/ is integer division.) Integer x = 10 x = x + x 2 - 4 / 2 Print x
- What will be the output of the following pseudocode? for (i = 10; i >= 1; i = i - 3) Print i end for
- What will be the output of the following pseudocode? (/ is integer division; + on strings concatenates.) Integer n = 100 String s = ""…
- What will be the output of the following pseudocode? (/ is integer division.) Function f(Integer n) if (n == 0) return 0 end if return (n…