Tuesday - 18 March, 2025 +91

Java Interview Questions

In this post I am going to cover some basic questions regarding to Core Java generally asked in Interviews. Please note that I am just covering the questions keeping beginners in mind. So advanced learners who know Java very well may entirely skip this post.

Q: What if the main method is declared as private?
A: The program compiles properly but at run time it will give a message “Main method not public.”

Q: What if I write static public void main instead of public static void main?
A: Both are valid. Program compiles and runs successfully.

Q: What if the static modifier is removed from the signature of the main method?
A: The Program will compile. But at run time throws an error.

Q: What if I do not provide the String array as the argument to the method?
A: Program compiles but it will not run.

Q: Can a main method in java return an integer?
A: No. Main method should always associated with void.

Q: Is it possible to write a program using goto keyword?
A: Even though goto keyword is reserved, we cannot write a program using goto.

Q: Can I have multiple main methods in the same class?
A: No the program fails to compile. The compiler will say that the main method is already defined in the class.
Q: Is it possible to write a Java program with out main method?
A: Yes it is possible. Refer this post.

This post will be updated frequently to add new questions with answers. So keep in touch

0 comments:

Post a Comment