Is main() method should be compulsorily declared in all Java classes?
Not required. main() method should be defined only if the source class is a Java application.
What is the return type of the main() method?
main() method doesn't return anything hence declared void.
Why main() method declared as static?
main() method is called by the JVM even before the instantiation of the class hence it is declared as static.
Can a main() method be overloaded?
Yes. You can have any number of main() methods with different method signature and implementation in the class.
public class MainOverloaded{
public static void main(String[] name) {
System.out.println("main() method with single argument::");
System.out.println(name[1]);
MainOverloaded mo=new MainOverloaded();
mo.main(Integer.parseInt(name[0]),name[1]);
mo.main(Integer.parseInt(name[0]),name[1],Double.parseDouble(name[2]));
}
public static void main(int rollno, String name) {
System.out.println("main() method with two arguments::");
System.out.println(rollno+" "+name);
}
public static void main(int rollno, String name,double percentage) {
System.out.println("main() method with three arguments::");
System.out.println(rollno+" "+name+" "+percentage);
}
}
Which package is imported by default?
java.lang package.
What is the base class of all classes?
java.lang.Object
What is difference between final, finally and finalize?
final is a keyword used to indicate that a variable holds a constant value.
finally is a block, it will be executed whether exception handled or not.
finalize is a method, used to perform clean up processing just before object is garbage collected.
What is difference between throw and throws?
throw is a keyword used to throw an exception explicitly.
throws is a clause used to declare an exception.
What is the first object-oriented programming language?
Simula
What is the first truly object-oriented programming language?
Smalltalk