ProgrammingHardAccenture · Cognizant pattern
What will be the output of the following pseudocode (counting pairs summing to a target, brute force)? (Arrays are indexed from 0.)
What will be the output of the following pseudocode (counting pairs summing to a target, brute force)? (Arrays are indexed from 0.)
Integer arr[5] = {1, 5, 7, -1, 5}
Integer target = 6, cnt = 0
for (each i from 0 to 4)
for (each j from i + 1 to 4)
if (arr[i] + arr[j] == target)
cnt = cnt + 1
end if
end for
end for
Print cnt- A. 2
- B. 4
- C. 3
- D. 1
Correct answer: C. 3
The pairs summing to 6 are (1,5) at indices (0,1), (1,5) at indices (0,4), and (7,-1) at indices (2,3) -- three matching pairs in total.
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 (counting pattern occurrences, allowing overlaps)? Function countOcc(String text…
- What will be the output of the following pseudocode (manually parsing a signed integer string)? Function strToInt(String s) Integer sign =…
- What will be the output of the following pseudocode (counting trailing zeros in n factorial, via counting factors of 5)? (All variables…
- What will be the output of the following pseudocode (fast exponentiation)? (All variables are integers, so / is integer division.) Integer…
- What will be the output of the following pseudocode (checking if one string is a rotation of another)? Function isRotation(String a…
- What will be the output of the following pseudocode (counting total set bits across 1..n)? Function countSetBitsRange(Integer n) Integer…