1.What are the main features of Java?
Java is known for:
- Object-Oriented – Everything is based on objects and classes.
- Platform Independent – Runs on any OS using JVM (Write Once, Run Anywhere).
- Secure – Uses bytecode verification and access control.
- Robust – Exception handling and memory management with garbage collection.
- Multithreading – Supports concurrent execution using threads.
- High Performance – Uses JIT (Just-In-Time) compiler for faster execution.
2.What is the difference between JDK, JRE, and JVM?
| Feature | JDK (Java Development Kit) | JRE (Java Runtime Environment) | JVM (Java Virtual Machine) |
|---|
| Definition | Provides tools for developing, compiling, and running Java programs | Provides libraries and JVM to run Java applications | Executes Java bytecode |
| Includes | JRE + development tools (compiler, debugger, etc.) | JVM + libraries and classes | Converts bytecode to machine code |
| Needed for? | Developers | End-users running Java applications | Runs Java programs |
3.What is the difference between == and equals()?
==compares references (memory addresses) of objects.equals()compares values of objects (content comparison).
Example:
String s1 = new String("Java");
String s2 = new String("Java");
System.out.println(s1 == s2); // false (different objects in memory)
System.out.println(s1.equals(s2)); // true (content is the same)
4.What is a constructor in Java?
A constructor is a special method used to initialize an object. It has the same name as the class and does not have a return type.
Example:
class Car {
String model;
// Constructor
Car(String m) {
model = m;
}
public void display() {
System.out.println("Car model: " + model);
}
public static void main(String[] args) {
Car c = new Car("Tesla");
c.display();
}
}
Types of constructors:
- Default Constructor – No parameters.
- Parameterized Constructor – Accepts parameters.
5.What is the difference between static and instance variables?
| Feature | Static Variable | Instance Variable |
|---|
| Keyword | static | No static |
| Memory Location | Class-level (shared across objects) | Separate for each object |
| Access | Can be accessed without an object | Requires an object to access |
Example:
class Example {
static int staticVar = 10; // Shared across all objects
int instanceVar = 20; // Separate for each object
}
6.What is method overloading and method overriding?
| Feature | Method Overloading | Method Overriding |
|---|
| Definition | Same method name, different parameters in the same class | Same method name and parameters in parent and child class |
| Purpose | Achieve compile-time polymorphism | Achieve runtime polymorphism |
| Example | Multiple add() methods with different parameters | Subclass overrides a method from superclass |
Example of Overloading:
class MathUtils {
int add(int a, int b) {
return a + b;
}
double add(double a, double b) {
return a + b;
}
}
Example of Overriding:
class Parent {
void show() {
System.out.println("Parent class");
}
}
class Child extends Parent {
void show() { // Overriding
System.out.println("Child class");
}
}
7.What is the difference between abstract class and interface?
| Feature | Abstract Class | Interface |
|---|
| Methods | Can have both abstract and concrete methods | Only abstract methods (before Java 8) |
| Variables | Can have instance variables | Only final and static variables |
| Inheritance | Supports single inheritance | Supports multiple inheritance |
Example of Abstract Class:
abstract class Animal {
abstract void sound(); // Abstract method
void sleep() { // Concrete method
System.out.println("Sleeping...");
}
}
Example of Interface:
interface Animal {
void sound(); // Abstract method (implicitly public)
}
8.What is the difference between ArrayList and LinkedList?
| Feature | ArrayList | LinkedList |
|---|
| Data Structure | Dynamic array | Doubly linked list |
| Insertion/Deletion | Slow (shifting required) | Fast (modifies pointers) |
| Access Time | Fast (O(1) for index) | Slow (O(n) for index) |
9.What is a Java Stream?
A Stream in Java (introduced in Java 8) is used for functional-style operations on collections.
Example:
import java.util.*;
import java.util.stream.*;
class StreamExample {
public static void main(String[] args) {
List<String> names = Arrays.asList("Alice", "Bob", "Charlie");
// Convert to uppercase using streams
List<String> upperNames = names.stream()
.map(String::toUpperCase)
.collect(Collectors.toList());
System.out.println(upperNames);
}
}
10.What is the difference between HashMap and HashSet?
| Feature | HashMap | HashSet |
|---|
| Stores | Key-value pairs | Only unique values |
| Allows Duplicates? | Keys must be unique (values can duplicate) | No duplicate values |
| Implements | Map interface | Set interface |
Example:
HashMap<Integer, String> map = new HashMap<>();
map.put(1, "A");
map.put(2, "B");
HashSet<String> set = new HashSet<>();
set.add("A");
set.add("B");
