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
  1. A. 2
  2. B. 4
  3. C. 3
  4. 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.

More Pseudocode and Code Tracing questions

Exams that ask this topic