Courseiva
Primitives, Strings and OperatorsmediumMultiple ChoiceObjective-mapped

1Z0-811 Primitives, Strings and Operators Practice Question

A stock trading system calculates daily profit using an int variable. During periods of high volatility, the profit can exceed Integer.MAX_VALUE (2,147,483,647). When this happens, the profit value wraps around to a negative number, leading to incorrect reporting. The lead developer wants to detect the overflow and throw an ArithmeticException rather than silently producing wrong results. The code cannot use long or BigInteger due to legacy constraints. Which approach should be taken?

Answer choices

Why each option matters

Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.

Correct answer & explanation

Use Math.addExact() for the addition and catch or let the exception propagate.

Math.addExact() performs integer addition and throws an ArithmeticException if the result overflows, exactly meeting the requirement to detect overflow and throw an exception without changing data types. Option A (bitwise AND) does not detect overflow reliably. Option C (casting to long) changes the data type, violating the legacy constraint. Option D (BigInteger) also changes the data type. Therefore, option B is correct.

Answer analysis

Option-by-option breakdown

For each option: why learners choose it and why it is or isn't the right answer here.

  • Use bitwise AND with a mask to detect overflow.

    Why it's wrong here

    Bitwise AND with a mask can detect overflow only when the overflow pattern is known and bounded, such as in unsigned integer arithmetic or fixed-width bit fields. In this scenario, the profit variable is a signed int, and overflow wraps arbitrarily around the two's complement range; no single mask can reliably distinguish a legitimate large positive value from a wrapped negative result. This approach is tempting because bitwise masking is commonly used in low-level systems to check flag bits or to isolate specific bits in hardware registers, where the overflow condition is deterministic and maskable.

  • Use Math.addExact() for the addition and catch or let the exception propagate.

    Why this is correct

    Math.addExact precisely detects overflow and throws ArithmeticException, fitting the requirement to alert about overflow.

  • Cast the operands to long before addition and then cast back to int.

    Why it's wrong here

    Casting to long avoids overflow temporarily, but casting back to int can still produce incorrect values without detection. It also violates the no-type-change constraint.

  • Use BigInteger for the calculation and convert back to int.

    Why it's wrong here

    Using BigInteger changes the data type, which is prohibited by the legacy constraint.

About these practice questions

This 1Z0-811 question is part of Courseiva's 481-question bank — original exam-style content with full explanations and wrong-answer analysis, never real exam questions or exam dumps. Learn why practice questions differ from exam dumps →

How Courseiva writes practice questions · Editorial policy

JA

Written by Johnson Ajibi, MSc IT Security

Senior Network & Security Engineer · founder of Courseiva

This 1Z0-811 practice question is part of Courseiva's free Oracle certification practice question bank. Courseiva provides original exam-style practice questions with explanations, topic-based practice, mock exams, readiness tracking, and study analytics to help learners prepare for the 1Z0-811 exam.