1. What is Java?
Question:
Java is a high-level, object-oriented, platform-independent programming language.
Explanation: Java code is compiled into bytecode and runs on JVM (Java Virtual Machine), making it portable.
2. What is JVM?
Question:
JVM (Java Virtual Machine) is an engine that executes Java bytecode.
Explanation: JVM converts bytecode into machine code and provides memory management, security, and garbage collection.
3. Difference between JDK, JRE, and JVM?
- JVM: Executes bytecode.
- JRE: JVM + libraries to run Java programs.
- JDK: JRE + development tools (javac, debugger).
4. What is OOP?
OOP (Object Oriented Programming) is a programming model based on objects.
- Encapsulation
- Inheritance
- Polymorphism
- Abstraction
5. What is Encapsulation?
Wrapping data and methods into a single unit (class).
class Student {
private int age;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
Explanation: Data is hidden using private variables and accessed through getters and setters.
6. What is Inheritance?
Inheritance allows one class to acquire properties of another class.
class Animal {
void eat() {
System.out.println("Eating");
}
}
class Dog extends Animal {
void bark() {
System.out.println("Barking");
}
}
Explanation: Dog inherits eat() method from Animal.
7. What is Polymorphism?
Ability of one method to perform different tasks.
class Bank {
int getRate() { return 5; }
}
class SBI extends Bank {
int getRate() { return 7; }
}
Explanation: Same method getRate() behaves differently.
8. What is Abstraction?
Hiding implementation and showing only essential features.
abstract class Shape {
abstract void draw();
}
class Circle extends Shape {
void draw() {
System.out.println("Drawing Circle");
}
}
9. Difference between Abstract class and Interface?
| Abstract Class | Interface |
|---|---|
| Can have methods with body | Only abstract methods (Java 7) |
| Supports variables | Supports constants only |
| Single inheritance | Multiple inheritance |
10. What is Constructor?
Constructor is used to initialize objects.
class Test {
Test() {
System.out.println("Constructor called");
}
}
11. What is Method Overloading?
Same method name with different parameters.
class Add {
int add(int a, int b) { return a+b; }
int add(int a, int b, int c) { return a+b+c; }
}
12. What is Method Overriding?
Redefining parent class method in child class.
class Parent {
void show() { System.out.println("Parent"); }
}
class Child extends Parent {
void show() { System.out.println("Child"); }
}
13. What is Exception Handling?
Handling runtime errors using try-catch blocks.
try {
int a = 10/0;
} catch(Exception e) {
System.out.println(e);
}
14. What is final keyword?
- final variable: value cannot change
- final method: cannot override
- final class: cannot inherit
15. What is Collection Framework?
A set of classes and interfaces for storing and manipulating data.
- List (ArrayList, LinkedList)
- Set (HashSet)
- Map (HashMap)
16. Difference between Array and ArrayList?
| Array | ArrayList |
|---|---|
| Fixed size | Dynamic size |
| Stores primitive & objects | Stores only objects |
17. What is Multithreading?
Running multiple threads simultaneously.
class MyThread extends Thread {
public void run() {
System.out.println("Thread running");
}
}
18. What is Synchronization?
Controls access to shared resources.
synchronized void display() {
System.out.println("Synchronized method");
}
19. What is Garbage Collection?
Automatic memory management by JVM.
20. What is String vs StringBuffer vs StringBuilder?
- String: Immutable
- StringBuffer: Mutable, thread-safe
- StringBuilder: Mutable, not thread-safe