Java Interview Questions and Answers for Freshers (Part 2 – Advanced Topics)

Mou

October 29, 2025


🌟 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?

FeatureArrayArrayList
SizeFixedDynamic
TypeCan store primitivesStores only objects
PerformanceFasterSlightly slower
Exampleint arr[] = new int[5];ArrayList<Integer> list = new ArrayList<>();

🔹 3. What is the difference between List and Set?

ListSet
Allows duplicate elementsDoesn’t allow duplicates
Maintains insertion orderDoesn’t guarantee order
Example: ArrayListExample: 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?

FeatureHashMapHashtable
SynchronizationNot synchronizedSynchronized
Allows nullYes (one null key)No
SpeedFasterSlower
Introduced inJava 1.2Java 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?

IteratorListIterator
Traverses forward onlyTraverses both directions
Works for all collectionsWorks only for List
Can remove elementsCan add, remove, modify elements

🔹 8. What is the difference between Comparable and Comparator?

ComparableComparator
Used to define natural orderUsed for custom sorting
Found in java.langFound in java.util
One compareTo() methodcompare() 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?

  1. Checked Exceptions – Checked at compile-time.
    Example: IOException, SQLException
  2. Unchecked Exceptions – Occur at runtime.
    Example: NullPointerException, ArithmeticException

🔹 11. What is the difference between throw and throws?

KeywordDescription
throwUsed to throw an exception manually
throwsDeclares 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?

CheckedUnchecked
Checked at compile timeChecked at runtime
Must be handled using try-catchOptional to handle
Example: IOExceptionExample: 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?

ProcessThread
Independent programPart of a process
HeavyweightLightweight
Has separate memoryShares memory with other threads

🔹 16. How can you create a thread in Java?

There are two ways:

  1. Extending the Thread class
  2. 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 classDefined in Object class
Doesn’t release lockReleases lock
Used to pause threadUsed 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?

  1. Load the driver
  2. Establish connection
  3. Create statement
  4. Execute query
  5. Close connection

Example:

Connection con = DriverManager.getConnection(
    "jdbc:mysql://localhost:3306/dbname", "root", "password");

🔹 22. What are Statement and PreparedStatement?

StatementPreparedStatement
Used for static SQL queriesUsed for dynamic queries
SlowerFaster
Prone to SQL injectionMore 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?

KeywordDescription
transientExcluded from serialization
staticBelongs 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?

ModifierScope
publicEverywhere
privateWithin the same class
protectedSame package + subclasses
defaultWithin same package

🔹 31. What is the difference between compile-time and runtime polymorphism?

TypeDescriptionExample
Compile-timeMethod overloadingSame name, different parameters
RuntimeMethod overridingSame name, same parameters in subclass

🔹 32. What is the difference between interface and abstract class?

InterfaceAbstract Class
Supports multiple inheritanceDoesn’t support multiple inheritance
Can’t have constructorCan have constructor
Methods are public and abstractCan 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 → class
  • out → 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.


Leave a Comment