Core Java Interview Questions

Naveen AnamaneniUpdated · 7 min read

In a viva at the end of a practical exam, the examiner doesn't ask you to write a whole program. They ask short questions: "What is the difference between these two?", "Why does this work?". The first technical round of a Java interview feels much the same. It checks that the basics are solid before moving to harder topics. This page collects the core Java questions asked most often, grouped from basic to advanced. Try to answer each one aloud first, then open the answer and compare.

How to Answer

A good interview answer has three parts: a one-line definition, one key detail or difference, and a short example or use. Thirty seconds is usually enough. If the interviewer wants more, they will ask.

Basic Questions

What is the difference between JDK, JRE and JVM?

The JVM runs bytecode. The JRE is the JVM plus the standard class libraries needed to run programs. The JDK is the JRE plus development tools such as javac, jar and jshell. Since Java 11, Oracle ships only the JDK; you can build a small runtime with jlink.

Why is Java called platform independent?

javac compiles source code to bytecode, not to machine code for one processor. Any operating system with a JVM can run the same .class files: "write once, run anywhere". The JVM itself is platform specific.

What are the primitive types in Java?

Eight: byte, short, int, long (whole numbers), float, double (decimals), char (a UTF-16 character) and boolean. Everything else is a reference type.

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

== compares primitive values, or for objects, whether two references point to the same object. equals() compares contents, as defined by the class. String, wrapper classes and records override equals to compare values.

Why is main() 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 (use System.exit(code) for an exit status). Since Java 25, compact source files may also use an instance void main().

Intermediate Questions

Is Java pass-by-value or pass-by-reference?

Always pass-by-value. For objects, the value passed is a copy of the reference, so a method can change the object's fields but can't make the caller's variable point to a different object. See the program below.

What does the static keyword mean?

A static member belongs to the class, not to any object. There is one copy of a static field shared by all objects, and static methods can be called without an object. Static methods can't use this or instance fields directly.

What are the uses of final?

A final variable can be assigned only once; a final method can't be overridden; a final class can't be extended (String is final). Note that a final reference to a list still allows changing the list's contents.

What is the difference between final, finally and finalize?

final is a modifier (see above). finally is the block after try that always runs, used for cleanup. finalize() was a method the garbage collector might call before freeing an object; it is deprecated for removal and should never be used. Use try-with-resources or Cleaner instead.

What are wrapper classes, and why are they needed?

Integer, Double, Character and the others wrap primitives in objects. Collections and generics work only with objects, so List<Integer> needs them. Autoboxing converts between the two automatically.

What is the difference between a constructor and a method?

A constructor has the class's name and no return type, runs once when an object is created with new, and initializes it. A method has a return type (or void), any name, and can be called many times. Constructors aren't inherited.

Advanced Questions

Why is String immutable in Java?

So that strings can be shared safely in the string pool, used as reliable HashMap keys (the hash code is cached), passed around threads without locking, and trusted in security checks such as file paths and class names.

What is the difference between stack and heap memory?

Each thread has its own stack, holding method call frames with local variables and references. The heap is shared by all threads and holds every object. Stack memory is freed when a method returns; heap objects are freed by the garbage collector.

Can you override a static or private method?

No. Static methods are hidden, not overridden: the call is chosen by the variable's declared type. Private methods aren't visible to subclasses, so a method with the same name in a subclass is a new, unrelated method.

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

A class can extend only one abstract class but implement many interfaces. Abstract classes can have instance fields, constructors and any access level; interfaces have no instance state and their abstract methods are public, though they can have default, static and private methods. Use an interface for a capability and an abstract class for shared state and code.

Seeing Pass by Value in Code

Let's see the program behind the pass-by-value question.

PassByValue.java▶ Try it
public class PassByValue {
    static class Student {
        String name;
 
        Student(String name) {
            this.name = name;
        }
    }
 
    static void rename(Student s) {
        s.name = "Srihitha";                 // changes the shared object
    }
 
    static void replace(Student s) {
        s = new Student("Advitha");          // changes only the local copy
    }
 
    static void addOne(int n) {
        n++;                                  // changes only the local copy
    }
 
    public static void main(String[] args) {
        Student student = new Student("Navika");
        rename(student);
        System.out.println(student.name);
        replace(student);
        System.out.println(student.name);
 
        int marks = 90;
        addOne(marks);
        System.out.println(marks);
    }
}
Output
Srihitha
Srihitha
90

Explanation: rename receives a copy of the reference, which still points to the same object, so changing s.name is visible to the caller. replace points its own copy of the reference at a new object; the caller's variable is untouched. addOne changes a copy of the int. In every case, Java copied a value into the parameter.

Interview Corner

What should you do when you don't know an answer?

Say so honestly, then reason aloud from what you do know: "I haven't used that, but since HashMap uses hashCode, I'd expect..." Interviewers respect clear reasoning far more than a confident guess that turns out wrong.

How long should an answer be?

Around thirty seconds: a definition, one key difference or detail, and an example. Stop there and let the interviewer ask for more.

Practice

  1. Easy: Answer the five basic questions aloud, timing yourself. Aim for under 30 seconds each.
  2. Harder: Write a small program that proves final on a List variable doesn't make the list unmodifiable, and then fix it with List.copyOf.

Quick check: Core Java

4 questions
1Which contains javac?
2How does Java pass object arguments?
3What can't a final class do?
4Where are objects stored?

Key Takeaways

Was this tutorial helpful?

Lesson 569 of 578