I get it - it can get frustrating waiting for that job interview. You start counting days, then hours and minutes until you finally sit down with your potential employer to discuss your future in the company. Sometimes nervousness might take the lead and you might find yourself in a difficult situation, not knowing what to say or how to respond. To avoid this, however, and to ace the interview, you should come prepared. This tutorial about Java interview questions will help you do exactly that.
I’ve segmented them into a few groups to make it easier to learn and remember. These questions (or, rather, their answers) will help you ace that interview and land the job as a Java developer in no time!
Table of Contents
- 1. Understanding the Basics of Java
- 1.1. Question 1: What is Java?
- 1.2. Question 2: Is Java fully object-orientated?
- 1.3. Question 3: Name a few notable Java features.
- 1.4. Question 4: What’s special about Java 8?
- 1.5. Question 5: What are the 'access specifiers' in Java?
- 1.6. Question 6: What is a ‘constructor’?
- 1.7. Question 7: What is an 'object'?
- 1.8. Question 8: What’s the difference between 'equals()' and '=='?
- 1.9. Question 9: What different object references can there be in Java?
- 1.10. Question 10: What are the differences between JDK, JVM, and JRE?
- 1.11. Question 11: What is ‘Garbage Collection’ used for in Java?
- 1.12. Question 12: Do you know how to differentiate abstract class from the interface?
- 1.13. Question 13: What are the differences between path and classpath variables?
- 1.14. Question 14: Do you know what is ‘Synchronization’ in Java?
- 1.15. Question 15: Do you know how to achieve thread-safety in Java?
- 2. Experienced Questions of Java
- 2.1. Question 1: What’s the difference between ‘method overloading’ and ‘method overriding’?
- 2.2. Question 2: What’s the output of this Java program?
- 2.3. Question 3: Is it possible to execute a program without the 'main() method'?
- 2.4. Question 4: What is ‘runtime polymorphism’?
- 2.5. Question 5: What is ‘Inheritance’?
- 2.6. Question 6: Name the superclass for all other classes in Java.
- 2.7. Question 7: What does ‘super’ in Java mean?
- 2.8. Question 8: What is the output of this Java program?
- 2.9. Question 9: What is 'association' in Java?
- 2.10. Question 10: What’s 'object cloning'?
- 3. Tips for the Interview
- 4. Conclusions
Understanding the Basics of Java
We’ll talk about Java 8 interview questions and I’ll provide answers for experienced developers. Let’s start from the beginning, though, and jump into the basics.
Latest Deal Active Right Now:
GET 50% OFF
DataCamp Black Friday Sale
During this DataCamp Black Friday, you can access the top-rated courses with a 50% discount. Enroll now for way less!
Question 1: What is Java?
It seems like a pretty simple question, doesn’t it? Well, your potential employer probably expects a clear and straight-to-the-point answer, so let’s provide him one.
Java is an object-orientated, high-level secure programming language. It was created and developed back in 1991 by a man named James Gosling. Java is designed to represent the “WORA” slogan - “write once, run anywhere”. It is known for being flexible and running with high-performance.
Question 2: Is Java fully object-orientated?
This is a quite possible to be one of Java interview questions and no, it is not fully object-orientated. Java uses some data types (char, byte, float) which are not objects.
Question 3: Name a few notable Java features.
For beginner programmers and developers, the most important feature is simplicity. Java is considered to be quite easy to learn and master, especially when compared with other programming languages out there.
Java is also considered to be very secure when compared to other programming languages. This is because of an interpreter called JVM - this interpreter is installed together with Java itself, and it constantly supplies your computer with the latest security updates from the internet.
Additionally, Java is fully portable. Because of the “WORA” principle which Java is built on, it can be transferred and applied on to any machine you'd like - it's fully flexible and multifunctional.
Question 4: What’s special about Java 8?
In your Java interview questions, you are more than likely to get mostly comparison-type ones. This update to the programming language was one of the more notable ones - it would be beneficial to know what new features emerged from it.
Java 8 introduced language support for String, provided an improved Date/Time API, and furthered the development of JVM - the system responsible for many things Java, including its security. Overall, Java 8 made the programming language more accessible and better in-line with modern-day programming languages.
This is probably going to be one of the main Java 8 interview questions that you’ll receive. Just keep in mind that Java 8 was a huge improvement both performance and security-wise, and you should be good to go.
Question 5: What are the 'access specifiers' in Java?
In total, four access specifiers could be asked as one of your Java interview questions. They are as follows: public, private, protected and default.
Public specifiers allow access to any class or via any method (hence their name). As opposed to this, private specifiers allow access only within the specified class itself. Protected allows access that somehow correlates with the class - either from within the class, from a sub-class or simply from the same package. Finally, default signifies the standard (default) scope and allows access only from the same package.
Question 6: What is a ‘constructor’?
A “constructor” is a portion of a code that initializes a specific object. Java has two types of constructors - a default one and a parameterized one.
Question 7: What is an 'object'?
A common term explanation among Java interview questions. An object in Java has a state and behavior. The most common and understandable definition of an object is that it is an instance of a class.
Question 8: What’s the difference between 'equals()' and '=='?
public class Equaltest {
public static void main(String[] args) {
String str1= new String(“HELLO”);
String str2= new String(“HELLO”);
if(Str1 == str2)
{
System.out.println("String 1 == String 2 is true");
}
else
{
System.out.println("String 1 == String 2 is false");
String Str3 = Str2;
if( Str2 == Str3)
{
System.out.println("String 2 == String 3 is true");
}
else
{
System.out.println("String 2 == String 3 is false");
}
if(Str1.equals(str2))
{
System.out.println("String 1 equals string 2 is true");
}
else
{
System.out.prinltn("String 1 equals string 2 is false");
}
}}
Question 9: What different object references can there be in Java?
Out of all Java interview questions, this one falls into the easy category - in Java, all object references are null.
Question 10: What are the differences between JDK, JVM, and JRE?
JDK is a Java Development Kit. It’s the main tool in Java used to compile the programs of this language. Within the package, it contains all of the necessary tools to start using it.
I’ve already mentioned JVM previously - it stands for Java Virtual Machine. It’s a machine that creates the environment in which Java bytecode can function properly.
JRE is Java Runtime Environment. This is the type of environment that JVM provides - it allows the Java bytecode to run and function properly.
Question 11: What is ‘Garbage Collection’ used for in Java?
Do not get mixed up when it comes to this kind of Java interview questions that seem to not have any connection to the Java itself. The purpose of this feature is to identify and discard the objects that are no longer needed by the application to facilitate the resources to be reclaimed and reused.
Question 12: Do you know how to differentiate abstract class from the interface?
- Abstract classes can have method implementations whereas interfaces cannot.
- A class can extend only one abstract class but it can be implemented into multiple interfaces.
- You can run an abstract class if it has main () method but not an interface.
Question 13: What are the differences between path and classpath variables?
The path is an environment variable that is used by the OS to locate the executables. For this reason, we need to add the directory location to the Path variable when Java is installed or to make the OS find any executable.
The classpath is specific to Java and is used by executables to locate class files. You can provide a classpath location while running a Java application and it can be a directory, ZIP file or JAR file.
Question 14: Do you know what is ‘Synchronization’ in Java?
Another term among Java interview questions. It is a reference to multi-threading. A synchronized block of code can be executed by only one thread at a time. Synchronization is a process that keeps all concurrent threads in execution to be in sync. The process of synchronization allows avoiding memory consistency errors caused due to an inconsistent view of shared memory. When a method is declared as synchronized the thread holds the monitor for that method’s object. If another thread is executing the synchronized method the thread is blocked until that thread releases the monitor.
Question 15: Do you know how to achieve thread-safety in Java?
You can achieve it by:
- Synchronization
- Atomic concurrent classes
- Implementing a concurrent Lock interface
- Using a volatile keyword
- Using immutable classes
- Thread-safe classes.
Experienced Questions of Java
Question 1: What’s the difference between ‘method overloading’ and ‘method overriding’?
One of the first possible basic Java interview questions could be this one. In a “method overloading” case, methods that are in the same class share the same name, yet their parameters differ. This is concerned with extensions of the method’s behavior more than anything else. Reversely, “method overriding” sub-classes have methods of the same name and parameters. The aim here is to alter the already-existing method’s behavior.
Just to give you an example, here’s method overloading:
class Adder {
Static int add(int x, int y)
{
return x+y;
}
Static double add( double x, double y)
{
return x+y;
}
public static void main(String args[])
{
System.out.println(Adder.add(33,33));
System.out.println(Adder.add(16.4,16.8));
}}
And this is method overriding:
class Dog {
void run(){
System.out.println(“dog is sleeping”);
}
Class Doberman extends Dog{
void run()
{
System.out.prinltn(“doberman is sleeping at night”);
}
public static void main( String args[])
{
Dog b=new Doberman();
b.run();
}
}
Question 2: What’s the output of this Java program?
public class Test
{
Test(int x, int y)
{
System.out.println("x = "+x+" y = "+y);
}
Test(int x, float y)
{
System.out.println("x = "+x+" y = "+y);
}
public static void main (String args[])
{
byte x = 30;
byte y = 65;
Test test = new Test(x,y);
}
}
The correct answer is this:
a = 30 b = 65
Question 3: Is it possible to execute a program without the 'main() method'?
Pretty standard among Java interview questions and yes, it is possible to do that. One of the most common ways to execute a program like that is by using a static block.
Question 4: What is ‘runtime polymorphism’?
A “runtime polymorphism” is a process in which a specific call that is issued to an overridden method will be resolved in runtime instead of compile time. Here’s an example:
class Tree {
void run()
{
System.out.println(“tree is standing”);
}
}
class Willow extends Tree {
void run()
{
System.out.prinltn(“willow is standing on a hill”);
}
public static void main(String args[])
{
Tree b= new Willow(); //upcasting
b.run();
}
}
Question 5: What is ‘Inheritance’?
The term is honestly almost self-explanatory - inheritance is when one object acquires the properties and parameters of another one (of a different class). The above-discussed method overriding uses this - the main idea of inheritance is that you can build new classes on already-existing ones. There are five different types of inheritance, but Java only supports four (Multiple inheritances aren't supported). Why aren't Multiple inheritances supported? There’s only one specific reason - to simplify the program. This should be an important note to remember for your Java interview questions.
Question 6: Name the superclass for all other classes in Java.
This is one of the easier ones - however, I should also add that it's one of the core questions. The superclass in Java is the object class.
Question 7: What does ‘super’ in Java mean?
“Super” in Java is used as a reference to point to an immediate parent class object. The command can also be used to invoke an immediate parent class methods and constructor.
Question 8: What is the output of this Java program?
class Animal
{
public Animal()
{
System.out.println("Animal class constructor called");
}
}
public class Zebra extends Animal
{
public Zebra()
{
System.out.println("Zebra class constructor called");
}
public static void main (String args[])
{
Zebra e = new Zebra();
}
}
The answer:
Animal class constructor called
Zebra class constructor called
These are very common Java interview questions. Always pay attention to the variables - they define the final answer.
Question 9: What is 'association' in Java?
One of the more asked Java coding interview questions, this might not seem like an “advanced” one at first. However, things like aggregation and composition stem from the association, so it is important to understand the term.
Association is when all objects have their lifecycles and no specific owner exists. It can vary between “one” and “many”.
- Easy to use with a learn-by-doing approach
- Offers quality content
- Gamified in-browser coding experience
- Free certificates of completion
- Focused on data science skills
- Flexible learning timetable
- High-quality courses
- Nanodegree programs
- Student Career services
- Nanodegree programs
- Suitable for enterprises
- Paid certificates of completion
- A wide range of learning programs
- University-level courses
- Easy to navigate
- University-level courses
- Suitable for enterprises
- Verified certificates of completion
Question 10: What’s 'object cloning'?
The “object cloning” command is used to create an identical copy of the object. This is done by using the clone() method from the Object class.
Tips for the Interview
I probably don’t need to mention the whole good night's sleep and healthy breakfast scenarios - you’ve probably heard it all at least a thousand times already. What I can say is that you most definitely shouldn’t be up at 3 AM revising your basic Java interview questions when you’ve got the interview at 8 AM the next day. There’s no need to stress - try not to think about the interview at least 24 hours up to it - revise your notes only on your way to the interview.
Also, act relaxed when you're there. If the employer sees that your nervous, he might think that you’re worrying because you’ve got something to hide - perhaps a lack of skill? Don’t act cocky, though - find the perfect balance between being confident and leaving room for improvisation.
Did you know?
Have you ever wondered which online learning platforms are the best for your career?
Conclusions
With a need for programmers on the rise, multiple companies and individual employers are looking for experienced professionals in the field of computer coding. Because Java is one of the most popular programming languages in the world (thanks to its flexibility, security, and simplicity), many potential programmers choose it as their primary language to learn. In this tutorial, I’ve shown you some core Java interview questions and answers. There are many more Java coding interview questions that you might get asked during your job interview, but if you know these, you’re already set on a good path. Don't forget to visit BitDegree courses and tutorials pages to learn more about computer science.
And if you are just starting out with Java, be sure to enroll in BitDegree's interactive course to learn the basics.