Java Interview Questions

Do you have what it takes to ace a Java Interview? We are here to help you in consolidating your knowledge and concepts in Java. Before we begin, let's understand what Java is all about.

What is Java?

Join our community and share your recent Java interview experiences.

Java Interview Questions for Freshers

1. Why is Java a platform independent language?

Java language was developed so that it does not depend on any hardware or software because the compiler compiles the code and then converts it to platform-independent byte code which can be run on multiple systems.

Create a free personalised study plan Create a FREE custom study plan Get into your dream companies with expert guidance Get into your dream companies with expert.. Real-Life Problems Prep for Target Roles Custom Plan Duration Flexible Plans

2. Why is Java not a pure object oriented language?

Java supports primitive data types - byte, boolean, char, short, int, float, long, and double and hence it is not a pure object oriented language.

3. Difference between Heap and Stack Memory in java. And how java utilizes this.

Stack memory is the portion of memory that was assigned to every individual program. And it was fixed. On the other hand, Heap memory is the portion that was not allocated to the java program but it will be available for use by the java program when it is required, mostly during the runtime of the program.

Java Utilizes this memory as -

Example- Consider the below java program:

class Main < public void printArray(int[] array)< for(int i : array) System.out.println(i); > public static void main(String args[]) < int[] array = new int[10]; printArray(array); > >

For this java program. The stack and heap memory occupied by java is -

Main and PrintArray is the method that will be available in the stack area and as well as the variables declared that will also be in the stack area.

And the Object (Integer Array of size 10) we have created, will be available in the Heap area because that space will be allocated to the program during runtime.

You can download a PDF version of Java Interview Questions. Download PDF Download PDF

Download PDF

Your requested download is ready!
Click here to download.

4. Can java be said to be the complete object-oriented programming language?

It is not wrong if we claim that Java is the complete object-oriented programming language because everything in Java is under the classes and we can access them by creating the objects.

But we can even say that Java is not a completely object-oriented programming language because it has the support of primitive data types like int, float, char, boolean, double, etc.

Now for the question: Is Java a completely object-oriented programming language? We can say that - Java is not a pure object-oriented programming language, because it has direct access to primitive data types. And these primitive data types don't directly belong to the Integer classes.

5. How is Java different from C++?

Learn via our Video Courses

6. Pointers are used in C/ C++. Why does Java not make use of pointers?

Pointers are quite complicated and unsafe to use by beginner programmers. Java focuses on code simplicity, and the usage of pointers can make it challenging. Pointer utilization can also cause potential errors. Moreover, security is also compromised if pointers are used because the users can directly access memory with the help of pointers.

Thus, a certain level of abstraction is furnished by not including pointers in Java. Moreover, the usage of pointers can make the procedure of garbage collection quite slow and erroneous. Java makes use of references as these cannot be manipulated, unlike pointers.

7. What do you understand by an instance variable and a local variable?

Instance variables are those variables that are accessible by all the methods in the class. They are declared outside the methods and inside the class. These variables describe the properties of an object and remain bound to it at any cost.

All the objects of the class will have their copy of the variables for utilization. If any modification is done on these variables, then only that instance will be impacted by it, and all other class instances continue to remain unaffected.

Example:

class Athlete < public String athleteName; public double athleteSpeed; public int athleteAge; >

Local variables are those variables present within a block, function, or constructor and can be accessed only inside them. The utilization of the variable is restricted to the block scope. Whenever a local variable is declared inside a method, the other class methods don’t have any knowledge about the local variable.

Example:

public void athlete() < String athleteName; double athleteSpeed; int athleteAge; >

Advance your career with Mock Assessments Refine your coding skills with Mock Assessments Real-world coding challenges for top company interviews Real-world coding challenges for top companies Real-Life Problems Detailed reports

8. What are the default values assigned to variables and instances in java?

9. What do you mean by data encapsulation?

10. Tell us something about JIT compiler.

11. Can you tell the difference between equals() method and equality operator (==) in Java?

We are already aware of the (==) equals operator. That we have used this to compare the equality of the values. But when we talk about the terms of object-oriented programming, we deal with the values in the form of objects. And this object may contain multiple types of data. So using the (==) operator does not work in this case. So we need to go with the .equals() method.

Both [(==) and .equals()] primary functionalities are to compare the values, but the secondary functionality is different.

So in order to understand this better, let’s consider this with the example -

String str1 = "InterviewBit"; String str2 = "InterviewBit"; System.out.println(str1 == str2);

This code will print true. We know that both strings are equals so it will print true. But here (==) Operators don’t compare each character in this case. It compares the memory location. And because the string uses the constant pool for storing the values in the memory, both str1 and str2 are stored at the same memory location. See the detailed Explanation in Question no 73: Link.

Now, if we modify the program a little bit with -

String str1 = new String("InterviewBit"); String str2 = "InterviewBit"; System.out.println(str1 == str2);

Then in this case, it will print false. Because here no longer the constant pool concepts are used. Here, new memory is allocated. So here the memory address is different, therefore ( == ) Operator returns false. But the twist is that the values are the same in both strings. So how to compare the values? Here the .equals() method is used.

.equals() method compares the values and returns the result accordingly. If we modify the above code with -

System.out.println(str1.equals(str2));

Then it returns true.

equals() ==
This is a method defined in the Object class. It is a binary operator in Java.
The .equals() Method is present in the Object class, so we can override our custom .equals() method in the custom class, for objects comparison. It cannot be modified. They always compare the HashCode.
This method is used for checking the equality of contents between two objects as per the specified business logic. This operator is used for comparing addresses (or references), i.e checks if both the objects are pointing to the same memory location.

Note:

12. How is an infinite loop declared in Java?

Infinite loops are those loops that run infinitely without any breaking conditions. Some examples of consciously declaring infinite loop is:

for (;;) < // Business logic // Any break logic >
while(true)< // Business logic // Any break logic >
do< // Business logic // Any break logic >while(true);

13. Briefly explain the concept of constructor overloading

Constructor overloading is the process of creating multiple constructors in the class consisting of the same name with a difference in the constructor parameters. Depending upon the number of parameters and their corresponding types, distinguishing of the different types of constructors is done by the compiler.

class Hospital < int variable1, variable2; double variable3; public Hospital(int doctors, int nurses)  < variable1 = doctors; variable2 = nurses; >public Hospital(int doctors)  < variable1 = doctors; >public Hospital(double salaries)  < variable3 = salaries >>

Three constructors are defined here but they differ on the basis of parameter type and their numbers.

14. Define Copy constructor in java.

Copy Constructor is the constructor used when we want to initialize the value to the new object from the old object of the same class.

class InterviewBit< String department; String service; InterviewBit(InterviewBit ib)< this.departments = ib.departments; this.services = ib.services; > >

Here we are initializing the new object value from the old object value in the constructor. Although, this can also be achieved with the help of object cloning.

15. Can the main method be Overloaded?

Yes, It is possible to overload the main method. We can create as many overloaded main methods we want. However, JVM has a predefined calling method that JVM will only call the main method with the definition of -

public static void main(string[] args)
Consider the below code snippets:
class Main < public static void main(String args[]) < System.out.println(" Main Method"); > public static void main(int[] args)< System.out.println("Overloaded Integer array Main Method"); > public static void main(char[] args)< System.out.println("Overloaded Character array Main Method"); > public static void main(double[] args)< System.out.println("Overloaded Double array Main Method"); > public static void main(float args)< System.out.println("Overloaded float Main Method"); > >

16. Comment on method overloading and overriding by citing relevant examples.

In Java, method overloading is made possible by introducing different methods in the same class consisting of the same name. Still, all the functions differ in the number or type of parameters. It takes place inside a class and enhances program readability. The only difference in the return type of the method does not promote method overloading. The following example will furnish you with a clear picture of it.

class OverloadingHelp < public int findarea (int l, int b) < int var1; var1 = l * b; return var1; > public int findarea (int l, int b, int h) < int var2; var2 = l * b * h; return var2; > >

Both the functions have the same name but differ in the number of arguments. The first method calculates the area of the rectangle, whereas the second method calculates the area of a cuboid. Method overriding is the concept in which two methods having the same method signature are present in two different classes in which an inheritance relationship is present. A particular method implementation (already present in the base class) is possible for the derived class by using method overriding.
Let’s give a look at this example:

class HumanBeing < public int walk (int distance, int time) < int speed = distance / time; return speed; > > class Athlete extends HumanBeing < public int walk(int distance, int time) < int speed = distance / time; speed = speed * 2; return speed; > >

Both class methods have the name walk and the same parameters, distance, and time. If the derived class method is called, then the base class method walk gets overridden by that of the derived class.

17. A single try block and multiple catch blocks can co-exist in a Java Program. Explain.

Yes, multiple catch blocks can exist but specific approaches should come prior to the general approach because only the first catch block satisfying the catch condition is executed. The given code illustrates the same:

public class MultipleCatch < public static void main(String args[]) < try < int n = 1000, x = 0; int arr[] = new int[n]; for (int i = 0; i > catch (ArrayIndexOutOfBoundsException exception) < System.out.println("1st block = ArrayIndexOutOfBoundsException"); > catch (ArithmeticException exception) < System.out.println("2nd block = ArithmeticException"); > catch (Exception exception) < System.out.println("3rd block = Exception"); > > >

Here, the second catch block will be executed because of division by 0 (i / x). In case x was greater than 0 then the first catch block will execute because for loop runs till i = n and array index are till n-1.

18. Explain the use of final keyword in variable, method and class.