exam questions

Exam 1z0-819 All Questions

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

Exam 1z0-819 topic 1 question 81 discussion

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

Given:



Which makes class A thread safe?

  • A. Class A is thread safe.
  • B. Make foo and setB synchronized.
  • C. Make foo synchronized.
  • D. Make A synchronized.
  • E. Make setB synchronized.
Show Suggested Answer Hide Answer
Suggested Answer: B 🗳️

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
1 month, 4 weeks ago
There is no right answer here. Variables that form the object's state are int a = 0; int b = 0; int c = 0; The problem is with policy for managing concurrent access to the object's state. Concurrent access to it can be done within the package directly. Reads and writes are atomic for reference variables and for most primitive variables (all types except long and double). So, there is the guarantee that it is atomic, but with no guarantees about the visibility of the (potentially) new value from other threads.
upvoted 1 times
...
ASPushkin
6 months, 1 week ago
Selected Answer: B
answer: B public class A int a = 0; int b = 0; int c = 0; First of all, class A is thread-safe because declaring variables a,b,c is not private. It is possible to change the value of them within the package directly. C. Failed Reads and writes are atomic for reference variables and for most primitive variables (all types except long and double). Method setB does not establish a happens-before relationship, so it’s simply that there is no guarantee that the modification it causes is visible in other threads. All you get in this case is the guarantee that the it is atomic, but with no guarantees about the visibility of the (potentially) new value. So, that means that both methods should be synchronized
upvoted 1 times
ASPushkin
6 months, 1 week ago
First of all, class A is not thread-safe because declaring variables a,b,c are not private.
upvoted 1 times
...
...
d7bb0b2
1 year ago
B is correct!
upvoted 1 times
...
Omnisumem
1 year, 4 months ago
Selected Answer: B
Obviously B.
upvoted 1 times
...
Stavok
1 year, 5 months ago
Selected Answer: B
The correct answer is B. Make foo and setB synchronized. Class A is not thread-safe because the methods foo and setB can be called concurrently by multiple threads, leading to race conditions when accessing and modifying the shared variables a, b, and c. To make class A thread-safe, both methods foo and setB should be made synchronized. This ensures that only one thread can execute either method at a time, preventing race conditions.
upvoted 2 times
...
tmuralimanohar
1 year, 6 months ago
Answer: B
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