exam questions

Exam 1z0-819 All Questions

View all questions & answers for the 1z0-819 exam

Exam 1z0-819 topic 1 question 38 discussion

Actual exam question from Oracle's 1z0-819
Question #: 38
Topic #: 1
[All 1z0-819 Questions]

Given the code fragment:

You must make the count variable thread safe.
Which two modifications meet your requirement? (Choose two.)

  • A. replace line 2 with public static synchronized void main(String[] args) {
  • B. replace line 1 with private volatile int count = 0;
  • C. replace line 3 with
    synchronized(test) {
    test.count++;
    }
  • D. replace line 1 with private AtomicInteger count = new AtomicInteger(0); and replace line 3 with test.count.incrementAndGet();
  • E. replace line 3 with
    synchronized(test.count) {
    test.count++;
    }
Show Suggested Answer Hide Answer
Suggested Answer: CD 🗳️

Comments

Chosen Answer:
This is a voting comment (?). It is better to Upvote an existing comment if you don't have anything to add.
Switch to a voting comment New
ASPushkin
5 months, 1 week ago
Selected Answer: CD
answer: CD Thread safety is the avoidance of data races When the result of multiple threads executing a critical section may differ depending on the sequence in which the threads execute The reason of that could be visibility and atomicity problem. Note that atomicity is very different to visibility. After atomic operation we can see another value without the happens-before relationship (see Java Memory Model). To avoid data inconsistency we can use synchronized statements or atomic operator like AtomicInteger. B Failed the use of volatile variables are limited, in non atomic operations (auto increment) E. Failed by definition synchronized statement synchronized(sync_object) {}
upvoted 1 times
...
d7bb0b2
1 year ago
Selected Answer: CD
D => 0nly one observarion, Notice atomic.incrementAndGet is ++testcont atomicinteget.getAndIncrement is testcount ++
upvoted 2 times
d7bb0b2
1 year ago
on the conext is not important because not used after that, but is only a observation
upvoted 1 times
...
...
Stavok
1 year, 6 months ago
Selected Answer: CD
Option C replaces line 3 with a synchronized block that synchronizes on the test object. This ensures that only one thread can execute the code within the synchronized block at a time, preventing race conditions when multiple threads try to increment the count variable simultaneously. Option D replaces line 1 with an AtomicInteger variable, which provides atomic operations for updating its value. The incrementAndGet method is then used on line 3 to atomically increment the value of the count variable. This also prevents race conditions when multiple threads try to increment the count variable simultaneously.
upvoted 1 times
...
Mukes877
1 year, 8 months ago
Selected Answer: CD
D & C is correct And E is not because synchronized can only be used with objects or references to objects and used to lock objects.
upvoted 1 times
...
RP384
1 year, 11 months ago
Selected Answer: CD
E is syntactically wrong
upvoted 1 times
...
Community vote distribution
A (35%)
C (25%)
B (20%)
Other
Most Voted
A voting comment increases the vote count for the chosen answer by one.

Upvoting a comment with a selected answer will also increase the vote count towards that answer by one. So if you see a comment that you already agree with, you can upvote it instead of posting a new comment.

SaveCancel
Loading ...
exam
Someone Bought Contributor Access for:
SY0-701
London, 1 minute ago