ProgrammingHardTCS NQT · Accenture pattern
What will be the output? (&& does not evaluate its right side when the left side is false.)
What will be the output? (&& does not evaluate its right side when the left side is false.)
Integer c = 0, s = 0
Function inc()
c = c + 1
return c
End Function
for (each i from 1 to 5)
if (i mod 2 == 0 && inc() > 1)
s = s + i
end if
end for
Print s, c- A. 6 2
- B. 6 5
- C. 4 2
- D. 4 5
Correct answer: C. 4 2
inc() is called only when i is even (i = 2 and 4), so c ends at 2. At i = 2, inc() returns 1, which is not > 1. At i = 4 it returns 2, so s = 4. Assuming inc runs on every pass gives c = 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? (& is bitwise AND, | is bitwise OR, ^ is bitwise XOR, << and >> are left and right…
- Two sorted arrays a = {1, 4, 7} and b = {2, 3, 8, 9} are merged. How many element comparisons does the pseudocode make? Integer i = 0, j =…
- What will be the output of the following pseudocode? (Arrays are indexed from 0.) Integer m[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}…
- What will be the output of the following pseudocode? (All variables are integers, so / is integer division.) Integer c = 0, i = 16 while…
- What will be the output of the following pseudocode? (All variables are integers, so / is integer division.) Function f(Integer n) if (n <…
- How many lines does this pseudocode print for hanoi(5, 'A', 'B', 'C')? Function hanoi(Integer n, Char from, Char via, Char to) if (n == 0)…