ProgrammingVery hardTCS NQT · Infosys · Capgemini
What will be the output of the following pseudocode? (Arrays are 0-indexed.)
What will be the output of the following pseudocode? (Arrays are 0-indexed.)
Integer a[7] = {1, 2, 4, 5, 7, 9, 10}
Integer i = 0, j = 6, c = 0
While i < j
If a[i] + a[j] <= 11 Then
c = c + (j - i)
i = i + 1
Else
j = j - 1
End If
End While
Print c- A. 12
- B. 13
- C. 9
- D. 3
Correct answer: A. 12
When a[i] + a[j] fits the limit, every index between i+1 and j also pairs with a[i], so the whole block of (j - i) pairs is added at once. The routine therefore counts all pairs with sum at most 11 (brute force also gives 12).
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? (Division is integer division; arrays are 0-indexed.) Integer a[9] = {2, 3, 3, 3, 5…
- What will be the output of the following pseudocode? (& is bitwise AND; arrays are 0-indexed.) Integer m[4][4] For i = 0 to 3 For j = 0 to…
- What will be the output of the following pseudocode? Function J(n, k) If n = 1 Then Return 0 Return (J(n - 1, k) + k) mod n End Function…
- What will be the output of the following pseudocode? Integer n = 1, s = 0 Repeat s = s + n n = n 2 + 1 Until s >= 100 Print n, s
- What will be the output of the following pseudocode? Integer s = 0 for (each i from 1 to 5) s = s + i i end for Print s
- What will be the output of the following pseudocode? Integer i = 10 while (i > 2) i = i - 3 end while Print i