Java OCA OCP Practice Question 1165

Question

Given:

class MyClass  { 
    private double side = 0; 
    String color; /*from ww  w.  j  a v a2s.c  o  m*/
    public MyClass (double length){ 
        this.side = length; 
     } 
    public double getSide ()  {  return side;     } 

    public void setSide (double side)  {  this.side = side;    } 
     
} 

public class Main  { 
    public static void main (String [] args) throws Exception  { 
        MyClass mysq = new MyClass (10); 
        mysq.color = "red"; 
         
        //set mysq's side to 20 
     } 
} 

Which of the following statements will set mysq's side to 20?

Select 1 option

A. mysq.side = 20; 
B. mysq = new MyClass (20); 
C. mysq.setSide (20); 
D. side = 20; 
E. MyClass.mysql.side = 20; 


Correct Option is  : C

Note

Since side is a private variable, you cannot access it from outside MyClass class.




PreviousNext

Related