ProgrammingHardAccenture · Cognizant pattern
What will be the output? (A static variable keeps its value between calls and is initialised only once. The two calls in Print are evaluated left to right.)
What will be the output? (A static variable keeps its value between calls and is initialised only once. The two calls in Print are evaluated left to right.)
Function fun(Integer n)
static Integer c = 0
c = c + n
if (n > 1)
fun(n - 1)
end if
return c
End Function
Print fun(4) + fun(2)- A. 10
- B. 33
- C. 13
- D. 23
Correct answer: D. 23
fun(4) adds 4 + 3 + 2 + 1 to c, so c = 10 and fun(4) returns 10. c is NOT reset, so fun(2) adds 2 + 1 and returns 13. 10 + 13 = 23. Treating c as an ordinary local gives 10 + 3 = 13.
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? Integer a = 1, b = 2, c = 3 a = b + c b = a - b c c = a b - c Print a + b + c
- What will be the output of the following pseudocode? (All variables are integers, so / is integer division.) Integer s = 0 for (each i…
- What will be the output of the following pseudocode? (All variables are integers, so / is integer division.) Integer n = 100, c = 0, d = 5…
- What will be the output? (calls counts every call, including the first.) Integer calls = 0 Function fun(Integer a, Integer b) calls =…
- What will be the output of the following pseudocode? Integer c = 0 for (i = 1; i < 64; i = i 2) for (j = i; j < 64; j = j + i) c = c + 1…
- What will be the output of the following pseudocode? (& is bitwise AND, | is bitwise OR, ^ is bitwise XOR, << and >> are left and right…