ProgrammingHardAccenture · Cognizant pattern
What will be the output of the following pseudocode (finding the missing number from 1..n)? (Arrays are indexed from 0.)
What will be the output of the following pseudocode (finding the missing number from 1..n)? (Arrays are indexed from 0.)
Integer arr[5] = {1, 2, 4, 5, 6}
Integer n = 6
Integer expected = n * (n + 1) / 2
Integer actual = sum(arr)
Print expected - actual- A. 2
- B. 4
- C. 3
- D. 5
Correct answer: C. 3
The sum of 1..6 is 21 (expected). The array's actual sum is 1+2+4+5+6=18. The missing number is 21-18=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 (checking whether an array is sorted in non-decreasing order)? (Arrays are indexed…
- What will be the output of the following pseudocode (summing a range with a step)? Function sumRange(Integer a, Integer b, Integer step)…
- What will be the output of the following pseudocode (tail-recursive factorial with an accumulator)? Function factAcc(Integer n, Integer…
- What will be the output of the following pseudocode (GCD of an entire list, folding pairwise)? Function gcdList(Array arr) Integer r =…
- What will be the output of the following pseudocode (counting permutations of a string via recursion)? Function countPerms(String s) if…
- What will be the output of the following pseudocode (basic run-length string compression, kept only if actually shorter)? Function…