You have a Java interview coming up and you’re smart. You know the best way to prepare for an interview is to practice answers to possible interview questions. Here, things get a little more daunting though – there are hundreds of questions you could be faced with in an interview. How do you know which ones to prepare for?
While there is no way to know what questions will be asked, make sure you prepare answers to this list of ten frequently asked Java interview questions. Technical recruiters often use these as their opening questions, testing your technical know-how before they launch into the more difficult ones.
1. What is the difference between an Abstract class and an Interface?
An abstract class has instance methods that implement a default behavior. An interface differs from an abstract class as it cannot implement default behavior. An interface only declares constants and instance methods.
2. How do stacks and queues differ?
Queues use the FIFO rule, whereas stacks use the LIFO rule.
3. What is the purpose of garbage collection in Java? When is it used?
A garbage collection finds and gets rid of objects that are no longer needed by a program. This process allows for the program’s resources to be reused.
An object is eligible to be garbage collected when it becomes unreachable to the program. The JVM will initiate garbage collection when it desires. Therefore, eligible objects may sit in memory for some time before being collected.
4. What is the Polymorphism Principle?
Simply put, it is “one interface, multiple methods.” This means that one set of code can have a variety of functions, and that the functionality of that specific piece of code is situation dependent.
5. What does static mean in Java?
As opposed to one per object, static means one per class. As static methods are connected to classes, and overriding is object based, static methods are final.
6. What is an immutable object in Java? Can you change the value of an immutable object?
An immutable object in Java is an object that cannot be changed after it is created. Because it cannot be disrupted by thread interference, an immutable object in Java allows you to create simple code that can be used in complex environments.
7. What is the difference between private, protected, and public modifiers?
- Private means that they are only accessible to the class that owns the feature.
- Protected means that they are accessible to the class to which they belong, as well as any subclasses, and any classes in the same package.
- Public means that they are accessible to all classes.
8. List several advantages of string class being immutable.
- Because immutable objects are thread safe, you can work on two different threads at the same time without issue.
- When your security system goes over read-only information, you don’t have to worry that it will be changed.
- String class being immutable makes Java substring implementation run as quickly and as smoothly as it does. You don’t have to create a substring copy, all you have to is point into an existing base.
9. What are two reasons you’d use a synchronized block instead of a synchronized method?
You would use a synchronized block when you want to:
- place locks for a shorter period of time
- lock on a specified object
10. What is the difference between String, StringBuffer, and StringBuilder?
The objects in StringBuffer and StringBuilder are mutable, while those in String are not. StringBuffer and StringBuilder are different in that StringBuilder is not synchronized, while StringBuffer is.
What opening questions have you been faced with in a Java interview? Are there any questions you’d add to the list? Let us know in the comments section below, or join the conversation on Facebook, Twitter, and LinkedIn.
Thanks to mlinksva for the use of their photograph.