Java Interview Questions and Answers



There are four principle concepts-Abstraction,Polymorphism,Inheritance and Encapsulation
Abstraction refers to the act of representing essential features without including the background details or explanations.
Encapsulation is a technique used for hiding the properties and behaviors of an object and allowing outside access only as appropriate. It prevents other objects from directly altering or accessing the properties or methods of the encapsulated object.
Inheritance is a mechanism wherein a new class is derived from an existing class. In Java, classes may inherit or acquire the properties and methods of other classes.
JVM stands for Java Virtual Machine and it makes Java a Portable and a Platform Independent Language. JVM is called Virtual as it is an Abstract System that executes Compiled Java Programs. JVM executes the Java ByteCode File
Object-Oriented – java is based on object-oriented programming where the class and methods describe about the state and behavior of object. Portable – Java program gets converted into Java Byte Codes that can be executed on any platform without any dependency. Platform independent – java works on “write once and run anywhere” as it supports multiple platforms like Windows, Linux, Mac, Sun Solaris, etc. Robust – Java has a strong memory management as there is no pointer allocations. It has automatic garbage collection that prohibits memory leaks. Interpreted – java compiler converts the codes into Java Byte Codes which are then interpreted and executed by Java Interpreter.
Java encapsulates the codes in various classes which define new data types. These new data types are used to create objects.
JVM stands for Java Virtual Machine which provides runtime environment for Java Byte Codes to be executed., JRE (Java Runtime Environment) that includes sets of files required by JVM during runtime., JDK (Java Development Kit) consists of JRE along with the development tools required to write and execute a program.
Object is an unique entity,which contains data and functions(characteristics and behaviour) together in an Object Oriented Programming(OOP) language.
Yes, We can execute any code, even before the main method. We are using a static block of code in the class when creating the objects at load time of class. Any statements within this static block of code will get executed one time while loading the class, even before the creation of objects in the main method.
There are two types of polymorphism one is Compile time polymorphism and the other is run time polymorphism. Compile time polymorphism is method overloading. Runtime time polymorphism is done using inheritance and interface.
In Java, runtime polymorphism or dynamic method dispatch is a process in which a call to an overridden method is resolved at runtime rather than at compile-time. In this process, an overridden method is called through the reference variable of a superclass. The determination of the method to be called is based on the object being referred to by the reference variable.
It is the Java Debugger. Jdb basically helps you out to find Java Bugs. It is a Command Line Debugger Tool for Java Classes. It provides inspection and debugging of Local and Remote Java Virtual Machine.
Binding refers to the linking of a procedure call to the code to be executed in response to the call. Dynamic binding (also known as late binding) means that the code associated with a given procedure call is not known until the time of the call at run-time. It is associated with polymorphism and inheritance.
Method Overloading is creating a method with the same name as an existing method in a class. Hence in simple words, method overloading allows us to have multiple versions of a method within a class.
Java Enterprise Edition(J2EE): Used to Develop Server-Side applications such as Java Servlets and Java Server Pages., Java Micro Edition(J2ME): Used to develop applications for Mobile Devices such as Cell Phones, Tablets., Java Standard Edition(J2SE): Develop Client Side Applets or Standalone Applications.
Method overriding occurs when sub class declares a method that has the same type arguments as a method declared by one of its superclass. The key benefit of overriding is the ability to define behavior that’s specific to a particular subclass type.
Overloading occurs when two or more methods in one class have the same method name but different parameters. while Overriding means having two methods with the same method name and parameters (i.e., method signature).
We can restrict inheritance for class - By using final keyword, If we make all method final, then we cannot override that., By using private constructors,By using Javadoc comment (//)
There are times when a Function needs to refer to an Object that is invoked by it. This keyword facilitates reference to an Object within that particular methods. This keyword can be referenced to an object of current class type.
Java doesn’t allow multiple inheritance to avoid the ambiguity caused by it. One of the example of such problem is the diamond problem that occurs in multiple inheritance.
Single Inheritance, Hierarchical Inheritance, Multi-Level Inheritance, Multiple Inheritance
Default Constructor: This is a constructor without any parameters. A Default constructor is automatically created if you do not define any constructor for a Class., Parameterized Constructor: A Constructor invoked with parameters is called a Parameterized constructor., Overloading Constructor: Java allows Polymorphism i.e., a Method Overloading can be achieved by defining Two or more methods with the same name in a class.
Static methods in Java are used to create Methods or Functions that exists independent of Instances created for a Class. These methods doesn’t use any instance variables of any object of the class that they are defined in. Static methods can only access static data. They cannot call any other methods except Static Methods. Static Methods can not refer to This or Super Keywords.
There are times when a Function needs to refer to an Object that is invoked by it. This keyword facilitates reference to an Object within that particular methods. This keyword can be referenced to an object of current class type.
This operator is used to Reference Objects Variables. It tests if the first operand is an instance of its second. Its written as:(Object Reference Variable) instanceOf (Class/ Interface Type)
Compile Time Errors-This includes primarily Syntax Errors indicated by Java Compiler at Compile Time.,Run Time Errors-This includes primarily the Logical Errors that are not catched by the Java Compiler by may result into Abnormal Termination of the Program. It includes Divide by Zero Error, Opening a File that doesn’t exist, etc.
Abstract classes are classes that contain one or more abstract methods. An abstract method is a method that is declared, but contains no implementation.
An abstract class can never be instantiated. Its sole purpose is to be extended (subclassed).
Yes, other nonabstract methods can access a method that you declare as abstract.
Yes, there can be an abstract class without abstract methods.
We can’t have more than one public class in a single java source file. A single source file can have multiple classes that are not public.
Java does not support multiple inheritance. The reason being that multiple inheritance causes ambiguity and so, Java doesn’t use it. Diamond problem helps in understanding this scenario.However, multiple inheritance in Java can be achieved by using interfaces. Even if several number of interfaces in a class with same method, there is no sign of ambiguity as methods in an interface are always abstract.
An interface in java is a blueprint of a class. It has static constants and abstract methods.The interface in java is a mechanism to achieve abstraction. There can be only abstract methods in the java interface not method body. It is used to achieve abstraction and multiple inheritance in Java.
You can’t instantiate an interface directly, but you can instantiate a class that implements an interface.
Yes, it is always necessary to create an object implementation for an interface. Interfaces cannot be instantiated in their own right, so you must write a class that implements the interface and fulfill all the methods defined in it.
Interfaces may have member variables, but these are implicitly public, static, and final- in other words, interfaces can declare only constants, not instance variables that are available to all implementations and may be used as key references for method arguments for example.
Only public and abstract modifiers are allowed for methods in interfaces.
Yes, we can have multiple methods with name “main” in a single class. However if we run the class, java runtime environment will look for main method with syntax as public static void main(String args[]).





You may also like: