Tutorials

Java Basic Interview Question


Java Interview Question Set3


Q1: Write main() method with correct signature?

Given below is the main() method with correct type signature.

public static void main(String[] args) {		

//main body		
		
}

Q2: Explain meaning of each term in main() method?

As we have already seen from above question that important terms in main are:

  • public
  • static
  • void
  • main
  • String[ ] args

We have already provided detailed answer explaining each term in our java tutorial here: click to read


 

Q3: Shuffle the terms of main method and execute. Will it run?

Yes it will run fine for some combinations.

The code will compile and run if we write keywords in main() method in any order as shown below:

  • public static void main(String[] args)
  • static public void main(String[] args)

But these combinations won't compile:

  • public void static main(String[] args) → compiler error as method main doesnot have valid return type. static is not a return type so this combination fails.
  • static void public main(String[] args) → Same reason as stated above

Q4: Why main() method is needed in java?

In java main() method is the entry point. 

Whenever a program is executed the JVM search for the main method with the type signature as explained in question 1.

So main is needed to start the program execution and help JVM locate the starting point of the program.

 


Q5: Can main() method be overloaded in java?

Yes we can overload main method in java.

 

Find the example of main() method overloading given below:

public class TutorialsInHand {

	public static void main(String[] args){
		int a=10, b =40;
		main(a, b);
	}

	public static void main(int a,int b){ //overloaded main
		int result = a+b;
		System.out.println(result);
	}

}

Similarly you can overload more methods. 

If you want to learn about method overloading in details then following the link to our java tutorial - click here



Please Share this page
Views : 78
Like every other website we use cookies. By using our site you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our Terms of Service. Learn more Got it!