Java OCA OCP Practice Question 750

Question

Given:

public class Main  { 
    private double side = 0;  // LINE 2 
   
    public static void main (String [] args)  {   // LINE 4 
        Main sq = new Main ();  // LINE 5 
        side = 10;  // LINE 6 
    } 
} 

What can be done to make this code compile and run?

Select 1 option

A. replace // LINE 2 with: 
   private int side = 0; 

B. replace // LINE 2 with: 
   public int side = 0; 

C. replace // LINE 5 with: 
   double sq = new Main (); 

D. replace // LINE 6 with: 
   sq.side = 10; //from  w ww . ja v a  2s  .co  m


Correct Option is  : D

Note

side is not a global variable that you can access directly (Note that Java doesn't have the concept of a global variable).

side is a field in Main class.

So you need to specify which Main object's side you are trying to access.

An integer can be assigned to a double but not vice versa.




PreviousNext

Related