ProgrammingVery hardAmazon OA · Infosys SP
What will be the output of the following pseudocode?
What will be the output of the following pseudocode?
Integer a[5] = {7, 7, 5, 7, 6}
Integer m1 = -1, m2 = -1
for (each i from 0 to 4)
if (a[i] >= m1)
m2 = m1
m1 = a[i]
else if (a[i] > m2)
m2 = a[i]
end if
end for
Print m1, m2- A. 7 6
- B. 7 7
- C. 7 5
- D. 7 -1
Correct answer: B. 7 7
Because of >=, the repeated 7 pushes the old 7 down into m2: after a[1], m1 = m2 = 7, and nothing smaller can replace it. Output 7 7. With a strict > (and skipping equal values) the second largest distinct value 6 would be found.
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] = {2, 3, 4, 5, 1}, passes = 0, n = 5 Boolean swapped for (each i from 0…
- What will be the output of the following pseudocode? (All variables are integers, so / is integer division) Integer n = 77, steps = 0, p…
- What will be the output of the following pseudocode? (<< is left shift, | is bitwise OR, & is bitwise AND) Integer x = 0 for (each i from…
- What will be the output of the following pseudocode? (All variables are integers, so / is integer division) Integer mults = 0 Function…
- What will be the output of the following pseudocode? String s = "(()(()))()" Integer depth = 0, best = 0, pairs = 0 for (each i from 0 to…
- What will be the output of the following pseudocode? (^ is XOR, & is AND) Integer s = 0, x, p for (each i from 1 to 20) x = i p = 0 while…