Q1. What are the data types in Java?
Java supports two main categories of data types:
- Primitive Types: Directly store values (e.g., int, char, float, boolean, etc.).
- Reference (Non-Primitive) Types: Store memory references and include String, arrays, enums, classes, and interfaces.
Example:
java
int age = 25;
String name = “John”;
🔹 Q2. What are wrapper classes in Java?
Wrapper classes wrap primitive values into objects. This is useful for working with collections like ArrayList.
Primitive | Wrapper |
int | Integer |
char | Character |
boolean | Boolean |
Example:
java
CopyEdit
Integer x = Integer.valueOf(10);
🔹 Q3. Does Java support dynamic arrays?
Native arrays (int[]) in Java are static. For dynamic resizing, use ArrayList from java.util.
Example:
java
ArrayList<String> list = new ArrayList<>();
list.add(“Java”);
list.add(“Python”);
🔹 Q4. What is JVM in Java?
JVM (Java Virtual Machine) executes Java bytecode (.class files). It allows Java to run on any platform without recompilation.
🔹 Q5. Why is Java platform-independent?
Java programs compile into bytecode, which can run on any OS that has a compatible JVM, enabling the write once, run anywhere capability.
🔹 Q6. What is the difference between local and global variables?
- Local Variable: Declared inside methods, accessible only within them.
- Global (Instance) Variable: Declared at class level, accessible throughout the class.
🔹 Q7. What is data encapsulation?
Encapsulation means hiding internal object data and exposing access via methods. It’s done using private fields and public getters/setters.
Example:
java
private int age;
public void setAge(int age) { this.age = age; }
🔹 Q8. What is method overloading?
Method overloading allows multiple methods with the same name but different parameter types or counts.
Example:
java
int sum(int a, int b) { return a + b; }
double sum(double a, double b) { return a + b; }
🔹 Q9. What is method overriding in Java?
Overriding means redefining a parent class method in a child class with the same signature.
Example:
java
class Animal {
void sound() { System.out.println(“Animal sound”); }
}
class Dog extends Animal {
void sound() { System.out.println(“Dog barks”); }
}
🔹 Q10. Why is the main() method static?
The main() method is static so the JVM can invoke it without needing an object of the class.
java
public static void main(String[] args) { }
🔹 Q11. What is the difference between throw and throws in Java?
Keyword | Purpose |
throw | Used to explicitly throw an exception |
throws | Declares what exceptions a method might throw |
Example:
java
throw new IOException(“File not found”);
public void readFile() throws IOException { }
🔹 Q12. What is a Singleton class in Java?
A singleton class ensures only one instance exists in the application.
Basic Singleton Pattern:
java
class Singleton {
private static Singleton instance;
private Singleton() {}
public static Singleton getInstance() {
if (instance == null)
instance = new Singleton();
return instance;
}
}
🔹 Q13. Does every try block need a catch block?
No. A try block can be followed by:
- Only a catch
- Only a finally
- Both catch and finally
Example:
java
try {
int a = 5 / 0;
} finally {
System.out.println(“Cleanup code”);
}
🔹 Q14. What does super do in Java?
super is used to:
- Call the parent class constructor.
- Access parent class methods or variables.
Example:
java
super.display(); // calls parent method
🔹 Q15. What is the use of final keyword in Java?
- Final variable: Can’t be reassigned.
- Final method: Can’t be overridden.
- Final class: Can’t be inherited.
🔹 Q16. How is exception handling done in Java?
Java uses try-catch-finally blocks for error handling.
java
try {
int a = 5 / 0;
} catch (ArithmeticException e) {
System.out.println(“Cannot divide by zero.”);
} finally {
System.out.println(“This block always executes.”);
}
🔹 Q17. How to prevent Java object serialization?
- Mark fields as transient.
- Avoid implementing Serializable interface.
- Throw NotSerializableException.
🔹 Q18. Difference between a constructor and a method?
Feature | Constructor | Method |
Name | Same as class | Can be any valid name |
Return type | None | Must specify (can be void) |
Purpose | Initializes object | Defines logic or behavior |
🔹 Q19. Why is reflection used in Java?
Reflection allows inspection and modification of classes, methods, and fields at runtime. Common in frameworks and tools.
Example Use: Dependency Injection, Annotation Processing.
🔹 Q20. What are ClassLoaders in Java?
ClassLoaders load .class files into JVM. Types include:
- Bootstrap ClassLoader: Loads core Java classes.
- Extension ClassLoader: Loads from ext directory.
System ClassLoader: Loads application classes from the classpath.
👉The Next 20 Questions-I: JAVA