1Z0-811 Java Basics and Syntax Practice Question
A developer writes code to calculate the average of two integers: int a = 5; int b = 10; int avg = a / b;. Which change ensures the average is correctly calculated as a double?
⚠ Common exam trap
Oracle often tests the misconception that changing variable types alone fixes integer division, but the trap is that integer literals (5 and 10) remain int unless explicitly cast or assigned to double variables before the division.
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
✓
Cast one operand to double: (double) a / b
In Java, when both operands of the division operator are integers, integer division is performed, truncating the fractional part. By casting one operand to double, the operation becomes floating-point division, yielding a double result (0.5). This ensures the average is calculated correctly as a double.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✗
No change needed; integer division is correct
Why it's wrong here
Integer division truncates, so average is incorrectly 0.
- ✗
Change the variable types to double
Why it's wrong here
Changing variable types would work, but the question asks for a change to the existing code, not a complete rewrite; also this is not the minimal change.
- ✓
Cast one operand to double: (double) a / b
Why this is correct
Casting one operand to double ensures floating-point division.
- ✗
Use Math.round(a / b)
Why it's wrong here
Math.round still operates on integer division result, not a fix.
Go deeper
Related to this question
About these practice questions
Courseiva writes every 1Z0-811 question from scratch — 481 in total, each with an explanation and a wrong-answer breakdown. None are copied from real exams or dumps. Learn why practice questions differ from exam dumps →
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.