Core Java Interview Questions & Answers
1. What are the key features introduced in JDK 8?
- Lambda Expressions, Stream API, Default & Static Methods, Optional Class, New Date/Time API
2. What is Stream API?
Stream API processes collections using functional programming (filtering, mapping, sorting) with parallel execution.
3. Explain any 5 methods of Object class.
- equals(): Compares objects
- hashCode(): Returns unique integer
- toString(): Returns string representation
- clone(): Creates object copy
- finalize(): Called before garbage collection
4. In which scenarios should hashCode() and equals() be implemented?
- Sets (HashSet, LinkedHashSet)
- Maps (HashMap, Hashtable) for key uniqueness
5. Which annotations are used in Core Java?
- @Override, @Deprecated, @SuppressWarnings, @FunctionalInterface
6. How does Lambda (->) work internally?
Lambda expressions use 'invokedynamic' in JVM, avoiding object creation.
7. Give Examples of Functional Interfaces in Java.
- Runnable, Callable, Predicate, Consumer, Function
8. Why is Comparable not a functional interface?
It inherits equals() and hashCode() from Object.
9. What is the use of start() method in Thread?
It starts a new thread and calls run() in a separate stack.
10. What is the use of run() method?
It defines the logic for a thread but runs in the same stack if called directly.
11. What is a Daemon Thread?
A background thread (e.g., Garbage Collector) that stops when non-daemon threads finish.
12. When is finalize() method called?
Before garbage collection, but not guaranteed immediately.
13. When is a thread safe? How to ensure thread safety?
- Using synchronized, atomic variables, and concurrent collections.
14. What is transient keyword?
Prevents a variable from being serialized.
15. What is ResultSet in JDBC?
Stores results of a database query.
16. Difference between Statement and PreparedStatement?
- Statement: Dynamic SQL, slow, prone to SQL Injection
- PreparedStatement: Precompiled, fast, prevents SQL Injection
17. What are the drawbacks of JDBC?
- Boilerplate Code
- Performance Issues
- Difficult Object Mapping
18. Difference between Comparable and Comparator?
- Comparable: compareTo(), single sorting logic
- Comparator: compare(), multiple sorting logics
19. Difference between compare() and compareTo() method?
- compareTo(): Compares current object with another
- compare(): Compares two objects
20. What is an Anonymous Class?
A class without a name, created inline.