Java Interview Questions

Java Interview Questions

  1. Tell me something about java?

Ans . Java is an object-oriented programming language. It was originally developed by James Gosling at Sun Microsystems and released in 1995 as a core component of Sun Microsystems’ Java platform.

  1. Enlist some of the important features of java?

Ans .

  • platform independent programming language
  • Polymorphism
  • Inheritance
  • Package
  • Interface
  • Abstract class
  • similar to C++ in syntax
  • automatic type checking,
  • automatic garbage collection,
  • simplifies pointers; no directly accessible pointer to memory,
  • simplified network access,
  • multi-threading!
  1. What is JVM?

Ans . Java Virtual Machine (JVM) is a virtual machine that resides in the computer and the machine. JVM executes the byte code generated by compiler and produce output. JVM is the one that makes java platform independent. The main function of JVM is to execute the byte code produced by compiler.

  1. What is JRE?

Ans . JRE (Java Runtime Environment) is the environment within which the java virtual machine runs.

  1. What is JDK?

Ans . JDK is Java Development Kit which is a superset of JRE, it contains everything that JRE has along with development tools such as compiler, debugger etc.

  1. Why Java is called as a platform independent?

Ans . Because of its byte codes which can run on any system irrespective of its underlying operating system.

  1. What is javac?

Ans . It is java compiler

  1. What are the various types of Operator supported by Java?

Ans .

  • Arithmetic Operators
  • Assignment Operators
  • Auto-increment and Auto-decrement Operators
  • Logical Operators
  • Comparison (relational) operators
  • Bitwise Operators
  • Ternary Operator
  1. What are the Data Types supported by Java?

Ans .

  • byte
  • short
  • int
  • long
  • float
  • double
  • boolean
  • char
  1. What is the use static data variable?

Ans . If the variable is declared as static then only one copy of that variable is created for the entire class.

  1. Is sizeof a keyword?

Ans . The sizeof operator is not a keyword.

  1. How would you make a copy of an entire Java object with its state?

Ans . Have this class implement Cloneable interface and call its method clone().

  1. What is Function Overriding and Overloading in Java?

Ans . Method overloading in Java occurs when two or more methods in the same class have the exact same name, but different parameters.

Whereas, method overriding is defined as the case when a child class redefines the same method as a parent class.  Overridden methods must have the same name, argument list, and return type.

  1. What is a Constructor in java?

Ans . A constructor gets invoked when a new object is created. Every class has a constructor.

  1. What is mean by polymorphisms and types of polymorphism explain?

Ans . An ability to take more than one form is called as polymorphism.

There are two types of polymorphism in java:

1) Static Polymorphism also known as compile time polymorphism

2) Dynamic Polymorphism also known as runtime polymorphism

  1. Explain dynamic polymorphism?

Ans . Polymorphism that is handled during run time is known as dynamic polymorphism. Method overriding is an example of dynamic polymorphism.

Example is as follows

class Param{

public void myMethod(){

System.out.println(“Overridden Method”);

}

}

public class Nitin extends Param{

public void myMethod(){

System.out.println(“Overriding Method”);

}

public static void main(String args[]){

ABC obj = new Nitin();

obj.myMethod();

}

}

O/P: Overriding Method

  1. Explain static polymorphism?

Ans . Polymorphism that is handled during compiler time is known as static polymorphism. Method overloading is an example of compile time polymorphism.

Example is as follows

class Sum

{

int add(int a, int b)

{

return a+b;

}

int  add(int a, int b, int c)

{

return a+b+c;

}

}

public class Test

{

public static void main(String args[])

{

Sum obj = new Sum();

System.out.println(obj.add(20, 10));

System.out.println(obj.add(30, 20, 10));

}

}

 

O/P: 30

60

  1. What is the use of Final Keyword in Java?

Ans . In java, a constant is declared using the keyword Final.

Value can be assigned only once and after assignment, value of a constant can’t be changed.

For example, to define const_val as a constsant is given below

Private Final int const_val=100

  1. How can you generate random numbers in Java?

Ans . Using Math.random() define in java.util package.

  1. What is the purpose of finalization?

Ans . The purpose of finalization is to give an unreachable object the opportunity to perform any cleanup

processing before the object is garbage collected