Core Java - Interview Questions and Answers for 'Main method' | Search Interview Question - javasearch.buggybread.com
Javasearch.buggybread.com

Search Interview Questions


 More than 3000 questions in repository.
 There are more than 900 unanswered questions.
Click here and help us by providing the answer.
 Have a video suggestion.
Click Correct / Improve and please let us know.
Label / Company      Label / Company / Text

   



Core Java - Interview Questions and Answers for 'Main method' - 15 question(s) found - Order By Rating

 Q1. What is the default execution method in Java?Core Java
Ans. public static void main(String[] args)

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     main method


 Q2. Why do we pass an array of strings to main method ?Core Java
Ans. Array of strings in the main method are the list of arguments or parameters which are sent to the application / program.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     main method   main method string array argument


 Q3. Can we compile and execute a Java class without main method ?Core Java
Ans. No without main method can not be executed it will throw an error illegal start of type. Main method is the entry point of application.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     jvm  compilation  main method      Basic


 Q4. If we don't provide any argument to the executed program, What arguments will be passed to the main method ? Will the String array argument be null ?Core Java
Ans. No. It won't be null but an empty array with size 0

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     main method


 Q5. Can we have multiple main methods in a single class ?Core Java
Ans. We can overload the main method by specifying different argument types. For example -

2 main methods with different arguments is perfectly legal

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

The following are not legal as compiler will complain of duplicate methods

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

Even The following are not legal as we cannot overload on return types

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

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     main method     Asked in 2 Companies


 Q6. Can an application have multiple main methods within different classes ? If yes, How will the app decide which one to be executed ?Core Java
Ans. Yes we can have a main method with string[] argument in every class of an application. When we execute an app we specify the starting point i.e the class that will get the control first and hence main method of that class gets executed.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     main method     Asked in 3 Companies


 Q7. Why main method is declared static ?Core Java
Ans. static is the keyword that makes it accessible even without creating any object and using class name only. Making it non static would like creation of object upfront before calling the method.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     main method  static     Asked in 4 Companies      Basic


 Q8. Can we declare the main method as private ?Core Java
Ans. Yes, compiler won't complain but at runtime it will give an error saying "Error: Main method not found in class". Even though we can use this method as any other private method, it cannot be invocate by executing the class.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     main method   main method visibility      basic

Try 1 Question(s) Test


 Q9. Do all java classes need a main method?
Ans. No, Main method is the entry point into an application. An application usually contain multiple classes to perform a function.

Lets take an example of a House, House usually have only one external Door and you may have internal doors to move around within a house. Internal Doors are methods of classes whereas External Door is a special method called main method.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     main method


 Q10. Can we declare a main method as abstract ?
Ans. No. Static methods cannot be overridden and hence make no sense to be declared abstract.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     main method   abstract

Try 1 Question(s) Test


 Q11. What will happen if static modifier is removed from the signature of the main method?
Ans. Program throws "NoSuchMethodError" error at runtime .

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     java   main method   main   static   static method   nosuchmethoderror


  Q12. Can we overload main method in Java ?Core Java
Ans. Yes, but the overloaded main methods without single String[] argument doesn't get any special status by the JVM. They are just another methods that needs to be called explicitly.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     java   main method   overloading   yes-no     Asked in 6 Companies      intermediate        frequent


Frequently asked to fresh graduates and less experienced.
 Q13. Why do we write public static void main ? Can we use some other syntax too for main ?Core Java
Ans. 1. public is the access modifier that makes the method accessible from anywhere, static is the keyword that makes it accessible even without creating any object, void means it doesn't return anything , String args[] is the array of argument that the method receives.

2. If we use main without the string args , it will compile correctly as Java will treat it as just another method. It wont be the method "main" which Java looks for when it looks to execute the class and hence will throw

Error: Main method not found in class , please define the main method as:
public static void main(String[] args)

3. Main is not a keyword but a special string that Java looks for while initiating the main thread.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     java   main method     Asked in 4 Companies      basic        frequent

Try 1 Question(s) Test


 Q14. How can we execute a Java class independently if it doesn't have a static main method ?

a. By initiating the flow in any of the static method
b. By initiating the flow in any of static block
c. By initiating the flow in any other instance method named as main
d. By initiating the flow in any other instance method named as main and making it final
Ans. By initiating the flow in any of static block

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     main method   java


 Q15. Which of the following is false about main method ?

a. It should be declared public and static
b. it should have only 1 argument of type String array
c. We can override main method
d. We can overload main method
Core Java
Ans. We can override main method

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     java   main method


 Q16. Which of the following keyword is expected with main method declaration ?Core Java
a. Final
b. Static
c. Volatile
d. Transient

Ans.b. Static

 Q17. Which of following about main method is not true ?Core Java
a. We can have multiple methods named main in a class
b. We can have var args with the main method
c. We can overload main method
d. We can override main method

Ans.d. We can override main method


Help us and Others Improve. Please let us know the questions asked in any of your previous interview.

Any input from you will be highly appreciated and It will unlock the application for 10 more requests.

Company Name:
Questions Asked: