100+ Java Interview Questions and Answers for Freshers & Experienced-1

Written by - Deepak Prasad
Topics we will cover hide
Java Interview Questions and Answers

This is a two part article where I will share 100+ Java Interview Questions and Answers for Freshers & Experienced professionals and users.

100+ Java Interview Questions and Answers for Freshers & Experienced-1


Java Interview Questions and Answers

Let us start with the java interview questions and answers


1. What is the difference between JDK and JRE?

  • JDK stands for Java Development Kit. It contains the tools and libraries for development of Java programs. It also contains compilers and debuggers needed to compile Java program
  • JRE stands for Java Runtime Environment. This is included in JDK. JRE provides libraries and JVM that is required to run a Java program.


2. What is Java Virtual Machine (JVM)?

Java Virtual Machine (JVM) is an abstract machine that executes Java Bytecode. There are different JVM for different hardware and software platforms. So JVM is platform dependent. JVM is responsible for loading, verifying and executing the Bytecode on a platform.


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

In java, JVM allocates memory to different processes, methods and objects. Some of the memory areas allocated by JVM are:

  1. ClassLoader: It is a component of JVM used to load class files.
  2. Class (Method) Area: It stores per-class structures such as the runtime constant pool, field and method data, and the code for methods.
  3. Heap: Heap is created a runtime and it contains the runtime data area in which objects are allocated.
  4. Stack: Stack stores local variables and partial results at runtime. It also helps in method invocation and return value. Each thread creates a private JVM stack at the time of thread creation.
  5. Program Counter Register: This memory area contains the address of the Java virtual machine instruction that is currently being executed.
  6. Native Method Stack: This area is reserved for all the native methods used in the application.


4. What is JIT compiler?

Just In Time compiler also known as JIT compiler is used for performance improvement in Java. It is enabled by default. It is compilation done at execution time rather earlier. Java has popularized the use of JIT compiler by including it in JVM.


5. How Java platform is different from other platforms?

Java is a platform independent language. Java compiler converts Java code in to byte code that can be interpreted by JVM. There are JVM written for almost all the popular platforms in the world.
Java byte code can run on any supported platform in same way. Where as other languages require libraries compiled for a specific platform to run


6. Why people say that Java is 'write once and run anywhere' language?

  • You can write Java code on Windows and compile it in Windows platform. The class and jar files that you get from Windows platform can run as it is on Unix environment. So it is a truly
    platform independent language.
  • Behind all this portability is Java byte code. Byte code generated by Java compiler can be interpreted by any JVM. So it becomes much easier to write programs in Java and expect those to run on any platform.
  • Java compiler javac compiles java code and JVM java runs that code.


7. How does ClassLoader work in Java?

In Java, ClassLoader is a class that is used to load files in JVM. ClassLoader loads files from their physical file locations e.g. Filesystem, Network location etc.
There are three main types of ClassLoaders in Java.

  • Bootstrap ClassLoader: This is the first ClassLoader. It loads classes from rt.jar file.
  • Extension ClassLoader: It loads class files from jre/lib/ext location.
  • Application ClassLoader: This ClassLoader depends on CLASSPATH to find the location of class files. If you specify your jars in CLASSPATH, then this ClassLoader will load them.


8. Do you think ‘main’ used for main method is a keyword in Java?

No, main is just a name of method. There can be multiple methods with same name main in a class file. It is not a keyword in Java.


9. Can we write main method as public void static instead of public static void?

No, you cannot write it like this. Any method has to first specify the modifiers and then the return value. The order of modifiers can change.
We can write static public void main() instead of public static void main().


10.In Java, if we do not specify any value for local variables, then what will be the default value of the local variables?

Java does not initialize local variables with any default value. So these variables will be just null by default.


11. Let say, we run a java class without passing any arguments. What will be the value of String array of arguments in Main method?

By default, the value of String array of arguments is empty in Java. It is not null.


12. What is the difference between byte and char data types in Java?

  • Both byte and char are numeric data types in Java. They are used to represent numbers in a specific range.
  • Major difference between them is that a byte can store raw binary data where as a char stores characters or text data.
    Usage of char is E.g. char ch = ‘x’;
  • Byte values range from -128 to 127.
  • A byte is made of 8 bits. But a char is made of 16 bits. So it is equivalent to 2 bytes.


13.What are the main principles of Object Oriented Programming?

Main principles of Object Oriented Programming (OOPS) are:

  • Abstraction
  • Encapsulation
  • Inheritance
  • Polymorphism


14.What is the difference between Object Oriented Programming language and Object Based Programming language?

  • Object Oriented Programming languages like Java and C++ follow concepts of OOPS like- Encapsulation, Abstraction, Polymorphism and Inheritance etc.
  • Object Based Programming languages follow some features of OOPS but they do not provide support for Polymorphism and Inheritance. Egg. JavaScript, VBScript etc.
  • Object Based Programming languages provide support for Objects and you can build objects from constructor. They languages also support Encapsulation. These are also known as Prototype-oriented languages.


15.Why do we need constructor in Java?

  • Java is an object-oriented language, in which we create and use objects. A constructor is a piece of code similar to a method. It is used to create an object and set the initial state of the object.
  • A constructor is a special function that has same name as class name.
  • Without a constructor, there is no other way to create an object.
  • By default, Java provides a default constructor for every object. If we overload a constructor then we have to implement default constructor


16. What is a Thread in Java?

In Java, a thread is a lightweight process that runs within another process or thread. It is an independent path of execution in an application. Each thread runs in a separate stack frame.
By default Java starts one thread when the main method of a class is called.


17.Why do we need default constructor in Java classes?

  • Default constructor is the no-argument constructor that is automatically generated by Java if no other constructor is defined.
  • Java specification says that it will provide a default constructor if there is no overloaded constructor in a class. But it does not say anything about the scenario in which we write an overloaded constructor in a class.
  • We need at least one constructor to create an object, that’s why Java provides a default constructor.
  • When we have overloaded constructor, then Java assumes that we want some custom treatment in our code. Due to which it does not provide default constructor. But it needs default constructor as per the specification. So it gives error.


18. Why constructors cannot be final, static, or abstract in Java?

  • If we set a method as final it means we do not want any class to override it. But the constructor (as per Java Language Specification) cannot be overridden. So there is no use of marking it final.
  • If we set a method as abstract it means that it has no body and it should be implemented in a child class. But the constructor is called implicitly when the new keyword is used. Therefore it needs a body.
  • If we set a method as static it means that it belongs to the class, but not a particular object. The constructor is always called to initialize an object. Therefore, there is no use of marking constructor static.


19.What is the purpose of ‘this’ keyword in java?

  • In Java, ‘this’ keyword refers to current instance of the object.
  • It is useful for differentiating between instance variables and local variables.
  • It can be used to call constructors. Or it can be used to refer to the instance.
  • In case of method overriding, this is used for falling the method of current class.


20. Explain the concept of Inheritance?

  • Inheritance is an important concept in Object Oriented Programming. Some objects share certain characteristics and behavior. By using Inheritance, we can put the common behavior and characteristics in a base class which also known as super class. And then all the objects with common behavior inherit from this base class.
  • It is also represented by IS-A relationship.
  • Inheritance promotes, code reuse, method overriding and polymorphism


21. Why Java does not support multiple inheritance?

  • Multiple Inheritance means that a class can inherit behavior from two or more parent classes.
  • The issue with Multiple Inheritance is that both the parent classes may have different implementation for the same method. So they have different ways of doing the same thing. Now which implementation should the child class choose?
  • This leads to ambiguity in Multiple Inheritance. This is the main reason for Java not supporting Multiple Inheritance in implementation.
  • Lets say you have a class TV and another class AtomBomb. Both have method switchOn() but only TV has switchOff() method. If your class inherits from both these classes then you have an issue that you can switchOn() both parents, but switchOff will only switchOff() TV.
  • But you can implement multiple interfaces in Java


22. How aggregation and composition are different concepts?

  • In OOPS, Aggregation and Composition are the types of association relations. A composition is a strong relationship. If the composite object is destroyed, then all its parts are destroyed. E.g. A Car has a Steering Wheel. If Car object is destroyed, then there is no meaning of Steering Wheel.
  • In Aggregation, the relationship is weaker than Composition.
    E.g. A Library has students. If a Library is destroyed, Students still exist. So Library and Student are related by Aggregation. A Library has Books. If Library is destroyed, the Books are also destroyed. Books of a Library cannot exist without the Library. So Book and Library are related by Composition.


23. Why there are no pointers in Java?

  • In Java there are references instead of pointers. These references point to objects in memory. But there is no direct access to these memory locations. JVM is free to move the objects within VM memory.
  • The absence of pointers helps Java in managing memory and garbage collection effectively. Also it provides developers with convenience of not getting worried about memory allocation and deallocation.


24. If there are no pointers in Java, then why do we get NullPointerException?

  • In Java, the pointer equivalent is Object reference. When we use a . it points to object reference. So JVM uses pointers but programmers only see object references.
  • In case an object reference points to null object, and we try to access a method or member variable on it, then we get NullPointerException.


25. What is the purpose of ‘super’ keyword in java?

  • ‘super’ keyword is used in the methods or constructor of a child class. It refers to immediate parent class of an object.
  • By using ‘super’ we can call a method of parent class from the method of a child class.
  • We can also call the constructor of a parent class from the constructor of a child class by using ‘super’ keyword.


26. Is it possible to use this() and super() both in same constructor?

No, Java does not allow using both super() and this() in same constructor. As per Java specification, super() or this() must be the first statement in a constructor.


27. What is the meaning of object cloning in Java?

  • Object.clone() method is used for creating an exact copy of the object in Java. It acts like a copy constructor. It creates and returns a copy of the object, with the same class and with all the fields having same values as of the original object.
  • One disadvantage of cloning is that the return type is an Object. It has to be explicitly cast to actual type.


28. In Java, why do we use static variable?

Whenever we want to have a common property for all objects of a class, we use a class level variable i.e. a static variable.
This variable is loaded in memory only once at the time of class loading. So it saves memory, since it is not defined per object in Java.


29. Why it is not a good practice to create static variables in Java?

  • Static variables are common to all the objects of a class. If a new object is created, there is no need to test the value of static variable. Any code that uses static variable can be in any state.
  • It can be within a new object or at a class level. So the scope of static variable is open ended in a Java class.
  • If we want tighter control on scope, then variables should be created at the object creation level.
  • Also defining static variables is not a good practice because they go against the principles of Object Oriented Programming.


30. What is the purpose of static method in Java?

  • Java provides the feature of static method to create behavior at the class level. The static method is common to all the objects of a class. We do not need to create any object of a class to call a static method. So it provides convenience of not creating an object for calling it.
  • Also a static method can access and modify static data members. This also helps in keeping the behavior as well as state at the class level.


31. Why do we mark main method as static in Java?

  • The main method in Java is marked as static, so that JVM can call it to start the program. If main method is not static, then which constructor will be called by Java process?
  • As such it is a known as convention to mark main method static in Java. But if we remove the static, then there will be ambiguity. Java process may not know which method of a class to call to start the program.
  • So this convention helps in Java process to identify the starting code for a program in class that is passed as an argument to java process.


32. In what scenario do we use a static block?

  • At times, there is a class that has static member variables. These variables need some complicated initialization. At this time static block helps as a tool to initialize complex static member variable initialization.
  • The static block is executed even before the execution of main.
  • Sometimes, we can also replace static block with a static method of class


33. What happens when static modifier is not mentioned in the signature of main method?

  • As per Java specification, main method has to be marked as static. It needs only one argument that is an array of String.
  • A program can compile with a non-static method. But on execution it will give NoSuchMethodError.


34. What is the difference between static method and instance method in Java?

  • Often, there is a need to define a behavior for a class that is not dependent on member variables of an object. Such behavior is captured in a static method. If there is a behavior dependent upon the member variables of an object, then we do not mark it static, it remains as instance method.
  • To call as static method, we do not need to create an object. We just call it with class name. But to call an instance method, we need to create/get an object first.
    Instance member variables cannot be accessed by a static method. But an instance method can call both instance variables and static variables.


35. Are we allowed to override a static method in Java?

No. Java does not allow overriding a static method. If you create a static method with same name in subclass, then it is a new method, not an overridden method.


36. What is the difference between method overloading and method overriding in Java?

Differences between method overloading and overriding are:

  • Method overloading is static polymorphism. Method overriding is runtime polymorphism.
  • Method overloading occurs within the same class. Method overriding happens in two classes with hierarchy relationship.
  • Parameters must be different in method overloading. Parameters must be same in method overriding.
  • Method overloading is a compile time concept. Method overriding is a runtime concept.


37. What is meant by covariant return type in Java?

  • A covariant return type of a method is one that can be replaced by a "narrower" type when the method is overridden in a subclass.
  • Let say class B is child of class A. There is a get() method in class A as well as class B. get() method of class A can return an instance of A, and get() method of class B return an instance of B. Here class B overrides get() method, but the return type is different.
  • Before Java 5, any method that overrides the method of parent class would have same return type.
  • From Java 5 onwards, a child class can override a method of parent class and the child class method can return an object that is child of object return by parent class method.


38. What is Runtime Polymorphism?

  • Runtime Polymorphism or Dynamic Polymorphism is the polymorphism that exists at runtime. In case of method overriding it is not known which method will be called at runtime. Based on the type of object, JVM decides the exact method that should be called.
  • So at compile time it is not known which method will be called at run time.


39. Explain the difference between static and dynamic binding?

In Static binding references are resolved at compile time. In Dynamic binding references are resolved at Run time.

Person p = new Person();
p.walk(); // Java compiler resolves this binding at compile time.

public void walk(Object o){
((Person) o).walk(); // this is dynamic binding.


40. What is Abstraction in Object Oriented programming?

  • Abstraction is the process of hiding certain implementation details of an object and showing only essential features of the object to outside world.
  • It is different from Abstract class in Java.
  • Abstraction process identifies commonalities and hides the complexity of implementation. It helps us in focusing on the interface that we share with the outside world.


41. How is Abstraction different from Encapsulation?

Abstraction happens at class level design. It results in hiding the implementation details. Encapsulation is also known as “Information Hiding”. An example of encapsulation is marking the member variables private and providing getter and setter for these member variables.


42. What is an abstract class in Java?

  • An abstract class in Java has one or more abstract methods. An abstract method is just declared in the abstract class, but it is not implemented.
  • An abstract class has to be extended in Java and its abstract methods have to be implemented by a child class. Also Java does not allow new instance of Abstract class.


43. Is it allowed to mark a method abstract as well as final?

No. It will be contradictory statement to mark a method abstract as well as final.
An abstract method has to be overridden by a child class. And a final method cannot be overridden. Therefore a method can be either abstract or final in Java.


44. How Annotations are better than Marker Interfaces?

  • Annotations serve the purpose of conveying metadata about the class to its consumers without creating a separate type for it.
  • Annotations are more powerful than a Marker interface. They allow programmers to pass more sophisticated information to classes that "consume" it


45. What is the difference between abstract class and interface in Java?

Differences between Abstract class and Interface are as follows:

  • An abstract class can have implemented methods with body (non-abstract methods). Interface has only abstract methods. From Java 8 onwards, interface can have static/default methods in implemented form.
  • An abstract class can have instance member variables. An interface cannot have instance variables. It can only have constants.
  • An abstract class can have a constructor. Interface cannot have constructor. It has to be implemented by another class.
  • A class can extend only one abstract class. A class can implement more than one interface.


46. How can we cast to an object reference to an interface reference?

An Object that implements an Interface can be cast to the same Interface. Since An Object implementing an Interface already provides implementation for the methods of that Interface, it is allowed to do so as per the rules of Inheritance


47. What is the purpose of package in Java?

  • A package is used to encapsulate a group of classes, interfaces and sub-packages. Often, it is a hierarchical structure of storing information. It is easier to organize the related classes and subpackages in this manner.
  • A Package also provides access protection for classes and interfaces. A package also helps in removing naming collision.


48. What is java.lang package?

  • In Java, java.lang package contains the classes that are fundamental to the design of Java programming language. The most important class in this package is Object class.
  • It also contains wrapper classes like- Integer, Boolean, Character etc. It provides Math class for mathematical operations.


49. Which is the most important class in Java?

It is an open-ended question with many answers. In my view, Object class is the most important class of Java programming language. It is the root of all the classes in Java. It provides some very important and fundamental methods.


50. Can you import same package or class twice in your class?

  • If we import same package multiple times in a class, compiler includes it only once. So neither JVM nor Compiler gives any error/warning on including a package multiple times.
  • If you have two classes with same name, then you may get name collision on importing the class erroneously.
  • JVM internally loads the class only one time.


I will continue this article with more 50+ Java Interview Questions and Answers for Freshers & Experienced-2

Deepak Prasad

He is the founder of GoLinuxCloud and brings over a decade of expertise in Linux, Python, Go, Laravel, DevOps, Kubernetes, Git, Shell scripting, OpenShift, AWS, Networking, and Security. With extensive experience, he excels in various domains, from development to DevOps, Networking, and Security, ensuring robust and efficient solutions for diverse projects. You can reach out to him on his LinkedIn profile or join on Facebook page.

Can't find what you're searching for? Let us assist you.

Enter your query below, and we'll provide instant results tailored to your needs.

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can send mail to admin@golinuxcloud.com

Thank You for your support!!

Leave a Comment