Java Commonly Asked Interview Questions

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

JAVA IMPORTANT INTERVIEW QUESTIONS

1) What are the features in JAVA?


Features of Java:
OOPS CONCEPT
 Object-oriented
 Inheritance
 Encapsulation
 Polymorphism
 Abstraction
 Platform independent: A single program works on different platforms without
any modification.
 High Performance: JIT (Just In Time compiler) enables high performance in
Java. JIT converts the bytecode into machine language and then JVM starts the
execution.
 Multi-threaded: A flow of execution is known as a Thread. JVM creates a thread
which is called main thread. The user can create multiple threads by extending
the thread class or by implementing Runnable interface.

2) How does Java enable high performance?


Java uses Just In Time compiler to enable high performance. JIT is used to convert the
instructions into bytecodes.

3) What are the Java IDE’s?


Eclipse and NetBeans are the IDE's of JAVA.

4) What do you mean by Constructor?


The points given below explain what a Constructor is in detail:
 When a new object is created in a program a constructor gets invoked
corresponding to the class.
 The constructor is a method which has the same name as class name.
 If a user doesn’t create a constructor implicitly a default constructor will be
created.
 The constructor can be overloaded.
 If the user created a constructor with a parameter then he should create another
constructor explicitly without a parameter.

5) What is meant by Local variable and Instance variable?

Local variables are defined in the method and scope of the variables that have existed
inside the method itself. An instance variable is defined inside the class and outside the
method and scope of the variables exist throughout the class.

6) What are the Oops concepts?


OOPS concepts include:
 Inheritance
 Encapsulation
 Polymorphism
 Abstraction
 Interface

7) What is Inheritance?
Inheritance means one class can extend to another class. So that the codes can be
reused from one class to another class.
Existing class is known as Super class whereas the derived class is known as a sub
class.

8) What is Polymorphism?
Polymorphism means many forms.
A single object can refer the super class or sub-class depending on the reference type
which is called polymorphism.
9) What is meant by Method Overriding?
Method overriding happens if the sub class method satisfies the below
conditions with the Super class method:
 Method name should be same
 Argument should be same
 Return type also should be same
The key benefit of overriding is that the Sub class can provide some specific information
about that sub class type than the super class.

10) What is meant by Overloading?


Method overloading happens for different classes or within the same class.
For method overloading, subclass method should satisfy the below conditions
with the Super class method (or) methods in the same class itself:
 Same method name
 Different argument type
 May have different return types

11) What is meant by Interface?


Multiple inheritance cannot be achieved in java. To overcome this problem Interface
concept is introduced.
An interface is a template which has only method declarations and not the method
implementation.

 All the methods in the interface are internally public abstract void.
 All the variables in the interface are internally public static final that is constants.
 Classes can implement the interface and not extends.
 The class which implements the interface should provide an implementation for
all the methods declared in the interface.
12) What is meant by Abstract class?

We can create the Abstract class by using “Abstract” keyword before the class name.
An abstract class can have both “Abstract” methods and “Non-abstract” methods that
are a concrete class.

Abstract method:

The method which has only the declaration and not the implementation is called the
abstract method and it has the keyword called “abstract”. Declarations are the ends with
a semicolon.

13) What is Multi-threading?


Multiple threads are executed simultaneously. Each thread starts their own stack based on
the flow (or) priority of the threads.
On the 1st line execution, JVM calls the main method and the main thread stack looks as
shown below.
Once the execution reaches, t.start () line then a new thread is created and the new stack
for the thread is also created. Now JVM switches to the new thread and the main thread are
back to the runnable state.

Once the run() method has completed, then JVM switches back to the main thread and the
User thread has completed the task and the stack was disappeared.

JVM switches between each thread until both the threads are completed. This is called
Multi-threading.

14) Explain thread life cycle in Java.


Thread has the following states:
 New
 Runnable
 Running
 Non-runnable (Blocked)
 Terminated
15) How is inheritance in C++ different from Java?

In Java, it is possible to inherit attributes and methods from one class to another. We group
the "inheritance concept" into two categories:

subclass (child) - the class that inherits from another class


superclass (parent) - the class being inherited from
To inherit from a class, use the extends keyword.

16) What is Java Polymorphism?

Polymorphism means "many forms", and it occurs when we have many classes
that are related to each other by inheritance.

Like we specified in the previous chapter; Inheritance lets us inherit attributes


and methods from another class. Polymorphism uses those methods to
perform different tasks. This allows us to perform a single action in different
ways.

17) What is Java Abstract Classes and Methods?

Data abstraction is the process of hiding certain details and showing only essential
information to the user.
Abstraction can be achieved with either abstract classes or interfaces (which you will
learn more about in the next chapter).

The abstract keyword is a non-access modifier, used for classes and methods:
 Abstract class: is a restricted class that cannot be used to create objects (to access
it, it must be inherited from another class).
 Abstract method: can only be used in an abstract class, and it does not have a
body. The body is provided by the subclass (inherited from).

You might also like