Java Topic
Java Interview Questions 1
Core Java Concepts
1. What is the difference between JDK, JRE, and JVM?
-
JVM (Java Virtual Machine): Runtime environment that executes Java bytecode
-
JRE (Java Runtime Environment): Contains JVM + libraries to run Java applications
-
JDK (Java Development Kit): Contains JRE + development tools (compiler, debugger)
2. Explain public static void main(String args[])
-
public: Accessible from anywhere -
static: Can be called without creating class instance -
void: Doesn't return any value -
main: Entry point method name -
String args[]: Command-line arguments
3. Difference between == and equals() method
-
==: Compares object references (memory address) -
equals(): Compares object content (should be overridden for value comparison)
4. What is the difference between ArrayList and LinkedList?
-
ArrayList: Dynamic array, fast random access, slow insertions/deletions
-
LinkedList: Doubly-linked list, slow random access, fast insertions/deletions
OOP Concepts
5. Explain OOP principles in Java
-
Encapsulation: Binding data and methods together, data hiding
-
Inheritance: IS-A relationship, code reuse
-
Polymorphism: Same interface, different implementations
-
Abstraction: Hiding complex reality, exposing essentials
6. Difference between abstract class and interface
-
Abstract class: Can have abstract and concrete methods, constructors, instance variables
-
Interface: All methods abstract (until Java 8), no constructors, only constants
7. What is constructor chaining?
Calling one constructor from another using this() or super()
public class Person {
public Person() {
this("Unknown"); // Calls parameterized constructor
}
public Person(String name) {
this.name = name;
}
}
Exception Handling
8. Difference between checked and unchecked exceptions
-
Checked: Compile-time exceptions (IOException, SQLException)
-
Unchecked: Runtime exceptions (NullPointerException, ArrayIndexOutOfBounds)
9. What is the finally block and when is it used?
-
Always executes whether exception occurs or not
-
Used for cleanup code (closing resources)
10. Can we have try block without catch or finally?
No, try must be followed by either catch or finally block.
Multithreading
11. Different ways to create threads
-
Extending Thread class
-
Implementing Runnable interface
-
Using Executor framework
12. What is synchronization?
-
Mechanism that allows only one thread to access shared resource at a time
-
Prevents race conditions using
synchronizedkeyword
13. Difference between wait() and sleep()
-
wait(): Releases lock, called on Object, requires notify() to wake up
-
sleep(): Doesn't release lock, called on Thread, wakes up after timeout
Collections Framework
14. Difference between HashMap and Hashtable
-
HashMap: Not synchronized, allows one null key, faster
-
Hashtable: Synchronized, doesn't allow null keys, thread-safe
15. What is the contract between equals() and hashCode()?
-
If two objects are equal by equals(), they must have same hashCode()
-
Same hashCode doesn't guarantee objects are equal
16. How does HashSet store elements internally?
Uses HashMap internally where elements are stored as keys and a dummy object as value.
Java 8+ Features
17. What are lambda expressions?
Anonymous functions that enable functional programming:
// Instead of anonymous class
Collections.sort(list, (a, b) -> a.compareTo(b));
18. Explain Stream API
Processing sequences of elements with functional operations:
list.stream()
.filter(n -> n % 2 == 0)
.map(n -> n * 2)
.collect(Collectors.toList());
19. What are optional classes?
Container object that may or may not contain non-null value, helps avoid NullPointerException.
Memory Management
20. What is garbage collection in Java?
-
Automatic memory management process
-
JVM automatically reclaims memory from unused objects
-
Runs in background via garbage collector threads
Advanced Concepts
21. Difference between String, StringBuilder, and StringBuffer
-
String: Immutable, thread-safe
-
StringBuilder: Mutable, not thread-safe, faster
-
StringBuffer: Mutable, thread-safe, slower
22. What is serialization and deserialization?
-
Serialization: Converting object to byte stream
-
Deserialization: Converting byte stream back to object
-
Implement Serializable interface
23. Explain Java memory model (Heap vs Stack)
-
Heap: Stores objects, instance variables, shared among threads
-
Stack: Stores local variables, method calls, thread-specific
24. What are annotations in Java?
Metadata about code that can be processed at compile-time or runtime:
@Override
@Deprecated
@SuppressWarnings
Design Patterns & Best Practices
25. What is the singleton pattern and how to implement it?
Ensures only one instance of class exists:
public class Singleton {
private static Singleton instance;
private Singleton() {}
public static Singleton getInstance() {
if (instance == null) {
instance = new Singleton();
}
return instance;
}
}
These questions cover fundamental to advanced Java concepts that are commonly asked in technical interviews, focusing on practical understanding and real-world application of Java principles.
Comments (0)
No comments yet. Be the first to comment!