Java Interview Questions and Answers


21. Why is the main() method declared static ?

main() method is called by the JVM even before the instantiation of the class
hence it is declared as static.

22. What is the argument of main() method ?

main() method accepts an array of String object as argument.

23. Can a main() method be overloaded ?

Yes. You can have any number of main() methods with different method signature and implementation in the class.

24. Can a main() method be declared final ?

Yes. Any inheriting class will not be able to have it’s own default main() method.

25. Does the order of public & static declaration matter in main() method ?

No. It doesn’t matter but void should always come before main().

26. Can we print “Hello” even before main() is executed ?

Print the statement inside a static block of code. Static blocks get executed when the class gets loaded into the memory and even before the creation of an object. Hence it will be executed before the main() method. And it will be executed only once.

27. What is the restriction imposed on a static method or a static block of code ?

A static method should not refer to instance variables without creating an instance and cannot use “this” operator to refer the instance.

28. Can a class be declared as static ?

We can not declare top level class as static, but only inner class can be declared static.

 29. Can a source file contain more than one class declaration ?

Yes a single source file can contain any number of Class declarations but only one of the class can be declared as public.

30. Can a class be declared as protected ?

No. The protected access modifier cannot be applied to class and interfaces. Methods, fields in a class can be declared protected, however methods and fields in a interface cannot be declared protected.

31. Can you give few examples of final classes defined in Java API?

java.lang.String, java.lang.Math are final classes.

32. What is the role of  Import Statement ?

Import statement is used to make other classes in a different package accessible to the current class and to tell the compiler where to find the classes. To use classes in the same package we don’t want to use import package. Import statement should be defined as follows.

33. How is final different from finally and finalize() ?

final is a modifier which can be applied to a class or a method or a variablefinal class can’t be inherited, final method can’t be overridden and final variable can’t be changed.

finally is an exception handling code section which gets executed whether an exception is raised or not by the try block code segment.

finalize() is a method of Object class which will be executed by the JVM just before garbage collecting object to give a final chance for resource releasing activity.

34. What is the importance of static variable ?

static variables are class level variables where all objects of the class refer to the same variable. If one object changes the value then the change gets reflected in all the objects.

35. Can we overload Constructors ?

Yes. We can overload constructors like normal overloading methods.


Profile photo of Siva

About Siva

Senior Hadoop developer with 4 years of experience in designing and architecture solutions for the Big Data domain and has been involved with several complex engagements. Technical strengths include Hadoop, YARN, Mapreduce, Hive, Sqoop, Flume, Pig, HBase, Phoenix, Oozie, Falcon, Kafka, Storm, Spark, MySQL and Java.

Leave a comment

Your email address will not be published. Required fields are marked *