Java Interview Questions
Java Interview Questions
Java Interview Questions
JVM: Converts Java bytecode into machine code so it can run on different operating systems.
JDK: Includes tools for developing Java applications (e.g., compiler, JRE).
Object: An instance of a class (e.g., your car is an object of the Car class).
Inheritance: Allows a class to use properties and methods from another class (e.g., Car inherits from
Vehicle).
What is Polymorphism?
Method Overloading: Multiple methods with the same name but different parameters.
Method Overriding: A subclass provides a specific implementation of a method from its superclass.
What is Encapsulation?
Encapsulation: Keeps data safe by making fields private and providing public methods to access
them.
What is Abstraction?
Abstraction: Hides complex implementation details and shows only the essential features. Achieved
Method: A block of code that performs a specific task and is part of a class. For example,
Static: Used for methods or variables that belong to the class, not to any specific object. For
example, static int count; belongs to the class and is shared among all instances.
Abstract Class: A class that can't be instantiated and can have abstract methods (without body) and
Stack: Memory area that stores local variables and method calls. It's small and operates in a LIFO
Heap: Memory area that stores objects. It's larger and used for dynamic memory allocation.
What are loops in Java? Explain for, while, and do-while loops.
For Loop: Executes a block of code a specific number of times. Example: for(int i = 0; i < 10; i++)
While Loop: Repeats a block of code as long as a condition is true. Example: while(condition)
Do-While Loop: Similar to the while loop but guarantees the block of code runs at least once.
Example: do { } while(condition)
Package: A way to organize related classes and interfaces. For example, java.util is a package that
StringBuilder: Mutable and faster for string manipulation, but not thread-safe.
Try-Catch Block: Used for handling exceptions. Code that might throw an exception goes inside try,
Example:
try {
int result = 10 / 0;
} catch (ArithmeticException e) {
What is the difference between the equals() method and the '==' operator?
== Operator: Compares references (whether two objects point to the same memory location).
this Keyword: Refers to the current object. It's used to avoid naming conflicts between class fields
Example:
class Person {
Person(String name) {