Home » Java Interview Questions and Answers

Java Interview Questions and Answers

In this article we have gathered the latest and most asked Java Interview Questions and Answers for both freshers and experienced. The Core Java Interview Questions cover the basics of Java Interview Questions, OOPs Interview Questions, String Handling Interview Questions, Multithreading Interview Questions, Collections Interview Questions, JDBC Interview Questions, etc.

Java is a high-level, class-based, object-oriented programming language designed to have as few implementation dependencies as possible. It is a general-purpose programming language intended to give programmers the ability to write once, run anywhere (WORA), which means that the compiled Java code can run on all platforms that support Java without the need to recompile.
Java was developed by James Gosling in June 1991. It can also be known as a platform as it provides its own JRE and API.

JAVA Interview Questions and Answers

These Java Interview Questions will help job seekers to prepare well for the job interview and cross the panel discussion easily.

So let’s get started :

1. What are the main features of Java?
2. What are the differences between C++ and Java?
3. What do you understand about JVM?
4. What are the major differences between JDK, JRE, and JVM?
5. What are the different types of memory areas allocated by JVM?
6. What is a Class in Java?
7. What is the platform?
8. Is the JVM platform independent?
9. What is the default value of the local variables?
10. Can you declare the main() method as final?
11. What is object cloning?
12. What is the output of the following Java program?
13. What is an object?
14. What will be the initial value of an object reference which is defined as an instance variable?
15. What is a constructor?
16. What is the purpose of a default constructor?
17. Can you make a constructor final?
18. What are the differences between the constructors and methods?
19. What is the output of the following Java program?
20. Is String a data type in java?
21. Why Strings in Java are called as Immutable?
22. When a lot of changes are required in data, which one should be a preference to be used? String or StringBuffer?
23. How can we make copy of a java object?
24. What is a Java servlet?
25. For multiple requests, how many objects of a servlet are created?
26. What is a JSP in Java and are they better than servlets?
27. Explain the Collection Framework in Java.
28. What is the difference between Concurrent Collections and Synchronized Collections?
29. Can a class in Java be inherited from more than one class?
30. What is a static keyword?
31. Can you override the static method?
32. What if the static modifier is removed from the signature of the main method?
33. How THIS keyword is used in the class?
34. Why Java doesn’t support multiple inheritances yet multiple interface implementation is possible?
35. How to restrict inheritance for a class?
36. Is it possible to override private methods?
37. What do you mean by method overriding?
38. What do you understand by thread-safe?
39. Explain multithreading in Java.
40. Explain JSON.

1. What are the main features of Java?

There are the following features in Java Programming Language. Some of the notable Java features are:

  • It is an object-oriented language, and because of this, it can be extended easily.
  • Secured programming language because it has no explicit pointer and runs inside a virtual machine sandbox.
  • It is Platform independent having the ability to Write Once and Run Anywhere (WORA).
  • Interpreted, the Java compiler generates byte-codes, rather than native machine code.
  • Robust in terms of memory management, automatic garbage collection.
  • It is multi-threaded, and it shares a common memory area to deal with multiple tasks in a single go.
  • It is a high-performing language when compared to other traditional interpreted programming languages because it uses the Just in Time compiler.

2. What are the differences between C++ and Java?

The differences between C++ and Java are given below:

You May Also Like :   SQL Interview Questions and Answers
C++Java
C++ is platform dependentJava is platform-independent
C++ is mainly used for system programming.Java is mainly used for application programming. It is widely used in window, web-based, enterprise and mobile applications.
C++ supports multiple inheritances.Java doesn’t support multiple inheritances through the class. Interfaces can obtain it in Java.
C++ supports pointers. You can write a program using a pointer in C++.Java supports the pointer internally and has restricted pointer support. Java does not allow to write pointer program.
C++ supports multiple inheritance.Java doesn’t support multiple inheritance through class. It can be achieved by interfaces in java.
C++ supports both calls by value and call by reference.Java supports call-by value only. There is no call by reference in Java.
C++ is an object-oriented language.Java is also an object-oriented language.
C++ supports structures and unions.Java doesn’t support structures and unions.
C++ doesn’t support documentation comment.Java supports documentation comment (/** … */) to create documentation for java source code.

3. What do you understand about JVM?

JVM stands for Java Virtual Machine. JVM is a virtual machine that enables the computer to run Java programs. Java code is compiled by the JVM into bytecode, which is machine-independent and as close to native code as possible.


4. What are the major differences between JDK, JRE, and JVM?

The difference between JDK, JRE, and JVM are:

JDK: JDK is the abbreviation for Java Development Kit. It is a software development environment used to develop Java applications and applets. It is physically present. It includes JRE + development tools.

JRE: JRE stands for Java Runtime Environment. It is an implementation of JVM. Java Runtime Environment is a set of software tools, which are used to develop Java applications. It is used to provide the runtime environment. It contains a set of libraries + other files that JVM uses at runtime.

JVM: JVM is an acronym for Java Virtual Machine; It is an abstract machine that provides the runtime environment in which Java bytecode can be executed. It is a specification that specifies the working of the Java Virtual Machine. Its implementation is provided by Oracle and other companies. Its implementation is known as JRE.


5. What are the different types of memory areas allocated by JVM?

Different types of memory areas allocated by JVM are:

  • Stack
  • Class
  • Program counter register
  • Native method stack
  • Heap

6. What is a Class in Java?

In Java, a class represents a defined common set of properties and methods for all objects of the same type. Generally, a class consists of components such as a modifier, class name, superclass, interface, and body. For real-time Java applications, several types of classes are used.

There are a few ways to create a class in Java:

  • New keywords
  • forName method
  • Clone () method

7. What is the platform?

A platform is the hardware or software environment in which a piece of software is executed. There are two types of platforms, software-based and hardware-based. Provides Java software-based platform.


8. Is the JVM platform independent?

No, JVM is not platform-independent as it is not written in Java.


9. What is the default value of the local variables?

Local variables are not initialized to any default value, neither primitives nor object references.


10. Can you declare the main() method as final?

Yes, we can declare the main() method as final in Java.


11. What is object cloning?

Object cloning is a method of creating an exact copy of an object. A new instance of the class of the current object is created. All its fields are initialized with the contents of the corresponding fields of this object. clone() is defined in the Object class.


12. What is the output of the following Java program?

class Test   
{  
    public static void main (String args[])   
    {  
        for(int i=0; 0; i++)   
        {  
            System.out.println("Hello World");  
        }  
    }  
}  

The above code will give the compile-time error because the for loop demands a boolean value in the second part and we are providing an integer value, i.e., 0.

You May Also Like :   C# Interview Questions and Answers

13. What is an object?

An object is a real-time entity with some state and behavior. In Java, an object is an instance of a class that has instance variables as the state of the object and methods as the behavior of the object. An object of a class can be created using the new keyword.


14. What will be the initial value of an object reference which is defined as an instance variable?

All object references are initialized to null in Java.


15. What is a constructor?

A constructor is a block of code that enables you to create an object of a class. It is called automatically when a new instance of an object is created.


16. What is the purpose of a default constructor?

The purpose of default constructor is to assign default values to the objects. If the class does not have a constructor, the Java compiler creates a default constructor. For example:

class Student3{  
int id;  
String name;  
  
void display(){System.out.println(id+" "+name);}  
  
public static void main(String args[]){  
Student3 s1=new Student3();  
Student3 s2=new Student3();  
s1.display();  
s2.display();  
}  
}  

Output:

0 null
0 null

17. Can you make a constructor final?

No, the constructor can’t be final.


18. What are the differences between the constructors and methods?

Following are the difference between constructors and methods:

Java ConstructorJava Method
A constructor is used to initialize the state of an object.A method is used to expose the behavior of an object.
The constructor is invoked implicitly.The method is invoked explicitly.
The constructor name must be same as the class name.The method name may or may not be same as class name.
A constructor must not have a return type.A method must have a return type.
The Java compiler provides a default constructor if you don’t have any constructor in a class.The method is not provided by the compiler in any case.

19. What is the output of the following Java program?

class Test   
{  
    int i;   
}  
public class Main   
{  
    public static void main (String args[])   
    {  
        Test test = new Test();   
        System.out.println(test.i);  
    }  
}  

The output of the program is 0 because the variable i is internally initialized to 0. As we know that if there is no constructor in the class then the default constructor is implicitly invoked, the variable i is initialized to 0 because there is no constructor in the class.


20. Is String a data type in java?

String is not a primitive data type in Java. When a string is created in Java, it is actually an object of the Java.Lang.String class that is created. After this String object is constructed, all the built-in methods of the String class can be used on the String object.


21. Why Strings in Java are called as Immutable?

In Java, string objects are said to be immutable because once a string is assigned a value, it cannot be changed and if changed, a new object is created.

In below example, reference str refers to a string object having value “Value one”.

String str="Value One";

When a new value is assigned to it, a new String object gets created and the reference is moved to the new object.

str="New Value";

22. When a lot of changes are required in data, which one should be a preference to be used? String or StringBuffer?

Since StringBuffers are dynamic in nature and we can change the values of StringBuffer objects unlike String which is immutable, using StringBuffer is always a good option when the data is going to be changed a lot. If we use String in such a case, a new String object will be created for every data change which will be an additional overhead.


23. How can we make copy of a java object?

We can use the concept of cloning to make a copy of an object. Using clone, we create copies of an object with the actual state.

You May Also Like :   C Programming Interview Questions and Answers

clone() is a method of the Cloneable interface and hence, it needs to implement the Cloneable interface in order to create an object copy.


24. What is a Java servlet?

Java developers use servlets when they need to extend the capabilities of a server. Servlets are used to extend web server hosted applications and are deployed to design dynamic web pages using Java. These servlets run on JVM and resist attacks. Servlets are portable, unlike CGI (Common Gateway Interface).


25. For multiple requests, how many objects of a servlet are created?

The servlet creates only one object at the time of the first request, regardless of how many incoming requests are received.


26. What is a JSP in Java and are they better than servlets?

Java Server Pages (JSP) is a server-side technology used to build applications, dynamic web content, and stand-alone web pages.

Yes, JSP is a better technology than servlets as they are very easy to maintain. Also, it does not require recompilation or redistribution of any kind. JSP, being an extension of Servlets, mainly incorporates most of its features.


27. Explain the Collection Framework in Java.

The Java Collection Framework provides an architecture to store and manage a set of objects. It defines a number of algorithms that can be applied to sets and maps. It enables developers to access pre-designed data structures as well as algorithms to manipulate data.

The Java Collection Framework includes:

  • Interfaces
  • Classes
  • Algorithm

28. What is the difference between Concurrent Collections and Synchronized Collections?

Concurrent collection and synchronized collection are used for thread safety. The difference between them is how they achieve thread-safety by their scalability, performance. The performance of concurrent collection is better than synchronized collection because it holds a single part of the map to achieve concurrency.


29. Can a class in Java be inherited from more than one class?

In Java, a class can only be derived from one class, not multiple classes. Multiple inheritance is not supported by Java.


30. What is a static keyword?

A static keyword is used for memory management to refer to a common property of all objects. If we implement Java static keyword with any method, then it is called static method. Static can be used for nested classes with any other class, initialization block, procedure and variables.


31. Can you override the static method?

No, a static method cannot be overridden as they are resolved at compile-time and not at runtime.


32. What if the static modifier is removed from the signature of the main method?

If the static modifier is removed from the signature of the main method, the program compiles. But, at runtime, it throws “NoSuchMethodError” error.


33. How THIS keyword is used in the class?

Following are the uses of this keyword in the class:

  • Can be used as an argument in the method call.
  • It is used as an argument in the constructor call.
  • Can be used to assign current class methods.
  • It can be used to return the existing class instance from the method.
  • It is used to refer to the existing class instance variable.

34. Why Java doesn’t support multiple inheritances yet multiple interface implementation is possible?

Multiple inheritance refers to the process by which one inherits the properties and behavior of multiple classes in a single class like in C++. But because of Java being a simple, robust and safe language, it skips multiple inheritances which is commonly called as Diamond Problem.

In Java 8, it supports default methods and thus a class can proceed with the implementation of two or more interfaces.


35. How to restrict inheritance for a class?

We can restrict inheritance for a class by:

  • Using the final keyword
  • Using the Javadoc comment (” // “)
  • Making a constructor private
  • Making all methods final, so that we cannot override them

36. Is it possible to override private methods?

No, it is not possible to override private methods because the scope of private methods is limited to the class, and we cannot access these methods outside the class.


37. What do you mean by method overriding?

If the child class has the same method declared in the parent class, then it is called method overriding. It allows a subclass or subclasses to provide a specific implementation of a method that is already provided by one of its super-classes or superclasses.


38. What do you understand by thread-safe?

It is the only program that behaves correctly when multiple threads are using the resource simultaneously.


39. Explain multithreading in Java.

Multithreading is a programming feature that allows multiple tasks to run simultaneously in a single program. In simple words, it enables the concurrent execution of two or more parts of the program for optimum utilization of CPU. Each program is called a thread. In Java, threads are lightweight processes within a process. They run in parallel and help in improving the performance of any program.


40. Explain JSON.

JSON stands for JavaScript Object Notation. It is a lightweight data-interchange format used as an alternative to XML. It is language-independent and easy to understand. JSON uses JavaScript syntax, but its format is text only. Text can be read and used as a data format by any programming language.


We hope these Java Interview Questions help you in cracking your next programmer job Interview.

All The Best!

5/5 - (1 vote)
Scroll to Top