Java Basic Interview Questions

Make sure you know the answers to these basic Java questions for your next interview.

What happens if you use a primitive local variable before initializing it?


The Java compiler will throw an error. Local variables do not get initialized to any default value even if they are primitives like int or boolean. Every local variable needs to be assigned a value before it can be accessed.

When is memory allocated for static members?


All instantiation and memory allocation for static variables for all classes in the program is done before running the main method.

What Collection would you use to iterate with insertion order and also have O(1) lookup time? Are there any drawbacks to using this Collection?


LinkedHashSet provides an average O(1) lookup time and also maintains the entries in a doubly-linked list, which can be used to maintain insertion order. The only drawback in this case is that we cannot have duplicates in the Collection because it is a Set.

How many servlet instances are created when multiple requests arrive simultaneously?


Only one Servlet instance is created at the time of the first call in the program. This instance is reused for all further requests even if multiple requests arrive simultaneously.

Describe the two different types of Servlets.


The two types of Servlets are GenericServlet and HttpServlet.

GenericServlet is a protocol-independent Servlet used as a base class for writing non-HTTP servlets. It provides basic functionality for writing a Servlet.

HttpServlet is an extension of GenericServlet that is used for writing Web-based HTTP servlets. It is used for HTTP methods like

...

What is the difference between: final, finally, finalize?


final is a keyword which can be used on a variable, class, or method.

finally is a code block that is guaranteed to run after a throws block, even if the exception is not thrown.

finalize is a method

...

What are Generics in Java?


Generics are a feature that allow a class to be parameterized into sub-types. Generics provide compile-time safety and prevent runtime typecasting errors resulting in ClassCastException. Without generics, a user is forced to use the lowest common base-class and then typecast the object, which is highly error-prone and has no type-safety.

Can a base-type access properties in its sub-types?


When a class is inherited, the base type can have properties shared by all sub-types, and each sub-type can have their specific properties. The sub-type properties cannot be accessed by other sub-types of the same Generic object or even in the base-type.

What is Polymorphism?


Polymorphism is the ability to have one interface for accessing several implementations. The interface determines the requirements for the implementation. The implementation can be accessed using the interface because it abides to the interface.

What is method overloading and method overriding?


Method overloading is having several forms of the same method, each differing in the formal argument list.

Method overriding is when the exact same method definition is created in a sub-class, which results in hiding the base-class method.

What happens if the main method is not static?


If a program has a main method without the static keyword, it will compile, but will throw a Runtime Exception with NoSuchMethodError.

Looking for developers?

Want to learn more about Gigster? Enter your email and we'll be in touch.