- Регистрация
- 1 Мар 2015
- Сообщения
- 1,481
- Баллы
- 155
Whether you're a fresher trying to land your first job or an experienced developer aiming for a senior role, knowing the right Java interview questions — and how to answer them well — can make all the difference.
Below are 40 essential Java questions with detailed answers that cover everything from core concepts to advanced topics.
? 1. What is the difference between JDK, JRE, and JVM?
Answer:
Answer:
Answer:
Answer:
Java has:
Answer:
Answer:
Answer:
Answer:
A constructor is a special method to initialize objects. Constructors are not inherited, but a subclass can call the parent constructor using super().
? 9. What is the use of final keyword?
Answer:
Answer:
Answer:
Abstraction hides implementation details. Achieved using:
Answer:
Answer:
Encapsulation hides internal object state using private fields and public getters/setters. It protects data integrity.
? 14. What is inheritance?
Answer:
Inheritance allows a class (child) to inherit properties/methods from another (parent). Promotes reusability.
? 15. What is polymorphism?
Answer:
Polymorphism allows the same interface to behave differently:
Answer:
Answer:
Answer:
A static block initializes static data. Executes once when the class is loaded.
? 19. Can you override static methods?
Answer:
No. Static methods belong to the class and not the instance, so they can’t be overridden. They can be hidden, though.
? 20. What is the purpose of this keyword?
Answer:
this refers to the current object instance. Commonly used to avoid naming conflicts and pass current object.
? 21. What is garbage collection in Java?
Answer:
Automatic memory management. Unused objects are cleaned up by the Garbage Collector (GC).
? 22. How do you force garbage collection?
Answer:
You can suggest GC using System.gc(), but the JVM is not guaranteed to perform it immediately.
? 23. What are exceptions in Java?
Answer:
Unexpected events that disrupt normal flow. Java has:
Answer:
Answer:
Introduced in Java 7. Ensures automatic closing of resources like FileReader, BufferedReader, etc.
try (BufferedReader br = new BufferedReader(new FileReader("file.txt"))) {
// read file
}
? 26. What is multithreading?
Answer:
Multithreading allows concurrent execution of two or more threads. Improves performance for I/O-heavy operations.
? 27. What is the difference between Runnable and Thread?
Answer:
Answer:
Synchronization controls access to shared resources to prevent thread interference or memory consistency errors.
? 29. What is a deadlock?
Answer:
Occurs when two or more threads are waiting on each other to release locks. They get stuck indefinitely.
? 30. What is a lambda expression?
Answer:
Introduced in Java 8. It enables writing anonymous methods concisely.
(a, b) -> a + b
? 31. What are functional interfaces?
Answer:
Interfaces with only one abstract method. Example: Runnable, Callable, Comparator, Function
? 32. What is the Stream API?
Answer:
Introduced in Java 8. Used to process collections in a functional style (filter, map, reduce, etc.).
? 33. What is Optional in Java 8?
Answer:
A container that may or may not hold a non-null value. Helps avoid NullPointerException.
Optional<String> name = Optional.ofNullable("John");
? 34. What is the difference between Comparable and Comparator?
Answer:
Answer:
Marks a variable not to be serialized. Used when saving object state to file.
? 36. What is the volatile keyword?
Answer:
Ensures visibility of changes to variables across threads. Prevents caching by threads.
? 37. What is the difference between stack and heap memory?
Answer:
Answer:
Metadata that provide information to the compiler or runtime (e.g., @Override, @Deprecated, @FunctionalInterface)
? 39. What are design patterns in Java?
Answer:
Reusable solutions to common software problems. Examples:
Answer:
? Final Tip: Don’t just memorize answers. Understand the why behind each concept and use hands-on examples to reinforce your knowledge.
Below are 40 essential Java questions with detailed answers that cover everything from core concepts to advanced topics.
? 1. What is the difference between JDK, JRE, and JVM?
Answer:
- JVM (Java Virtual Machine): Runs Java bytecode. It's platform-dependent but provides platform independence at the code level.
- JRE (Java Runtime Environment): Contains the JVM + libraries for running Java applications.
- JDK (Java Development Kit): JRE + development tools (compiler, debugger). Used to write and compile Java code.
Answer:
- Object-Oriented
- Platform Independent
- Robust (Garbage Collection, Exception Handling)
- Secure (Bytecode verifier, no pointers)
- Multithreaded
- Portable
- High Performance (JIT Compiler)
Answer:
- == compares references (memory addresses).
- .equals() compares values/content. Override .equals() to customize this behavior in your class.
Answer:
Java has:
- Primitive types: byte, short, int, long, float, double, char, boolean
- Reference types: objects, arrays, strings
Answer:
- Autoboxing: Primitive to wrapper class conversion (e.g., int → Integer)
- Unboxing: Wrapper to primitive (e.g., Integer → int)
Answer:
- ArrayList: Fast access, slow insert/remove
- LinkedList: Slow access, fast insert/remove at head/tail
Answer:
- HashMap: Not synchronized, allows one null key
- Hashtable: Thread-safe (synchronized), no null keys/values
Answer:
A constructor is a special method to initialize objects. Constructors are not inherited, but a subclass can call the parent constructor using super().
? 9. What is the use of final keyword?
Answer:
- final variable: Constant value
- final method: Cannot be overridden
- final class: Cannot be extended
Answer:
- Overloading: Same method name, different parameters (compile-time polymorphism)
- Overriding: Subclass provides a specific implementation of a parent method (run-time polymorphism)
Answer:
Abstraction hides implementation details. Achieved using:
- Abstract classes
- Interfaces
Answer:
- Abstract class: Can have constructors, fields, method implementations
- Interface: Only method signatures (Java 8+ allows default/static methods)
Answer:
Encapsulation hides internal object state using private fields and public getters/setters. It protects data integrity.
? 14. What is inheritance?
Answer:
Inheritance allows a class (child) to inherit properties/methods from another (parent). Promotes reusability.
? 15. What is polymorphism?
Answer:
Polymorphism allows the same interface to behave differently:
- Compile-time: Method overloading
- Run-time: Method overriding
Answer:
- private: Only within class
- default (no modifier): Package-level access
- protected: Package + subclass access
- public: Accessible everywhere
Answer:
- static: Belongs to class, can’t access non-static members directly
- non-static: Belongs to object, can access static members
Answer:
A static block initializes static data. Executes once when the class is loaded.
? 19. Can you override static methods?
Answer:
No. Static methods belong to the class and not the instance, so they can’t be overridden. They can be hidden, though.
? 20. What is the purpose of this keyword?
Answer:
this refers to the current object instance. Commonly used to avoid naming conflicts and pass current object.
? 21. What is garbage collection in Java?
Answer:
Automatic memory management. Unused objects are cleaned up by the Garbage Collector (GC).
? 22. How do you force garbage collection?
Answer:
You can suggest GC using System.gc(), but the JVM is not guaranteed to perform it immediately.
? 23. What are exceptions in Java?
Answer:
Unexpected events that disrupt normal flow. Java has:
- Checked Exceptions: Must handle (e.g., IOException)
- Unchecked Exceptions: Runtime issues (e.g., NullPointerException)
Answer:
- throw: Used to throw an exception
- throws: Declares that a method may throw an exception
Answer:
Introduced in Java 7. Ensures automatic closing of resources like FileReader, BufferedReader, etc.
try (BufferedReader br = new BufferedReader(new FileReader("file.txt"))) {
// read file
}
? 26. What is multithreading?
Answer:
Multithreading allows concurrent execution of two or more threads. Improves performance for I/O-heavy operations.
? 27. What is the difference between Runnable and Thread?
Answer:
- Runnable: Interface
- Thread: Class that implements Runnable Using Runnable is more flexible (can extend other classes)
Answer:
Synchronization controls access to shared resources to prevent thread interference or memory consistency errors.
? 29. What is a deadlock?
Answer:
Occurs when two or more threads are waiting on each other to release locks. They get stuck indefinitely.
? 30. What is a lambda expression?
Answer:
Introduced in Java 8. It enables writing anonymous methods concisely.
(a, b) -> a + b
? 31. What are functional interfaces?
Answer:
Interfaces with only one abstract method. Example: Runnable, Callable, Comparator, Function
? 32. What is the Stream API?
Answer:
Introduced in Java 8. Used to process collections in a functional style (filter, map, reduce, etc.).
? 33. What is Optional in Java 8?
Answer:
A container that may or may not hold a non-null value. Helps avoid NullPointerException.
Optional<String> name = Optional.ofNullable("John");
? 34. What is the difference between Comparable and Comparator?
Answer:
- Comparable: Natural ordering (compareTo())
- Comparator: Custom ordering (compare())
Answer:
Marks a variable not to be serialized. Used when saving object state to file.
? 36. What is the volatile keyword?
Answer:
Ensures visibility of changes to variables across threads. Prevents caching by threads.
? 37. What is the difference between stack and heap memory?
Answer:
- Stack: Stores method calls and local variables
- Heap: Stores objects and class instances
Answer:
Metadata that provide information to the compiler or runtime (e.g., @Override, @Deprecated, @FunctionalInterface)
? 39. What are design patterns in Java?
Answer:
Reusable solutions to common software problems. Examples:
- Singleton
- Factory
- Builder
- Observer
- Strategy
Answer:
- Sealed classes
- Pattern matching for switch
- Records (data classes)
- Strong encapsulation of internal APIs
? Final Tip: Don’t just memorize answers. Understand the why behind each concept and use hands-on examples to reinforce your knowledge.