Interview prep

Top 40 Core Java Interview Questions for Freshers

The top 40 core Java interview questions for freshers, with short answers on JVM, strings, OOP, collections, exceptions, Java 8 and threads, plus MCQ practice.

40QUESTIONSQWhat is the equals and hashCode…AEqual objects need the same hash code
  • Interview
  • Java
  • Freshers

Quick answer: Fresher Core Java interviews focus on five areas: JVM and strings, OOP, collections, exceptions, and Java 8 features with basic threads. The 40 questions below cover all five, from JDK versus JRE versus JVM and == versus equals to how HashMap works, checked versus unchecked exceptions and streams. Learn to explain each answer in your own words with one small example.

These core Java interview questions are the ones fresher rounds ask again and again. There are exactly 40, grouped into five topics. Each answer is short enough to say aloud in under a minute.

Do not memorise the words. Read an answer, close the page, and explain it to a friend or to your phone's voice recorder. If you cannot give an example, you do not know it yet.

Basics, JVM and strings (Q1 to Q8)

Q1. Why is Java called platform independent?

The compiler turns .java files into bytecode (.class files), not machine code. Any computer with a JVM can run that bytecode. So you write once and run on Windows, Linux or Mac without recompiling.

Q2. What is the difference between JDK, JRE and JVM?

The JVM runs bytecode. The JRE is the JVM plus the standard libraries needed to run programs. The JDK is the JRE plus tools to develop programs, such as the javac compiler. Developers install the JDK. See our page on JDK vs JRE vs JVM.

Q3. Why is main declared public static void?

public so the JVM can call it from outside the class. static so it can run without creating an object. void because it returns nothing to the JVM. String[] args holds command-line arguments.

Q4. What are the primitive data types in Java?

There are eight: byte, short, int, long, float, double, char and boolean. They store simple values directly. Everything else, including String and arrays, is an object.

Q5. What is the difference between == and equals()?

== compares references for objects: do both variables point to the same object? equals() compares content, if the class overrides it, as String does. For primitives, == compares values.

Q6. Why are strings immutable in Java?

Once created, a String object cannot change. This makes strings safe to share between threads, safe to use as HashMap keys, and allows the string pool to reuse one object for the same literal. Methods like toUpperCase() return a new string.

Q7. What is the difference between String, StringBuilder and StringBuffer?

String is immutable. StringBuilder is mutable and fast, for building text in a loop. StringBuffer is also mutable, but its methods are synchronized, so it is slower. Use StringBuilder unless several threads share the object.

Q8. What is the string constant pool?

It is a special area of the heap where Java keeps one copy of each string literal. String a = "hi"; String b = "hi"; makes both point to the same object. new String("hi") always creates a separate object.

OOP questions (Q9 to Q18)

Q9. What are the four pillars of OOP?

Encapsulation hides data behind methods. Inheritance lets a class reuse another class with extends. Polymorphism lets one call behave differently for different objects. Abstraction shows what an object does and hides how. Give a real example for each, such as a BankAccount for encapsulation.

Q10. What is the difference between a class and an object?

A class is a blueprint. An object is one instance created from it with new. Car is a class; your friend's red car is an object.

Q11. What is a constructor?

A special method with the class name and no return type, run when an object is created. If you write none, Java adds a default no-argument constructor. You can overload constructors with different parameters.

Q12. What is the difference between overloading and overriding?

Overloading means the same method name with different parameters in the same class, decided at compile time. Overriding means a child class gives its own version of a parent method with the same signature, decided at run time.

Q13. What is the difference between an abstract class and an interface?

A class can extend only one abstract class but implement many interfaces. An abstract class can have constructors, any fields and normal methods. An interface has constants and abstract methods, plus default and static methods since Java 8 and private methods since Java 9.

Q14. Can we override a static method?

No. Static methods belong to the class, not to objects. If a child class declares the same static method, it hides the parent method. The call is decided by the reference type, not the object.

Q15. What do this and super mean?

this refers to the current object, often used to tell a field from a parameter: this.name = name. super refers to the parent class, used to call its constructor, super(name), or its methods.

Q16. What does the final keyword do?

A final variable cannot be reassigned. A final method cannot be overridden. A final class, such as String, cannot be extended.

Q17. What are the access modifiers in Java?

private is visible only inside the class. No modifier (default) is visible in the same package. protected adds subclasses in other packages. public is visible everywhere.

Q18. Why does Java not support multiple inheritance with classes?

To avoid the diamond problem: if two parent classes have the same method, the child would not know which one to use. Java allows multiple inheritance of type through interfaces instead.

More OOP practice is in our Java OOP interview questions.

Collections questions (Q19 to Q26)

Q19. What is the Collections Framework?

A set of interfaces and classes for storing groups of objects. The main interfaces are List, Set, Queue and Map. Common classes are ArrayList, HashSet, ArrayDeque and HashMap.

Q20. What is the difference between ArrayList and LinkedList?

ArrayList uses a resizable array, so reading by index is fast. LinkedList uses linked nodes, so adding or removing at the ends is cheap, but reading by index is slow. Most code uses ArrayList.

Q21. What is the difference between List and Set?

A List keeps insertion order and allows duplicates. A Set does not allow duplicates. HashSet has no order, LinkedHashSet keeps insertion order and TreeSet keeps items sorted.

Q22. How does HashMap work internally?

It stores entries in an array of buckets. hashCode() of the key picks the bucket, and equals() finds the exact key inside it. When a bucket gets too many entries, Java 8 and later turn it into a balanced tree. Read how HashMap works internally for detail.

Q23. What is the difference between HashMap and Hashtable?

HashMap is not synchronized and allows one null key and null values. Hashtable is synchronized, allows no nulls and is a legacy class. For thread-safe maps today, use ConcurrentHashMap.

Q24. What is the equals and hashCode contract?

If two objects are equal by equals(), they must return the same hashCode(). Two objects with the same hash code need not be equal. If you override equals() without hashCode(), HashSet and HashMap stop working correctly.

Q25. What is the difference between Comparable and Comparator?

Comparable gives a class one natural order through compareTo(), written inside the class. Comparator defines extra orders outside the class, such as Comparator.comparing(Student::getMarks).

Q26. What is a fail-fast iterator?

An iterator that throws ConcurrentModificationException if the collection changes while you loop over it, except through the iterator's own remove(). ArrayList and HashMap iterators are fail-fast.

Exception handling questions (Q27 to Q32)

Q27. What is the difference between checked and unchecked exceptions?

Checked exceptions, such as IOException, must be caught or declared with throws, or the code will not compile. Unchecked exceptions extend RuntimeException, such as NullPointerException, and the compiler does not force you to handle them.

Q28. What is the difference between throw and throws?

throw actually throws one exception object: throw new IllegalArgumentException("Age must be positive"). throws in a method signature tells callers which checked exceptions the method may pass up.

Q29. Does finally always run?

Almost always. It runs whether the try block finishes normally, throws or returns. It does not run if the JVM stops, for example with System.exit(), or if the program crashes.

Q30. What is try-with-resources?

A try statement that declares resources, such as a file reader, in brackets. Java closes them automatically at the end, even if an exception occurs. The resource must implement AutoCloseable.

Q31. How do you create a custom exception?

Extend Exception for a checked one or RuntimeException for an unchecked one, and pass a message to super. For example, class InsufficientBalanceException extends RuntimeException.

Q32. What is the difference between final, finally and finalize?

final is a keyword for constants, methods and classes. finally is a block that runs after try. finalize() was a method called before garbage collection; it is deprecated and should not be used.

See the Java exception handling questions for more.

Java 8, threads and memory (Q33 to Q40)

Q33. What is a lambda expression?

A short way to write a function without a name: (a, b) -> a + b. Lambdas make code for sorting, filtering and event handling much shorter than anonymous classes.

Q34. What is a functional interface?

An interface with exactly one abstract method, such as Runnable, Comparator or Predicate. A lambda can be used wherever a functional interface is expected. @FunctionalInterface asks the compiler to check the rule.

Q35. What is the Stream API?

A way to process collections in a pipeline: list.stream().filter(m -> m >= 40).map(...).toList(). Intermediate operations like filter are lazy; nothing runs until a terminal operation such as collect, count or toList.

Q36. What is Optional?

A container that may or may not hold a value. Returning Optional<Student> instead of null makes the caller deal with the empty case, using methods like orElse() or ifPresent().

Q37. How do you create a thread in Java?

Extend Thread and override run(), or implement Runnable and pass it to a Thread. Then call start(), not run(). In real projects, an ExecutorService manages a pool of threads for you.

Q38. What does synchronized do?

It lets only one thread at a time run a block or method on the same lock. This prevents a race condition, where two threads update shared data together and one update is lost.

Q39. What is the difference between stack and heap memory?

Each thread has its own stack for method calls and local variables. The heap is shared and holds all objects. A local variable Student s lives on the stack, while the Student object it points to lives on the heap.

Q40. What is garbage collection?

The JVM automatically frees heap memory used by objects that no reference can reach any more. You cannot force it; System.gc() is only a request. You can help by not holding references you no longer need.

Core Java MCQ and viva questions

Many written tests include a core Java MCQ round. Try this one before you read the output.

public class Quiz {
    static int count = 0;

    Quiz() { count++; }

    public static void main(String[] args) {
        new Quiz();
        new Quiz();
        String s1 = "Java";
        String s2 = "Ja" + "va";
        System.out.println(count);
        System.out.println(s1 == s2);
        System.out.println(10 + 20 + "Java" + 10 + 20);
    }
}
2
true
30Java1020

count is static, so both objects share it. "Ja" + "va" is joined at compile time, so it uses the pooled literal. In the last line, numbers add until the string appears; after that, everything is joined as text.

Core Java viva questions are shorter. Practise one-line answers to these:

Viva question One-line answer
What is bytecode? The .class output of javac that the JVM runs
What is a package? A folder-like group of related classes
What is a wrapper class? An object form of a primitive, such as Integer for int
What is autoboxing? Automatic conversion from int to Integer and back
What is an instance variable? A field that each object gets its own copy of

Core Java interview questions for freshers: one-week plan

  1. Day 1: Q1 to Q8. Write a program that shows == against equals().
  2. Day 2: Q9 to Q18. Explain each OOP pillar with your own project.
  3. Day 3: Q19 to Q26. Use HashMap and TreeSet in a small program.
  4. Day 4: Q27 to Q32. Write a custom exception for a bank account.
  5. Day 5: Q33 to Q40. Rewrite three loops as streams.
  6. Day 6: Revise with our core Java cheat sheet and solve five core Java practice problems.
  7. Day 7: A mock interview with a friend. Swap roles.

Pro tip: After every answer, expect "Can you give an example?" or "Where did you use this?" Link at least ten of these questions to code in your own project.

For a wider set that includes SQL and Spring Boot, see our Java interview questions for freshers. The Java full stack course guide shows what else full stack interviews cover. In CodeBegun's Java Full Stack with AI course, batches are capped at 27 students, so mock interviews come with feedback on your own answers.

Frequently asked questions

How many Core Java questions should a fresher prepare?

Prepare these 40 well rather than 300 badly. Interviewers usually ask 10 to 15 Core Java questions and then follow up on your answers, so depth matters more than count.

What are common Core Java viva questions?

Viva questions are usually short definitions: what is JVM, what is a class, what is inheritance, what is an exception, and the difference between == and equals. Answer in one or two sentences and add an example if asked.

Do freshers need to know multithreading for interviews?

Know the basics: two ways to create a thread, what synchronized does and what a race condition is. Deep concurrency questions are more common for experienced candidates.

Should I learn Spring Boot before a Core Java interview?

Not first. Fresher rounds test Core Java, SQL and coding before frameworks. For full stack roles, add Spring Boot basics after you are confident with these 40 questions.

How do I answer a Java question I do not know?

Say so honestly, then share what you do know that is related, or how you would find out. Interviewers respect a clear 'I am not sure, but I think it works like this' far more than a confident wrong answer.


Want to know how ready you are for a Java interview? Book a free demo class in our Madhapur, Hyderabad classroom or live online, or see the Java Full Stack with AI course.

Found this useful? Share it: LinkedIn Facebook WhatsApp

Written by

Siva Prasad Galaba

Staff Engineer & Founder, CodeBegun

Founder of CodeBegun. 15+ years building Java systems at companies like Crunchyroll. Teaching the next generation to code the way the industry actually works.

Back to all articles
Chat with us