Java OCA OCP Practice Question 1586

Question

Consider the following class definition:

public class Main{
   public static void main (){  new Main ().sayHello ();  }   //1
   public static void sayHello (){ System.out.println ("Static Hello World");
   public void sayHello ()  { System.out.println ("Hello World ");  }  //3
}

What will be the result of compiling and running the class?

Select 1 option

  • A. It will print Hello World.
  • B. It will print Static Hello World.
  • C. Compilation error at line 2.
  • D. Compilation error at line 3.
  • E. Runtime Error.


Correct Option is  : D

Note

You cannot have two methods with the same signature, name and parameter types, in one class.

If you put one sayHello() method in other class, it won't compile because you cannot override/hide a static method with a non static method and vice versa.




PreviousNext

Related