Basic Java questions
1. What is the difference between JDK, JRE and JVM?
The JVM runs Java bytecode and makes Java platform independent. The JRE is the JVM plus the core libraries needed to run programs. The JDK is the JRE plus development tools such as the compiler (javac), debugger and jar tool.
2. What are the four pillars of OOP in Java?
Encapsulation (hiding state behind methods), inheritance (reusing a parent class with extends), polymorphism (one interface, many implementations through overriding and overloading) and abstraction (exposing what an object does through abstract classes and interfaces, not how).
3. What is the difference between == and equals()?
== compares references for objects (whether both point to the same object) and values for primitives. equals() compares logical content; String, Integer and most library classes override it. If you override equals() you must also override hashCode().
4. Why is String immutable in Java?
Immutability lets strings be safely shared in the string pool, cached for hashCode, used as HashMap keys and passed between threads without synchronization. It also prevents security issues when strings hold file paths, URLs or credentials.
Intermediate Java questions
5. How does HashMap work internally?
HashMap stores entries in an array of buckets. The key's hashCode() is spread and mapped to a bucket index; collisions are stored as a linked list, which is converted to a balanced tree when a bucket grows beyond 8 entries (Java 8+). When size exceeds capacity times the load factor (0.75) the table is doubled and entries are rehashed.
6. What is the difference between checked and unchecked exceptions?
Checked exceptions (IOException, SQLException) extend Exception and must be caught or declared with throws; the compiler enforces it. Unchecked exceptions extend RuntimeException (NullPointerException, IllegalArgumentException) and usually indicate programming errors, so handling is optional.
7. ArrayList vs LinkedList: when do you use which?
ArrayList is backed by a resizable array, giving O(1) random access and cache-friendly iteration; inserts in the middle are O(n). LinkedList is a doubly linked list with O(1) insertion at the ends but O(n) access by index. In practice ArrayList is the default choice.
8. What are Java 8 streams and lambdas used for?
Lambdas are concise implementations of functional interfaces. Streams process collections declaratively with operations like filter, map, sorted and collect. Intermediate operations are lazy and only run when a terminal operation such as collect or forEach is called.
Advanced Java questions
9. What is the difference between synchronized and ReentrantLock?
Both provide mutual exclusion. synchronized is simpler and released automatically when the block exits. ReentrantLock adds tryLock with timeouts, interruptible locking, fairness and multiple Condition objects, but it must be unlocked in a finally block.
10. How does Spring Boot dependency injection work?
Spring scans classes annotated with @Component, @Service, @Repository or @Configuration beans and creates them in the application context. Dependencies are injected, preferably through constructors, by type. Auto-configuration then wires common beans such as DataSource or web servers based on the classpath and properties.
Want to practise these with a trainer? APEX live batches include mock interviews, real projects and placement support. See upcoming batches.
