Any java interview be it for fresher or for experienced professional will have an interview round completely focusing on
Core Java. For experienced candidates this is usually the first round.
I am enlisting few of the most popular core java questions.
1.
Interview questions for freshers.
2.
Interview questions for experienced .
1.Interview questions for freshers.
- What is Class?
Ans. Class
is a template. It specifies the data members and possible operations on its
data members.
- What is Object?
Ans.
Object is an instance of class.
- What is an abstract class?
Ans.
Abstract class is a special type of class which cannot be instantiated. The
purpose of
Using abstract classes is to provide
the Common functionality to all the subclasses.
- What is interface?
Ans.
Interface is a way to achieve full abstraction in java. All the methods in an interface do not have body.
Any class implementing interface should
fulfill the contract to implement all the methods in the interface. This
behavior allows us to use Run time polymorphism to fullest.
- What is difference between abstract class and
interface? when will you use the abstract class?
a.
Abstract classes represent partial
abstraction. Interfaces represent full abstraction.
b.
The purpose of using abstract
classes is to provided abstraction for a few methods and provide some common
default implementation for some methods. Whereas interfaces are used when you
want all the subclasses to provide their own implementation to all methods.
c.
It is always preferable to implement
an interface then extending an abstract class. As once your class extend an
abstract class it cannot extend any other class.
- A class may extend maximum of how other many classes?
Ans A
class in java can extend at most one class.
- Does Java supports multiple inheritance?
Ans. Java
does not support multiple CLASS inheritance, as it creates some major problems
like Diamond problem in c++. However
Java supports Multiple interface implementation. So your class can implement
multiple interfaces.
- What are the four main concepts of object oriented
languages? Explain all four?
Ans- Four
major concepts are
1.
Abstraction
2.
Encapsulation
3.
Inheritance
4.
Polymorphism
- What are types of
relationship between objects?
Ans-
a.
Association- Class A uses class B.
This relationship is used when one object wants to use other object to perform
some service for it. Association also specifies the multiplicity between
objects.
Note- There is no
ownership between objects, Both have independent lifecycle.
Example- Teacher and student. A teacher can
b.
Aggregation- Class “A” has an
instance of class “B” . So Aggregation is special type of association , it is
directional association between object . Direction specifies which object
contains other object.
where instance of Contained
class(B) can independently exist even without instance of parent class (A).
c.
Composition- Composition is a
stricter form of aggregation. Where a Contained object cannot exist without
container object.
Example- A line item cannot exist without Order.
- What is inheritance? explain with example.
Ans Inheritance
is a feature in java which allows a new class(derived class) to be derived from
other class(base class). There by allowing the
derived class to inherit the properties and methods of the base class.
This helps improve the code reuse.
11. What is polymorphism? explain with example.
Ans. Polymorphism
means, ability to behave differently in different context. In term of java
Polymorphism is the ability of calling the methods based on type of the object not based on the type of reference.
In java
reference of the base class can refer to the object of derived class.
Class a{
Public
void fun(){
System.out.println(“
Function of class a called”);
}
}
Class b
extends a{
Public
void fun(){
System.out.println(“
Function of class b called”);
}
}
// calling code
a aobj=new b();
b.fun();// this will call the fun
method of class b
So the method being called is based on the type of object (class b) not
based on the refrence(class a);
- What are different types of polymorphism?
a.
Static polymorphism. Example-
method overloading
b.
Dynamic polymorphism example- method
overriding.
- What is the base class of all the classes?
Object
class is base class of all objects.
- What is difference between JVM, JRE and JDK?
JDK- Java
development kit includes the tools, binaries and java classes used for
Developing,
compiling and running java programs. JDK includes tools like compiler, visualvm
ect.
JVM-
Converts the byte codes to machine specific code, and critical functionalities
like
Memory management, garbage collection and
security.
JRE- Java
runtime environment is implementation of JVM, it is the platform to execute
java programs. It contains jvm, java binaries and other custom binaries to
execute java program. JRE does not includes development specific tools like
compiler.
- How Java achieves platform independence?
- What is stack?
- What is heap?
- Java is call by reference or call by value?
- What is garbage collector?
- How can you call garbage collector?
- What is equals function of Object class used for ?
- what is difference between == and equals() function?
- What is thread?
- What is difference between thread and process?
- What are different ways to create a thread? which is
preferred way?
- What are the functions of object class-
wait,notify and notifyall used for? or how will threads inter
communicates?
- What is synchronized key word in java?
- What is deadlock?
- what is thread starvation?
- What are Executor interface?
- Write a program to simulate producer consumer problem?
- What do you mean by string is immutable?
- What is String pool?
- Write a program to Reverse a string.
- Write a program to generate a Fibonacci sequence.
- Write a program to check if a string is java
palindrome?
- Write a program to check if two given strings are same?
do not use .equals function?
- What is Collection framework in java?
- What is List, Map, Set interfaces? what are there
concrete implementation classes?
- What is difference between list and set
implementations?
- What is difference between a list and a map?
- What is difference between Hashtable and Hashmap?
- What is Exception in java?