Computer Science · Java Programming
Consider the following Java code: class X { private int a; private int b; public synchronized void addA() { a++; } public synchronized void addB() { b++; } } Can two different threads concurrently access the same instance of class X, with one performing x.addA() and the other performing x.addB() at the same time?
- 1No, because the code will cause a compilation error.
- 2Yes, but it will always result in a Runtime Exception.
- 3No, because both methods are synchronized on the same object instance lock.✓
- 4Yes, because they are calling different methods.
Correct answer
No, because both methods are synchronized on the same object instance lock.
Download Single Question PDF
Save this question and verified answer as a clean study sheet with live clickable link.
Share with Friends & Study Groups
Challenge your peer aspirants on WhatsApp or Telegram to solve this question!
Preparing for Computer Science?
Practice full-length mock tests with real exam timers, instant score analysis, and all-India ranking.
Want more practice questions on Java Programming?
Solve complete topic-wise tests with detailed solutions on GkGet.
Related Practice Questions
Recommended practice questions in Computer Science · Java Programming