Java Interview Questions and Answers


Answer. There are four principle concepts-Abstraction,Polymorphism,Inheritance and Encapsulation.

Answer. Abstraction refers to the act of representing essential features without including the background details or explanations.

Answer. 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.

Answer. 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.

Answer. 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.

Answer.

  • 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.

Answer. Java encapsulates the codes in various classes which define new data types. These new data types are used to create objects.

Answer.

  • 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.

Answer. Object is an unique entity,which contains data and functions(characteristics and behaviour) together in an Object Oriented Programming(OOP) language.

Answer. 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.

Answer. 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.>

Answer. 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.

Answer. 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.

Answer. 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.

Answer. 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.

Answer.

  • 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.

Answer. 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.

Answer. 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).

Answer. 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 (//)

Answer. 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.

Answer. 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.

Answer. Single Inheritance, Hierarchical Inheritance, Multi-Level Inheritance, Multiple Inheritance.

Answer.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.

Answer. 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.

Answer.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).

Answer.

  • 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.

Answer. Abstract classes are classes that contain one or more abstract methods. An abstract method is a method that is declared, but contains no implementation.

Answer.An abstract class can never be instantiated. Its sole purpose is to be extended (subclassed).

Answer. Yes, other nonabstract methods can access a method that you declare as abstract.

Answer. Yes, there can be an abstract class without abstract methods.

Answer. 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.

Answer. 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.

Answer. 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.

Answer. You can’t instantiate an interface directly, but you can instantiate a class that implements an interface.

Answer. 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.

Answer. 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.

Answer. Only public and abstract modifiers are allowed for methods in interfaces.

Answer. 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[]).