Courseiva
Java Basics and SyntaxhardMultiple ChoiceObjective-mapped

1Z0-811 Java Basics and Syntax Practice Question

A financial trading application processes high-volume transactions. The system uses a multithreaded architecture where multiple threads update a shared Account object's balance field. Recently, intermittently incorrect balance calculations have been reported. Developers suspect a race condition on the balance field. The Account class is defined as follows:

public class Account {
    private double balance = 0.0;
    public void deposit(double amount) { balance += amount; }
    public void withdraw(double amount) { balance -= amount; }
    public double getBalance() { return balance; }
}

Threads are created using ExecutorService with a fixed thread pool. The issue occurs only under heavy load. Which course of action should the development team take to resolve the issue while maintaining performance?

⚠ Common exam trap

Oracle often tests the misconception that `volatile` solves all concurrency issues, but the trap here is that `volatile` does not provide atomicity for compound operations, so candidates who choose Option A fail to recognize that `balance += amount` is not a single atomic step.

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

Synchronize the deposit and withdraw methods.

Synchronizing the `deposit` and `withdraw` methods ensures mutual exclusion on the `balance` field. In Java, the compound operations `balance += amount` and `balance -= amount` are not atomic; they involve a read, modify, and write sequence. Synchronization guarantees that only one thread executes these methods at a time, preventing race conditions and ensuring correct balance calculations under heavy load.

Answer analysis

Option-by-option breakdown

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

  • Declare the balance field as volatile.

    Why it's wrong here

    volatile ensures visibility but not atomicity of compound operations.

  • Use AtomicLong and convert to double.

    Why it's wrong here

    AtomicLong is for long, not double. AtomicDouble does not exist in standard Java.

  • Wrap the balance field in a synchronized block within each method using a separate lock object.

    Why it's wrong here

    This is essentially the same as synchronizing the methods but adds boilerplate; not the most straightforward.

  • Synchronize the deposit and withdraw methods.

    Why this is correct

    Synchronizing ensures both visibility and atomicity for these methods.

Visual reference

Client Recursive Resolver Root DNS (13 root servers) TLD DNS (.com, .org, …) Authoritative example.com query IP addr answer

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.