ProgrammingHardLTIMindtree · Cognizant GenC
What will be the output of the following pseudocode?
What will be the output of the following pseudocode?
Function g(Integer n)
if (n == 0)
return 0
end if
if (n mod 2 == 0)
return g(n - 1) + n
else
return g(n - 1) - n
end if
end function
Print g(9)- A. -9
- B. -5
- C. -4
- D. 5
Correct answer: B. -5
g(9) = -9 + 8 - 7 + 6 - 5 + 4 - 3 + 2 - 1 = -5.
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[6] = {1, 2, 3, 4, 5, 6} Procedure rev(Integer l, Integer r) while (l < r)…
- What will be the output of the following pseudocode? String s = "aaabccdddd", r = "" Integer i = 0, j while (i < length(s)) j = i while (j…
- What will be the output of the following pseudocode? Integer i, j, c = 0 for (i = 1; i <= 6; i = i + 1) for (j = 1; j <= 6; j = j + 1) if…
- What will be the output of the following pseudocode? Integer a = 48, b = 18, steps = 0 while (a != b) if (a > b) a = a - b else b = b - a…
- What will be the output of the following pseudocode? Integer a[15] = {2, 5, 8, 12, 16, 23, 38, 45, 56, 72, 91, 99, 101, 110, 120} Integer…
- What will be the output of the following pseudocode? Integer a[4][4], i, j, s = 0 for (i = 0; i < 4; i = i + 1) for (j = 0; j < 4; j = j +…