🌟 Java Interview Questions and Answers for Freshers (Part 2 – Advanced Topics)
If you’ve already learned Java basics and read Part 1, this section will take you one step higher.
Many interviewers test freshers not only on core concepts but also on slightly advanced topics like Collections, Threads, JDBC, and Exception Handling.
Let’s dive in and make each question easy to understand.
🔹 1. What are Java Collections?
Answer:
The Collections Framework in Java is a set of classes and interfaces that store and manage groups of objects efficiently.
It includes List, Set, Map, and Queue interfaces.
Example:
import java.util.*;
class Example {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>();
list.add("Java");
list.add("Python");
System.out.println(list);
}
}
🔹 2. What is the difference between Array and ArrayList?
| Feature | Array | ArrayList |
|---|---|---|
| Size | Fixed | Dynamic |
| Type | Can store primitives | Stores only objects |
| Performance | Faster | Slightly slower |
| Example | int arr[] = new int[5]; | ArrayList<Integer> list = new ArrayList<>(); |
🔹 3. What is the difference between List and Set?
| List | Set |
|---|---|
| Allows duplicate elements | Doesn’t allow duplicates |
| Maintains insertion order | Doesn’t guarantee order |
| Example: ArrayList | Example: HashSet |
🔹 4. What is HashMap in Java?
Answer:
A HashMap stores data in key-value pairs.
Each key is unique, but values can repeat.
Example:
import java.util.*;
class Example {
public static void main(String[] args) {
HashMap<Integer, String> map = new HashMap<>();
map.put(1, "Apple");
map.put(2, "Banana");
System.out.println(map.get(1)); // Output: Apple
}
}
🔹 5. What is the difference between HashMap and Hashtable?
| Feature | HashMap | Hashtable |
|---|---|---|
| Synchronization | Not synchronized | Synchronized |
| Allows null | Yes (one null key) | No |
| Speed | Faster | Slower |
| Introduced in | Java 1.2 | Java 1.0 |
🔹 6. What is Iterator in Java?
Answer:
An Iterator is used to traverse elements in a collection one by one.
Example:
import java.util.*;
class Example {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>();
list.add("A");
list.add("B");
Iterator<String> it = list.iterator();
while(it.hasNext()) {
System.out.println(it.next());
}
}
}
🔹 7. What is the difference between Iterator and ListIterator?
| Iterator | ListIterator |
|---|---|
| Traverses forward only | Traverses both directions |
| Works for all collections | Works only for List |
| Can remove elements | Can add, remove, modify elements |
🔹 8. What is the difference between Comparable and Comparator?
| Comparable | Comparator |
|---|---|
| Used to define natural order | Used for custom sorting |
Found in java.lang | Found in java.util |
| One compareTo() method | compare() method |
Example:
class Student implements Comparable<Student> {
int age;
Student(int age) { this.age = age; }
public int compareTo(Student s) {
return this.age - s.age;
}
}
🔹 9. What is an Exception in Java?
Answer:
An exception is an event that interrupts the normal flow of the program during execution.
It can be caused by invalid input, file issues, or memory errors.
🔹 10. What are the types of exceptions?
- Checked Exceptions – Checked at compile-time.
Example:IOException,SQLException - Unchecked Exceptions – Occur at runtime.
Example:NullPointerException,ArithmeticException
🔹 11. What is the difference between throw and throws?
| Keyword | Description |
|---|---|
| throw | Used to throw an exception manually |
| throws | Declares exceptions in method signature |
Example:
void test() throws IOException {
throw new IOException("Error");
}
🔹 12. What is finally block in Java?
Answer:
The finally block is always executed whether an exception occurs or not.
It is mainly used to close resources like files or database connections.
Example:
try {
int a = 10 / 0;
} catch(Exception e) {
System.out.println("Error");
} finally {
System.out.println("Finally block executed");
}
🔹 13. What is the difference between checked and unchecked exceptions?
| Checked | Unchecked |
|---|---|
| Checked at compile time | Checked at runtime |
| Must be handled using try-catch | Optional to handle |
| Example: IOException | Example: NullPointerException |
🔹 14. What is multithreading in Java?
Answer:
Multithreading allows multiple threads to run concurrently in a program.
Each thread is a lightweight sub-process that executes independently.
Example:
class MyThread extends Thread {
public void run() {
System.out.println("Thread running...");
}
}
🔹 15. What is the difference between process and thread?
| Process | Thread |
|---|---|
| Independent program | Part of a process |
| Heavyweight | Lightweight |
| Has separate memory | Shares memory with other threads |
🔹 16. How can you create a thread in Java?
There are two ways:
- Extending the Thread class
- Implementing the Runnable interface
Example (Runnable):
class MyTask implements Runnable {
public void run() {
System.out.println("Task running...");
}
}
🔹 17. What is synchronization?
Answer:
Synchronization ensures that only one thread can access a resource at a time, preventing conflicts in multithreaded environments.
Example:
synchronized void display() {
System.out.println("Synchronized method");
}
🔹 18. What is the difference between sleep() and wait()?
| sleep() | wait() |
|---|---|
| Defined in Thread class | Defined in Object class |
| Doesn’t release lock | Releases lock |
| Used to pause thread | Used for thread communication |
🔹 19. What is deadlock in Java?
Answer:
A deadlock occurs when two or more threads are waiting for each other’s locked resources indefinitely.
Example:
Thread A holds Resource 1 and waits for Resource 2, while Thread B holds Resource 2 and waits for Resource 1.
🔹 20. What is JDBC in Java?
Answer:
JDBC (Java Database Connectivity) is an API that allows Java programs to connect and interact with databases like MySQL, Oracle, etc.
🔹 21. What are the steps to connect Java to a database?
- Load the driver
- Establish connection
- Create statement
- Execute query
- Close connection
Example:
Connection con = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/dbname", "root", "password");
🔹 22. What are Statement and PreparedStatement?
| Statement | PreparedStatement |
|---|---|
| Used for static SQL queries | Used for dynamic queries |
| Slower | Faster |
| Prone to SQL injection | More secure |
🔹 23. What is serialization in Java?
Answer:
Serialization is the process of converting an object into a byte stream to save it or transfer it over a network.
Example:
class Student implements Serializable {
int id;
String name;
}
🔹 24. What is transient keyword?
Answer:
The transient keyword is used to skip certain fields from being serialized.
Example:
transient int password;
🔹 25. What is difference between transient and static?
| Keyword | Description |
|---|---|
| transient | Excluded from serialization |
| static | Belongs to class, not object |
🔹 26. What is the purpose of the “this” keyword?
Answer:this refers to the current object of the class.
Example:
class Demo {
int x;
Demo(int x) { this.x = x; }
}
🔹 27. What is the super keyword?
Answer:super refers to the immediate parent class object.
It is used to call parent class variables, methods, or constructors.
Example:
class Animal {
void sound() { System.out.println("Animal sound"); }
}
class Dog extends Animal {
void sound() {
super.sound();
System.out.println("Dog bark");
}
}
🔹 28. What is wrapper class in Java?
Answer:
Wrapper classes convert primitive data types into objects.
Example: int → Integer, float → Float.
Example:
int a = 10;
Integer obj = Integer.valueOf(a);
🔹 29. What is auto-boxing and unboxing?
Answer:
- Autoboxing: Automatically converting primitive to object.
- Unboxing: Automatically converting object back to primitive.
Example:
int a = 10;
Integer b = a; // Autoboxing
int c = b; // Unboxing
🔹 30. What is difference between public, private, protected, and default?
| Modifier | Scope |
|---|---|
| public | Everywhere |
| private | Within the same class |
| protected | Same package + subclasses |
| default | Within same package |
🔹 31. What is the difference between compile-time and runtime polymorphism?
| Type | Description | Example |
|---|---|---|
| Compile-time | Method overloading | Same name, different parameters |
| Runtime | Method overriding | Same name, same parameters in subclass |
🔹 32. What is the difference between interface and abstract class?
| Interface | Abstract Class |
|---|---|
| Supports multiple inheritance | Doesn’t support multiple inheritance |
| Can’t have constructor | Can have constructor |
| Methods are public and abstract | Can have abstract and concrete methods |
🔹 33. What is package import in Java?
Answer:
To use a class from another package, use the import keyword.
Example:
import java.util.ArrayList;
🔹 34. What is System.out.println()?
Answer:
It’s used to print data to the console.
Here:
System→ classout→ static member (PrintStream)println()→ method to print text
🔹 35. What is difference between public static void main() and normal method?
Answer:main() is the entry point of a Java program.
It is static so that it can be run without creating an object.
🟢 Final Tips for Freshers
- Focus on Collections, OOP, Exception Handling, and Threads.
- Learn to write small programs like sorting, reversing a string, or file handling.
- Explain your logic clearly in interviews — even if your code isn’t perfect.
- Keep revising syntax but focus more on concepts.