Top 10 Java Interview Questions
Are you looking to a get a job as a java software engineer or developer? Here are the top ten questions asked during job interviews.
What are the different type of access modifiers in Java?
There are four access modifiers in Java: public, protected, private, and default (friendly). They can be applied to both classes and to methods within a class.
Public: A public class is visible in other packages.
Protected: A protected class is visible only in the same package or in subclasses.
Private: Accessible only to the class which it is contained in.
Default (friendly): Accessible to classes in the same package (Not in subclasses in other packages).
Public: A public method is visible to all classes.
Protected: A protected method is visible to classes in the same package.
Private: A private method is only available to the class in which it is contained in.
Default (friendly): Accessible to classes in the same package, but not subclasses.
What is an immutable object? Give an example.
An immutable object is an object that cannot have its state changed once it’s created. Immutable objects are particularly useful in multithreaded situations. An example of an immutable object are instances of
the java.lang.String class.
What is a marker interface?
A marker interface is an interface that doesn’t define any fields. It is merely used as a mechanism to “mark” classes which support a certain capability by marking a class as implementing the interface.
When do you override hashcode and equals()?
You must override hashcode and equals whenever you define an object that could be added to a collection using a hash for the collection key. If you don’t, the object will inherit the hashcode and equals method from java.lang.object and will not yield proper results.
What are the differences between Swing and Awt?
Swing components are light-weight whereas Awt components are not. Swing components do not make use of any native UI components of the underlying platform making them completely platform independent. Swing is an evolution of Awt, which fixes a number of flaws and dependencies introduced with Awt.
Does Java use pass-by-value or pass-by-reference?
This question is a little bit more complicated than it initially appears. For methods Java passes all arguments by value. However, when you manipulate objects in Java, they are manipulated by reference.
What is the difference between final, finalize, and finally?
Final: Final in Java makes a variable unable to be reassigned once it is initialized. This is the closest thing you can get to a true constant in Java.
Finalize: The finalize method is something that many Java developers are not aware of. It is a method that aids in garbage collection. The method is automatically called by the JVM whenever an object is about to be destroyed, allowing it to clean up its state. It should not be used for non-memory related resources.
Finally: Anything defined in a finally block automatically executes when a try block exits ( technically unless there is a System.exit() call in the try block ) or when an exception in thrown from the block. Putting all clean up code in the finally block is always good practice regardless whether or not an exception likely to be thrown.
Explain how the keyword synchronized relates to multithreading?
Synchronized relates to multithreading in the because it limits only a single thread to access any code block contained within a synchronized method or statement at a time. It is important to not apply the synchronized keyword too liberally because doing so could negatively effect the performance of multithreaded applications.
What do all java objects inherit from?
All java objects inherit from java.lang.object.
What are the differences between an interface and an abstract class?
The primary differences between an interface and an abstract class is that an interface cannot define any instance variables or define any method definitions. An abstract class can have define instance variables, as well as, define any number of methods.