OA. free
Free
Accenture Core Computer Science Core Computer Science Medium

When calculating the sum of a large array of positive 32-bit integers in Java using an int...

Accenture technical mcq question, verified with a worked answer. Free to practise - no sign-up.

When calculating the sum of a large array of positive 32-bit integers in Java using an int accumulator, what issue can occur?

Choose one option.
Show answer & explanation
Answer: A. Arithmetic integer overflow resulting in negative values

In Java, int is a 32-bit signed integer with a maximum value of 2,147,483,647 (Integer.MAX_VALUE). When summing a large array of positive integers, the accumulator can exceed this limit, causing wraparound to negative values due to two's complement representation. Java does not throw exceptions for integer overflow—it silently wraps around, which is a common source of bugs.

Step-by-step Derivation:
Example: If we sum positive integers that total 2,500,000,000, the int accumulator will wrap:

  • Maximum int value: 2,147,483,647
  • Sum exceeds max by: 2,500,000,000 - 2,147,483,647 = 352,516,353
  • Result wraps to: -2,147,483,648 + 352,516,353 = -1,794,967,295 (negative)

Option B is wrong because primitive int elements don't throw NullPointerException.
Option C is wrong because Java does not automatically promote to BigInteger; overflow silently wraps.
Option D is wrong because the exception relates to sum calculation, not array indexing at index 0.