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

Search Java Interview Questions


 2098 questions in repository.
 There are more than 200 unanswered questions.
Click here and help us by providing the answer.
Label / Company      Label / Company / Text

   



Interview Questions and Answers for 'Main method' - 16 question(s) found - Order By Newest

Frequently asked to fresh graduates and less experienced.
 Q1. Why do we write public static void main ? Can we use some other syntax too for main ?Core Java
Admin
info@buggybread.com
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 and using class name only , void means it doesnt return anything , String args[] is the array of argument that this method receives.

2. If I use Main , it will compile correctly as Java will treat it as just another method but 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     Cognizant (CTS)  Computer Solutions & Software International (CSSI)  Telcoinabox      basic        frequent

Try 1 Question(s) Test


 Q2. 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
Anonymous
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


 Q3. Can we have multiple main methods in a single class ?Core Java2017-01-12 11:00:02

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     HCL Technologies  SapientNitro


  Q4. Can we overload main method in Java ?Core Java
Admin
info@buggybread.com
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     The Advisory Board Company  Thomson Reuters  ePace Technologies  PC Technology  PEOL  MilleniumSoft      intermediate        frequent


 Q5. What will happen if static modifier is removed from the signature of the main method?
Admin
info@buggybread.com
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


 Q6. 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
Anonymous
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


 Q7. Can we declare a main method as abstract ?2016-06-21 13:11:31

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


 Q8. Do all java classes need a main method?2016-07-20 15:17:32

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


 Q9. Can we declare the main method as private ?Core Java2016-07-26 08:31:57

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



Do you think these are the Best Java Frameworks ?

OpenXavaSPRING MVCApache StripesCheck everything
that is Best in Java

Click Here



 Q10. Why main method is declared static ?Core Java2017-01-09 18:35:30

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     Adtech  Quark  PayPal        Basic


 Q11. Can an application have multiple main methods within different classes ? If yes, How will the app decide which one to be executed ?Core Java2017-01-12 10:56:40

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     HCL Technologies  Aspire Systems  SapientNitro


 Q12. 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 Java2017-04-04 14:38:48

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


 Q13. Can we compile and execute a Java class without main method ?Core Java2017-06-18 18:11:13

 This question is still unanswered. Can you please provide an answer.


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

   Like      Discuss      Correct / Improve     jvm  compilation  main method      Basic


 Q14. Why do we pass an array of strings to main method ?Core Java2017-06-23 12:58:05

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


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

Ans.b. Static

 Q16. 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



Subscribe to Java News and Posts. Get latest updates and posts on Java from Buggybread.com
Enter your email address:
Delivered by FeedBurner



comments powered by Disqus
 

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:
         

X Close this

X Close this

Help Us Improve.
Please share your
interview experience.

Company Name:   


Questions Asked: