Java basic Interview Question and Answer for Fresher
Java basic Interview Question and Answer for Fresher
3. What is JVM?
JVM stands for Java Virtual Machine. It is an abstract machine that provides the runtime
environment in which Java programs are executed. The JVM interprets Java bytecode and
translates it into machine-specific code.
8. What is inheritance?
Inheritance is a mechanism in which one class inherits properties and methods from another
class. The class that inherits is called the subclass or derived class, and the class that is inherited
from is called the superclass or base class.
9. What is encapsulation?
Encapsulation is a mechanism in which data and methods are combined into a single unit called
a class. Encapsulation provides data hiding, which means that the internal implementation
details of a class are hidden from the outside world.
10. What is polymorphism?
Polymorphism is a mechanism in which a single method can have different forms or
implementations. Polymorphism can be achieved through method overloading or method
overriding.
23. What is the difference between a shallow copy and a deep copy in Java?
A shallow copy creates a new object with the same reference as the original object, while a
deep copy creates a new object with a new reference.
26. What is a binary search tree? How can you implement one in Java?
A binary search tree is a data structure in which each node has at most two children, and the
left child is smaller than the parent while the right child is larger. You can implement one in
Java using recursion and comparison operators.
27. What is the difference between an abstract class and an interface in Java?
An abstract class is a class that cannot be instantiated and can contain abstract methods, while
an interface is a collection of abstract methods and constants that can be implemented by any
class.
32. What is the difference between a private and a protected method in Java?
A private method is only accessible within the same class, while a protected method is
accessible within the same class and any subclass.
34. What is the difference between a while loop and a do-while loop in Java?
A while loop executes the loop body if the condition is true, while a do-while loop executes the
loop body at least once before checking the condition.
37. What is the difference between a static and non-static variable in Java?
A static variable is associated with the class, while a non-static variable is associated with an
instance of the class.