Core Java fundamentals interview questions 14 questions in 6 chapters
- 01The platform
- 02Object-oriented basics5 questions
- 03Objects, strings and numbers4 questions
- 04Exceptions
- 05Collections
- 06Sources
The platform
1.1 What are the JDK, the JRE and the JVM, and how does Java code run? Easy 2 min
The JVM is the virtual machine that runs Java bytecode. The JRE was the JVM plus the standard libraries, which is what you needed to run an app, and the JDK adds the tools to develop: the javac compiler, jar, jshell, debugging and monitoring tools. javac compiles my source into bytecode in class files, the JVM loads those classes, interprets the bytecode at first and then the JIT compiler turns hot methods into native machine code. That is why the same jar runs on any operating system that has a JVM.
Read the full answer
Object-oriented basics
2.1 What are the four principles of object-oriented programming in Java? Easy 2 min
Encapsulation, inheritance, polymorphism and abstraction. Encapsulation hides an object's state behind methods, so I keep fields private and validate in setters or constructors. Inheritance lets a class reuse and extend another with extends. Polymorphism means one reference type, many behaviours: a PaymentMethod variable can hold a Card or a Wallet, and the right pay method runs at runtime. Abstraction means exposing what an object does, through interfaces and abstract classes, and hiding how.
Read the full answer2.2 What is the difference between method overloading and method overriding? Easy 1 min
Overloading is several methods with the same name in one class but different parameter lists; the compiler picks one at compile time from the argument types. Overriding is a subclass giving its own version of an inherited instance method with the same signature; the JVM picks it at runtime from the object's actual class. Overriding can't reduce visibility or throw broader checked exceptions, and I always add @Override so the compiler catches mistakes. Static methods are hidden, not overridden.
Read the full answer2.3 What is the difference between an interface and an abstract class? Easy 1 min
An abstract class can have state, constructors and any access level, and a class can extend only one. An interface defines a contract: it has no instance fields, but since Java 8 it can have default and static methods, and a class can implement many interfaces. I use an interface for a capability many unrelated classes share, like Comparable or PaymentMethod, and an abstract class when closely related classes share code and fields.
Read the full answer2.4 What do the access modifiers and the static keyword mean? Easy 1 min
There are four access levels: public is visible everywhere, protected is visible in the same package and in subclasses, package-private, with no keyword, is visible only in the same package, and private is visible only inside the class. Static means the member belongs to the class, not to an object, so there is one copy shared by all instances, and static methods can't use this or instance fields. I keep fields private and use the smallest access that works.
Read the full answer2.5 Is Java pass-by-value or pass-by-reference? Easy 1 min
Java is always pass-by-value. For a primitive, the method gets a copy of the value. For an object, the method gets a copy of the reference, so it can change the object's contents through it, but if it points the parameter at a new object, the caller doesn't see that. So mutating a list inside a method is visible outside; reassigning the parameter is not.
Read the full answer
Objects, strings and numbers
3.1 What is the difference between == and equals(), and why must hashCode() match equals()? Easy 2 min
For objects, == checks whether two references point to the same object, and equals checks whether they are logically equal. Object's default equals is the same as ==, so value classes override it. The contract says that equal objects must have the same hashCode, because HashMap and HashSet first use the hash to find a bucket and then call equals. If I override equals without hashCode, two equal keys land in different buckets and the map can't find them. Records generate both for me.
Read the full answer3.2 Why is String immutable, and when do you use StringBuilder or StringBuffer? Easy 2 min
A String can't change after it's created; methods like concat or toUpperCase return a new String. That makes strings safe to share between threads, safe as HashMap keys, safe for things like file paths and class names, and it lets the JVM reuse literals from the string pool. When I build a string in a loop I use StringBuilder, which is mutable and fast. StringBuffer is the old synchronized version; it's rarely needed now.
Read the full answer3.3 What is the difference between primitives and wrapper classes, and what is autoboxing? Easy 2 min
Primitives like int, long and boolean hold the value directly, can't be null and are cheap. Wrappers like Integer are objects, so they can be null and can go into collections. Autoboxing converts int to Integer automatically and unboxing does the reverse. Two traps: unboxing a null Integer throws NullPointerException, and comparing two Integers with == checks identity, which happens to work for -128 to 127 because of the cache but fails for 128. Always compare wrappers with equals.
Read the full answer3.4 What is the difference between final, finally and finalize? Easy 2 min
They're unrelated. final is a keyword: a final variable can't be reassigned, a final method can't be overridden and a final class can't be extended. finally is the block after try that always runs, whether or not an exception was thrown, so it's for cleanup. finalize was a method the garbage collector might call before freeing an object; it's unreliable, deprecated for removal since Java 18, and replaced by try-with-resources and Cleaner.
Read the full answer
Exceptions
4.1 What is the difference between checked and unchecked exceptions? Easy 1 min
Checked exceptions, like IOException, must be caught or declared with throws, and the compiler enforces it; they're for problems the caller can reasonably recover from. Unchecked exceptions are RuntimeException and its subclasses, like NullPointerException or IllegalArgumentException; they usually mean a bug or bad input, and the compiler doesn't force handling. Errors like OutOfMemoryError are serious JVM problems that an application normally shouldn't catch.
Read the full answer4.2 How do try-catch-finally and try-with-resources work? Easy 1 min
try holds the code that might fail, catch handles a specific exception type, and finally always runs for cleanup. try-with-resources is the better way to clean up: anything that implements AutoCloseable, like a stream or a JDBC connection, is declared in the try parentheses and closed automatically in reverse order, even when an exception is thrown. If close also throws, that exception is kept as a suppressed exception instead of hiding the original one.
Read the full answer
Collections
5.1 What are the main collection types, List, Set, Map and Queue, and when do you use each? Easy 1 min
A List is ordered and allows duplicates, so I use ArrayList by default. A Set has no duplicates: HashSet for fast lookup, LinkedHashSet to keep insertion order, TreeSet to keep it sorted. A Map stores key-value pairs: HashMap by default, LinkedHashMap for insertion order, TreeMap for sorted keys. A Queue or Deque is for processing in order, and ArrayDeque is the usual choice for a stack or queue. For concurrency I use the java.util.concurrent versions like ConcurrentHashMap.
Read the full answer5.2 What is the difference between Comparable and Comparator? Easy 1 min
Comparable defines a class's natural order inside the class itself, with one compareTo method, like String sorting alphabetically. Comparator is a separate object that defines any other order, so I can have many: by salary, by name, reversed. Since Java 8 I build them with Comparator.comparing, thenComparing and reversed. TreeMap, TreeSet and sort use whichever I give them, and the order should be consistent with equals.
Read the full answer
Sources
- JVM Specification, chapter 1: Introduction
- dev.java: Getting started with Java
- dev.java: Object-oriented programming concepts
- Java Language Specification, chapter 5: Conversions and contexts
- Java Language Specification, chapter 8: Classes
- Java Language Specification, chapter 11: Exceptions
- Java Language Specification, chapter 14: Blocks, statements and patterns
- Java tutorial: Overriding and hiding methods
- Java tutorial: Abstract methods and classes
- dev.java: Abstract methods and classes
- Java tutorial: Controlling access to members of a class
- Java tutorial: Understanding class members
- Java tutorial: Passing information to a method or a constructor
- Javadoc: java.lang.Object
- Javadoc: java.lang.String
- Javadoc: java.lang.StringBuilder
- Javadoc: java.lang.Integer
- JEP 421: Deprecate finalization for removal
- Java tutorial: Unchecked exceptions, the controversy
- Java tutorial: The try-with-resources statement
- Javadoc: java.lang.AutoCloseable
- dev.java: Catching and handling exceptions
- Javadoc: Collections framework overview
- Javadoc: java.lang.Comparable
- Javadoc: java.util.Comparator