Java Interview Questions

1) What is difference between JDK,JRE and JVM?

JDK (Java Development Kit)

Java Developer Kit contains tools needed to develop the Java programs, and JRE to run the programs. The tools include compiler (javac.exe), Java application launcher (java.exe), Appletviewer, etc… Compiler converts java code into byte code. Java application launcher opens a JRE, loads the class, and invokes its main method.
You need JDK, if at all you want to write your own programs, and to compile the m. For running java programs, JRE is sufficient. JRE is targeted for execution of Java files i.e. JRE = JVM + Java Packages Classes(like util, math, lang, awt,swing etc)+runtime libraries. JDK is mainly targeted for java development. I.e. You can create a Java file (with the help of Java packages), compile a Java file and run a java file.

JRE (Java Runtime Environment)

Java Runtime Environment contains JVM, class libraries, and other supporting files. It does not contain any development tools such as compiler, debugger, etc. Actually JVM runs the program, and it uses the class libraries, and other supporting files provided in JRE. If you want to run any java program, you need to have JRE installed in the system
The Java Virtual Machine provides a platform-independent way of executing code; programmers can concentrate on writing software, without having to be concerned with how or where it will run. But, note that JVM itself not a platform independent. It only helps Java to be executed on the platform-independent way. When JVM has to interpret the byte codes to machine language, then it has to use some native or operating system specific language to interact with the system. One has to be very clear on platform independent concept. Even there are many JVMs written on Java, however hey too have little bit of code specific to the operating systems. 
If u just want to run applets (ex: Online Yahoo games or puzzles), JRE needs to be installed on the machine.

JVM (Java Virtual Machine)

As we all aware when we compile a Java file, output is not an ‘exe’ but it’s a ‘.class’ file. ‘.class’ file consists of Java byte codes which are understandable by JVM. Java Virtual Machine interprets the byte code into the machine code depending upon the underlying operating system and hardware combination. It is responsible for all the things like garbage collection, array bounds checking, etc… JVM is platform dependent.
The JVM is called “virtual” because it provides a machine interface that does not depend on the underlying operating system and machine hardware architecture. This independence from hardware and operating system is a cornerstone of the write-once run-anywhere value of Java programs.
There are different JVM implementations are there. These may differ in things like performance, reliability, speed, etc. These implementations will differ in those areas where Java specification doesn’t mention how to implement the features, like how the garbage collection process works is JVM dependent, Java spec doesn’t define any specific way to do this.

2) OOPS conceps in java?


1.Object
2.Class
3.Abstraction
4.Encapsulation
5.Inheritance
6.Polymorphism

3) Object in java?


Object is a any real time entity which has its state and behaviour where state represents the value of an object and behaviour represents the functionality

Eg.Lamp

State - lamp:on and off
Behaviour - lamp:turn on and turn off


4) Class in java?


A class is a group of objects . It is a template or blueprint from which objects are created.

A class in java can contain:

  • data member
  • method
  • constructor
  • block
  • class and interface

5) Abstraction in java?


Showing only the essential details to the users without background details i.e showing only the functionality by hiding the implementations is called abstraction.

Eg. We know to operate tv remove but dont know what really happens inside it

It can be acheived using abstract class and interface in java

6) Encapsulation in java?


Wrapping up of code and data together into a single unit is called encapsulation.It is usefull for security purposes

Eg. Capsule 

In java encapsulation can be acheived through putter setter method , where the variable must be declared in private so that you can set or get the value of the variable without using those methods.


6) Inheritance in java?



Inheritance is the ability to acquire the properties of one class from another class and keyword used for inheritance is extends.

java supports,

  • Single,
  • Multi-level and
  • Hierarchical inheritance

and it does not supports,
  • Multiple and
  • Hybrid inheritance
so to acheive multiple inheritance in java we go for interface


7) Polymorphism in java?


Polymorphism is the ability to take more than one form. There are two types of polymorphism


  • Runtime polymorphism,
  • Compiletime polymorphism

Runtime Polymorphism:

Method overriding is the best example of runtime polymorphism

Method Overriding:

Has same method name ,same parameters but in different classes. It can be acheived only through inheritance

Compiletime Polymorphism:

Method overloading is the best example of compiletime polymorphism

Method Overloading:

Has same method name with different parameters in same class
















No comments:

Post a Comment