ProgrammingHardInfosys · Accenture pattern
What will be the output of the following pseudocode? (Arrays are indexed from 0.)
What will be the output of the following pseudocode? (Arrays are indexed from 0.)
Integer a[5] = {5, 1, 8, 2, 3}, c = 0
for (i = 0 to 3)
for (j = i + 1 to 4)
if (a[i] > 2 * a[j])
c = c + 1
end if
end for
end for
Print c- A. 5
- B. 3
- C. 4
- D. 6
Correct answer: C. 4
Only pairs with the larger value EARLIER count. 5 (index 0): 5 > 2·1 and 5 > 2·2, but not 5 > 2·3 → 2. 8 (index 2) is compared only with the later 2 and 3: 8 > 4 and 8 > 6 → 2. Total 4. The 1 at index 1 lies before 8, so (8, 1) is not a pair; counting ordinary inversions (a[i] > a[j]) gives 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? (A static variable is initialised once and keeps its value between calls; operands…
- What will be the output of the following pseudocode? (^ is bitwise XOR, & is AND, >> is a right shift.) Integer x = 29, y = 30 Integer gx…
- What will be the output of the following pseudocode? (Arrays are indexed from 0; the matrix has 3 rows and 4 columns.) Integer m[3][4] =…
- What will be the output of the following pseudocode? (calls counts every call of f, including the first.) Integer calls = 0 Function…
- What will be the output of the following pseudocode? (/ is integer division; the division in the loop is always exact.) Integer n = 10, c…
- What will be the output of the following pseudocode? ('ref' parameters are passed by reference; ^ is bitwise XOR; arrays are indexed from…