Java Exam Topics - Well Explained Answers
1. Method Overloading
Method overloading is a feature in Java where multiple methods can have the same name but different
parameters (type, number, or order).
Example:
class Example {
void display(int a) {
System.out.println("Integer: " + a);
void display(String a) {
System.out.println("String: " + a);
Purpose: Improves readability and allows the same method name to be used for different data types.
2. Garbage Collection
Garbage Collection in Java is the process of automatically removing unused objects from memory to free up
resources.
- Handled by Java Virtual Machine (JVM)
- Helps prevent memory leaks
- Invoked automatically, but can be requested using System.gc();
Java Exam Topics - Well Explained Answers
3. Final, Finalize, and Finally
final: Keyword to define constants or prevent method overriding/inheritance.
Example: final int x = 10;
finalize(): Method called by garbage collector before destroying an object (rarely used in modern Java).
finally: A block that always executes after try-catch, used for cleanup operations.
Example:
try {
// risky code
} catch(Exception e) {
// exception handling
} finally {
// always executed
4. Comparison between Java and C++
| Feature | Java | C++ |
|------------------|-------------------------|-------------------------|
| Platform | Platform-independent | Platform-dependent |
| Pointers | No direct pointers | Supports pointers |
| Memory Mgmt | Automatic (GC) | Manual |
| Multiple Inherit | Supported via interfaces| Directly supported |
| Syntax | Similar to C++ | Based on C |
Java Exam Topics - Well Explained Answers
5. Inheritance
Inheritance allows one class (child) to inherit the properties and methods of another class (parent).
Example:
class Animal {
void sound() {
System.out.println("Animal sound");
class Dog extends Animal {
void bark() {
System.out.println("Bark");
6. Interface
An interface in Java is a reference type, similar to a class, that can contain only abstract methods (Java 8+
allows default methods too).
Example:
interface Animal {
void sound();
}
Java Exam Topics - Well Explained Answers
class Dog implements Animal {
public void sound() {
System.out.println("Bark");
7. Abstract Class vs Interface
| Feature | Abstract Class | Interface |
|----------------------|------------------------|--------------------------|
| Methods | Abstract + concrete | Only abstract (Java 8+ default) |
| Fields | Instance fields allowed | Only static and final fields |
| Inheritance | Single inheritance | Multiple implementation |