Java OCA OCP Practice Question 1380

Question

Given the following code,

which statements can be placed at the indicated position without causing compile and run time errors?

public class Main { 
   int i1; 
   static int i2; 
   public void method1 (){ 
      int i; 
      //  ... insert statements here 
    } 
} 

Select 3 options

  • A. i = this.i1;
  • B. i = this.i2;
  • C. this = new Test ( );
  • D. this.i = 4;
  • E. this.i1 = i2;


Correct Options are  : A B E

Note

For Option A.

As i 1 is an instance variable, it is accessible through 'this'.

For Option B.

Although 'this' is not needed to access i2, it is not an error to do so.

For Option C.

No, you can't change this.

For Option D.

You cannot do this.i as i is a local variable.

For Option E.

You are just assigning a static field's value to non-static field.




PreviousNext

Related