ProgrammingModerateTCS NQT · Infosys
What will be the output of the following pseudocode?
What will be the output of the following pseudocode?
Integer a[7] = {1, 3, 2, 4, 1, 5, 2}, c = 0
for (each i from 1 to 5)
if (a[i] > a[i - 1] AND a[i] > a[i + 1])
c = c + 1
end if
end for
Print c- A. 3
- B. 2
- C. 4
- D. 5
Correct answer: A. 3
Interior elements larger than both neighbours: 3 (between 1 and 2), 4 (between 2 and 1) and 5 (between 1 and 2). The end elements are never checked. c = 3.
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[5] = {1, 2, 3, 4, 5} Integer odd = 0, even = 1, s = 0, ans = 0 // counts of…
- What will be the output of the following pseudocode? (& is bitwise AND on 32-bit integers) Function pow4(Integer n) return n > 0 AND (n &…
- What will be the output of the following pseudocode? (& is AND, ^ is XOR, << is left shift) Integer x = 7, m = 1 while ((x & m) != 0) x =…
- What will be the output of the following pseudocode? Integer printed = 0 Procedure perm(String s, Integer l) // s is passed by value if (l…
- What will be the output of the following pseudocode? Integer t[4][4] = {{2}, {3, 4}, {6, 5, 7}, {4, 1, 8, 3}} // row r has r + 1 numbers…
- What will be the output of the following pseudocode? Integer a = 20, b = 6, c = 0 while (a > b) a = a - 3 b = b + 2 c = c + 1 end while…