Java OCA OCP Practice Question 2716

Question

Given the following declaration:

public class Main{
   public int i;
   public static void main(String argv[]){
      Main sc = new Main();
      // Comment line
   }
 }

Which of the following statements are correct if they replace the comment line?

  • a. System.out.println(i);
  • b. System.out.println(sc.i);
  • c. System.out.println(Main.i);
  • d. System.out.println((new Main()).i);


b and d

Note

Option a is incorrect because instance variables need to be used with an object.

Option c is incorrect because instance variables cannot be used with a classname.




PreviousNext

Related