The document contains a list of 100 core Java interview questions aimed at senior developers, covering essential concepts such as OOP principles, data structures, memory management, and exception handling. Key topics include the differences between `==` and `equals()`, synchronization, thread pools, and the use of streams. It also addresses advanced features like functional interfaces, garbage collection, and the creation of immutable classes.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
2 views2 pages
Java_100_Core_Questions
The document contains a list of 100 core Java interview questions aimed at senior developers, covering essential concepts such as OOP principles, data structures, memory management, and exception handling. Key topics include the differences between `==` and `equals()`, synchronization, thread pools, and the use of streams. It also addresses advanced features like functional interfaces, garbage collection, and the creation of immutable classes.
The four pillars are Encapsulation, Abstraction, Inheritance, and Polymorphism. Encapsulation hides internal state and only exposes operations. Abstraction lets me interact with complex systems via simple interfaces. Inheritance allows reuse and extension of code. Polymorphism means I can use one interface for multiple implementations, either through method overloading or overriding.
2. What is the difference between == and equals()?
`==` compares object references, while `equals()` compares values. For example, two different String objects with the same text will return false using `==` but true using `equals()`.
3. Why must you override hashCode() when overriding equals()?
Because hash-based collections like HashMap rely on consistent hash codes. If equals() returns true, hashCode() must be the same to locate the object correctly.
4. What’s the difference between ArrayList and LinkedList?
ArrayList is backed by an array, making random access faster (O(1)), but insertions/removals are slower. LinkedList is better for frequent add/remove operations as it doesn’t require shifting elements.
5. What is the Java Memory Model (JMM)?
It defines how threads interact through memory. It guarantees visibility and ordering. You use `volatile`, `synchronized`, or higher-level concurrency APIs to work within it safely.
6. How does synchronization work in Java?
`synchronized` ensures mutual exclusion by locking on the object monitor. Only one thread can enter a synchronized block or method per object at a time.
7. What are the different types of thread pools in Java?
There are fixed, cached, single-threaded, and scheduled thread pools. I use `Executors.newFixedThreadPool()` when I want a limited number of concurrent threads.
8. What’s the difference between final, finally, and finalize()?
`final` prevents modification, `finally` is a block that always executes, and `finalize()` is a method called by the GC before object collection (though it's deprecated now).
9. What is autoboxing and unboxing?
Autoboxing converts primitives to wrapper classes automatically. Unboxing does the reverse. For example, int to Integer and back during assignments or method calls. 10. How would you handle nulls using Optional? Optional lets me express absence explicitly. I can use `Optional.ofNullable(obj).orElse(default)` or `ifPresent()` to avoid null pointer exceptions.
11. What is a functional interface?
A functional interface has exactly one abstract method. It's the foundation for lambda expressions. For example, Runnable or Comparator.
12. Explain the difference between map() and flatMap() in streams.
`map()` transforms elements one-to-one, while `flatMap()` flattens nested structures. It's useful when dealing with streams of lists or optionals.
13. How do you create an immutable class in Java?
Make the class final, mark all fields private and final, initialize them in the constructor, and avoid setters. Return copies for mutable objects like Date or List.
14. How does garbage collection work in Java?
GC automatically reclaims memory by removing unreachable objects. It works in generations (young, old) and uses collectors like G1 or ZGC depending on configuration.
15. How do you make a class Singleton?
Using a private constructor and a static instance. In Java, I prefer using an enum or static holder pattern for thread safety and serialization safety.
16. How would you use streams to process a list of users by age? `users.stream().filter(u -> u.getAge() > 30).collect(Collectors.toList());` filters users over 30 and collects them.
17. What’s the use of transient keyword?
It marks a field to be skipped during serialization. Useful when a field is sensitive or not serializable.
18. What is the difference between checked and unchecked exceptions?
Checked exceptions must be handled or declared, like IOException. Unchecked ones, like NullPointerException, are RuntimeExceptions and need not be declared.
19. What is the default value of int and boolean in Java?
`int` defaults to 0, `boolean` to false when used as class fields. Local variables must be initialized before use.
20. What is method overloading vs overriding?
Overloading means same method name, different parameters. Overriding means a subclass redefines a parent method with the same signature.