Java OCA OCP Practice Question 2849

Question

Given:

2. public class Main {  
3.   static int x;  
4.   int y;  
5.   public static int getX() { return x; }  
6.   public static void setX(int newX) { x = newX; }  
7.   public int getY() { return y; }  
8.   public void setY(int newY) { y = newY; }  
9. } 

Which lines of code need to be changed to make the class thread safe? (Choose all that apply.)

  • A. Line 2
  • B. Line 3
  • C. Line 4
  • D. Line 5
  • E. Line 6
  • F. Line 7
  • G. Line 8


H:Note

B-G are correct.

The variables need to be private, and even methods that don't change a variable's value need to be synchronized if they access the variable.

A is incorrect.

This class statement has no effect on thread safety-ness.




PreviousNext

Related