<<< Core Java Interview Questions and Answers >>>





Core Java - Questions & Answers

Here is the list of frequently asked Core Java Interview Questions. The answers are short and to the point.These questions are frequently asked by the recruiters. So we try our best to provide you the Core java interview questions.

«« | 1 2 3 4 5 | »»

Core Java: Java Interview Questions

1) What are the principle concepts of oops?

Ans :The different OOPS concepts are :

  1. Encapsulation
  2. Inheritance
  3. Polymorphism

Encapsulation :The process of wrapping or putting up of data into a single unit class & keep data safe from misuse is called encapsulation.

    Example : Take a car class, In car we have many parts which is not required for driver to know.He is required to know only about how to start and stop the car.So, we can expose what all are required & hide the rest by using encapsulation.

Inheritance :Inheritance is concept of getting properties of one class object to another class object.

  1. Extends is the keyword used for developing inheritance between two classes and two interfaces.
  2. Implements is the keyword used to developed inheritance between interface and class.

Polymorphism :Polymorphism is an important Object oriented concept and widely used in Java and other programming language.Polymorphism is the ability of an object to take on many forms. The most common use of polymorphism in OOP occurs when a parent class reference is used to refer to a child class object.

There are two types of polymorphism:

  1. Compile time polymorphism: This is done using method overloading.
  2. Run time polymorphism: This is done using inheritance and interfaces.


2) Why is java called a platform independent language?

Ans :When java compiles a java program, it generates .class file. Class file contains bytecode.These byte codes are interpreted by JVM.Since JVM is available for all the platform by Sun MicroSystem,we can execute this bytecode in any platform.A byte code which is generated in windows platform can be executed in Linux platform.This make java a platform independent.


3) Why java is not pure Object Oriented language?

Ans :Java is not pure Object Oriented language because it supports Primitive datatype such as int, byte, long... etc, to be used, which are not objects.

There are few reasons why java is not pure Object Oriented language :

  1. Java supports primitive data type such as int . char , float .
  2. All these are not object but you can argue we have wrapper class for each of the data type such as Integer(int) , Float(float) etc .
  3. Even without object we can run a java program (using static)
  4. Features like multiple inheritance which are part of oops are not supported

There are seven qualities to be satisfied for a programming language to be pure Object Oriented.They are:

  1. Encapsulation
  2. Inheritance
  3. Polymorphism
  4. Abstraction
  5. All predefined types are objects
  6. All operations are performed by sending messages to objects
  7. All user defined types are objects.


4) What is the difference between private, protected, and public?

Ans :
Public : Public is the most well known of the Java keywords. Public is also the easiest of the Java access modifiers because of its nature. A variable or method that is public means that any class can access it. This is useful for when the variable should be accessible by your entire application. Usually common routines and variables that need to be shared everywhere are declared public.

Private Private access modifier is the most restrictive access level. Class and interfaces cannot be private. Private variables and methods are those that are meant to be directly accessed by the Java class that owns them.

Protected:Protected variables and methods allow the class itself to access them,classes inside of the same package to access them, and subclasses of that class to access them. Variables, methods and constructors which are declared protected in a superclass can be accessed only by the subclasses in other package or any class within the package of the protected members' class.


5) What is the difference between JVM JRE and JDK?

Ans :
JVM (Java Virtual Machine) : JVM is an abstract machine.It is a specification that provides runtime environment in which java bytecode can be executed.It runs pre compiled Java programs, which mean JVM executes .class files (byte-code) and produces output. It provides a machine interface that does not depend on the underlying operating system and machine hardware architecture. Programs written in Java are compiled into machine language, but it is a machine language for a computer that is, virtual and doesn't really exist. This so-called "virtual" computer is known as the Java virtual machine (JVM).

The JVM performs following main tasks:

  1. Loads code
  2. Verifies code
  3. Executes code
  4. Provides runtime environment

JRE (Java Runtime Environment JRE is used to provide runtime environment.JRE is an implementation of the JVM which actually executes Java programs.It contains set of libraries + other additional components that JVM uses at runtime.It does not contain any development tools such as compiler, debugger.If you want to run any java program, you need to have JRE installed in the system.

JDK (Java Development Kit) :JDK is a bundle of software components or a group of utilities that is used to develop Java based applications and JRE to run the programs.It is an implementation of either of Java SE, Java EE or Java ME.

Java Development Kit is a bundle of the following software components that are needed to develop Java based applications.

  1. Java Compiler
  2. Java Interpreter
  3. Java Disassembler
  4. Java Documentation
  5. Java Debugger
  6. Java Applet Viewer


6) What is the difference between java compiler and java interpreter?

Ans : The difference between java compiler and java interpreter:

Java Compiler Java Interpreter
1. Compiler Takes Entire program as input. 1. Interpreter Takes Single instruction as input.
2. Compiler generates intermediate code. 2. Interpreter does not generates intermediate code.
3. Program need not be compiled every time. 3. Program need to be compiled every time.
4. Memory Requirement is more. 4. Memory Requirement is Less.
5. Difficult to debug. 5. Easy to debug.


7) What is Encapsulation?

Ans :The process of wrapping or putting up of data into a single unit class & keep data safe from misuse is called encapsulation.
Example : Take a car class, In car we have many parts which is not required for driver to know.He is required to know only about how to start and stop the car.So, we can expose what all are required & hide the rest by using encapsulation.


8) What is Inheritance?

Ans :Inheritance is concept of getting properties of one class object to another class object.

  1. Extends is the keyword used for developing inheritance between two classes and two interfaces.
  2. Implements is the keyword used to developed inheritance between interface and class.


9) Why multiple inheritance is not supported in java?

Ans : In multiple inheritance there is every chance of multiple properties of multiple objects with the same name available to the sub class object with same priorities leads for the ambiguity.Java doesn’t support multiple inheritance in classes because it can lead to diamond problem
What is diamond problem? With Below diagram , we will try to explain what is diamond problem? Below diagram shows multiple inheritance as Class Gama extends both Class Alpha & Beta. Assume a method in class Ray and class Alpha & Beta overrides. Gama is extending both Alpha & Beta.So if Gama wants to use the same method which method would be called Alpha or Beta.In this way it will leads for the ambiguity. So,this is the main reason Java not support multiple inheritance.


10) What is constructor?

Ans :Constructor in java is a special type of method that is used to initialize the object and is executed at the time of Object creation.The constructor gets called when we create an object of a class. Name of constructor in java must be exactly same with the class on which you declare constructor.Constructor that have no parameter is known as default constructor.Default constructor provides the default values to the object like 0, null etc. depending on the type.

«« | 1 2 3 4 5 | »»