StuffArrow is under maintenance. These are the demo deals and offers you are seeing here. We will come up with fresh and live deals and offers soon. Thanks for your patience. Keep visit our site for updates

Java Interview Questions and Answers

Here we will publish the Java Interview questions and answers. These java questions are majorly asked in interview.

  • What is Java?
  • What is an object in java?
  • What is the class in java?
  • How Java is platform independent?
  • What is JVM?
  • Difference between JVM, JRE and JDK?
  • Is JVM is platform Independent?
  • What is OOPs? What are the features of OOPS?
  • What is inheritance in java?
  • What is polymorphism in java?
  • What is method overloading in java? Or static polymorphism.
  • What is method overriding in java? Or dynamic polymorphism.

What is Java?

Java is Object Oriented Programming Language. Its Open source language and very popular nowadays.

 

What is an object in java?

An Object is a thing which has state and behavior, e.g. a Car, pen, table, employee etc.  object is the instance of the class.

 

What is the class in java?

In simple term Class is just a design plan to create object in java. Object can be created through class only.

 

What is JVM?

JVM is Java Virtual Machine which provide environment to run java programs. JVM is platform dependent.

 

How Java is platform independent?

Platform Independent means we can run same program into different-different Platform. When we run the .java file, JVM create the .class file for the same file, now this .class file can be run on any platform. That’s why it is called that java is Platform independent, but be careful here that JVM is not platform independent, its platform dependent.

 

Difference between JVM, JRE and JDK?

JVM: JVM is java virtual machine that helps to run the java byte code (.class file).

JRE: As per Oracle documentation, JRE provides the libraries, the Java Virtual Machine, and other components to run java applications.

JDK: JDK is java Development kit which contain JRE and other tools.

 

Is JVM is platform Independent?

No, JVM is not platform Independent. Different vendors have different specification for JVM.

 

What is OOPs? What are the features of OOPS?

OOPS means Object Oriented Programming language. OOPS contains some features which an object oriented language should adhere. Following are the features:

 

What is inheritance in java?

Inheritance is one of the feature of OOPS, where child class inherit the properties of its base(parent) class. Suppose P is parent class and C is its child class, then all the properties of P will be automatically available to its child class C.

 

What is polymorphism in java?

Poly means many and morphism means many, so polymorphism means many form. It’s the feature of OOOPS where one object performs differently in different conditions. There are two types of polymorphism. Compile time Polymorphism and Run time polymorphism.

 

What is method overloading in java? Or static polymorphism.

Method overloading is the feature where one class can have multiple methods with same name but different no. of parameters. e.g.

 

Class A {

Public void m1();

Public void m1(int a);

Public void m1(int a,int, b);

}

 

 

In this example class A has 3 methods same name m1, but having 3 different types of argument, so this is example of method overloading.

 

What is method overriding in java? Or dynamic polymorphism.

Method Overriding is the feature where child class method overrides the base class method, if both the methods are same. e.g.

Class P {

public void test() {

}

}

Class C extends P {

public void test() {

}

}

Class Test {

C obj=new C();

Obj.test(); // this will call the child class method

}

 

How many ways an object can be created in Java?

Suppose we have class Employee and we have to create object of Employee class

package  com.stuffarrow
Class Employee {
int id;
String name;
}

There are following different ways to create objects in java:

  • new keyword: this is very familiar and commonly used way to create the object.
Employee emp = new Employee();

 

  • forName() : this is another way to create the java object. In this, class name should be known.
Employee object = (Employee) Class.forName("com.stuffarrow.Employee").newInstance();

 

  • Using clone(): we can use clone() to create the duplicate object of some other object which is already created.
Employee emp1 = new Employee();
Employee emp2 = (Employee) emp1.clone();

Here emp1 is already created and we are cloning emp1 to create new object emp2.

  • By Deserialization: Deserialization is the process of getting back the object which was saved during serialization. Deserialization also create new object when we try to fetch the object which was saved during serialization.
ObjectInputStream inStream = new ObjectInputStream(anInputStream );
Employee emp3 = (Employee) inStream.readObject();

 

 

Newsletter

%d bloggers like this: