Java Basics and OOPs Interview Questions with Answers
Java Basics
Q: What are the main features of Java?
A: Platform-independent, Object-oriented, Simple, Secure, Robust, Multithreaded, High
Performance, Dynamic.
Q: What is the difference between JDK, JRE, and JVM?
A: - JVM (Java Virtual Machine): Runs Java bytecode.
- JRE (Java Runtime Environment): JVM + libraries to run Java applications.
- JDK (Java Development Kit): JRE + development tools (compiler, debugger).
Q: Explain the Java compilation process.
A: Java source code (.java) is compiled by javac to bytecode (.class), which is executed by the JVM.
Q: What are data types in Java?
A: Java has 8 primitive types: byte, short, int, long, float, double, char, and boolean.
Q: Difference between == and .equals()?
A: == checks reference equality. .equals() checks value/content equality.
Q: What are access modifiers?
A: - private: within the class only
- default: within the same package
- protected: same package + subclasses
- public: accessible everywhere
Q: What is type casting?
A: Converting one data type to another. Example: int x = (int) 3.14;
Q: Difference between Array and ArrayList?
A: - Arrays are fixed-size and can hold primitives.
- ArrayList is resizable but only holds objects.
Q: Use of the final keyword?
A: - Final variable: constant
- Final method: cannot be overridden
- Final class: cannot be extended
Q: Difference between break and continue?
A: - break: exits the loop entirely
- continue: skips current iteration
Control Structures
Q: Syntax of a for loop?
A: for (int i = 0; i < 5; i++) {
System.out.println(i);
Q: How does a switch work?
A: It selects a block to execute based on variable value.
Example:
switch(day) {
case 1: System.out.println("Mon"); break;
default: System.out.println("Invalid");
Q: Difference between while and do-while?
A: - while: checks condition before executing
- do-while: executes at least once
OOPs Concepts
Q: What are the 4 pillars of OOP?
A: - Encapsulation: wrapping data in classes
- Abstraction: hiding details using interfaces/abstract classes
- Inheritance: reusing code via subclassing
- Polymorphism: multiple forms (overloading/overriding)
Q: What is a class and object?
A: Class: blueprint
Object: instance of a class
Q: What is inheritance?
A: Acquiring properties of one class into another using extends.
Q: Overloading vs Overriding?
A: - Overloading: same method name, different parameters (compile time)
- Overriding: same method in subclass (runtime)
Q: Difference between super and this?
A: - super: refers to parent class
- this: refers to current object
Q: What is abstraction?
A: Hiding implementation, showing only functionality. Achieved using abstract class or interface.
Q: What is an interface?
A: A contract with method declarations. All methods are public and abstract by default.
Q: Difference between interface and abstract class?
A: - Interface: all methods abstract, no constructors
- Abstract class: can have method bodies and constructors
Q: Can a class implement multiple interfaces?
A: Yes. Java supports multiple inheritance via interfaces.
Q: What is polymorphism?
A: Ability to take many forms. Example: method overriding.
Q: What is encapsulation?
A: Binding data and code into a single unit (class). Fields are private, access via getters/setters.
Exception Handling
Q: What is exception handling?
A: Mechanism to handle runtime errors using try-catch blocks.
Q: Checked vs Unchecked exceptions?
A: - Checked: checked at compile time (IOException)
- Unchecked: runtime (NullPointerException)
Q: Purpose of try-catch-finally?
A: - try: code that might throw exception
- catch: handles exception
- finally: always executes
Q: Use of throws keyword?
A: Declares exceptions a method might throw.
Miscellaneous
Q: What is a constructor?
A: Special method to initialize objects. Same name as class, no return type.
Q: Can constructors be overloaded?
A: Yes, by changing parameters.
Q: Difference between static and non-static?
A: - Static: belongs to class
- Non-static: belongs to object
Q: What is a package?
A: A namespace that organizes classes and interfaces.
Q: What is garbage collection?
A: Automatic memory management that deletes unused objects.