Advanced Java Interview Questions & Answers | LinkedIn
1 What is the difference between wait() and sleep() methods in Java?
Answer: wait() is a method for threads to wait for a condition to be met and
releases the monitor, while sleep() simply pauses a thread for a specified time.
2. Explain the concept of garbage collection in Java?
Answer: Garbage collection is the process of automatically reclaiming memory
occupied by objects no longer in use, helping manage memory efficiently.
3. What is a Java annotation, and how are they used?
Answer: Annotations provide metadata about code elements and are used for
various purposes like code documentation and runtime processing.
4. What is the try-with-resources statement in Java, and how does it work?
Answer: try-with-resources is used for automatic resource management,
ensuring that resources like files or sockets are properly closed after use.
5. What is the Java Native Interface (JNI), and when is it used?
Answer: JNI allows Java code to interact with native libraries and is used when
you need to access platform-specific or low-level features.
6. Explain the concept of multithreading in Java?
Answer: Multithreading allows concurrent execution of multiple threads, enabling efficient utilization of CPU resources and responsiveness in applications.
7. What is the purpose of the assert statement in Java?
Answer: assert is used for debugging purposes to check certain conditions during development and can be enabled or disabled at runtime.
8. How does the Java Virtual Machine (JVM) handle method overloading & overriding?
Answer: Method overloading is resolved at compile-time based on the method signature, while method overriding is resolved at runtime using dynamic dispatch.
9. What is the difference between StringBuilder and StringBuffer?
Answer: Both classes provide mutable strings, but StringBuffer is thread-safe, while StringBuilder is not.
10. Explain the concept of design patterns in Java?
Answer: Design patterns are recurring solutions to common design problems, promoting code reusability, maintainability, and scalability.
11. What is reflection in Java, and how is it used?
Answer: Reflection allows Java code to inspect and manipulate class objects, fields, methods, and constructors at runtime.
12. Describe the principles of object-oriented programming (OOP) and their application in Java.
Answer: OOP principles include encapsulation, inheritance, polymorphism, and abstraction, which are fundamental concepts in Java for building modular and extensible code.
13. What is the purpose of the finalize() method in Java, and when is it called?
Answer: The finalize() method is used for cleanup operations on an object before it's garbage collected. It's called by the garbage collector before reclaiming the memory.
14. Explain the concept of checked and unchecked exceptions in Java?
Answer: Checked exceptions are those that must be either caught using try-catch or declared using throws in a method's signature. Unchecked exceptions are subclasses of RuntimeException and don't require explicit handling.
15. What is the difference between deep copy and shallow copy of objects in Java?
Answer: A deep copy creates a new object and recursively copies all objects
referenced by the original object, while a shallow copy creates a new object and
copies references to the objects referenced by the original.
16. How does Java support multiple inheritance through interfaces?
Answer: Java supports multiple inheritance of types (interfaces) but not implementation.
A class can implement multiple interfaces to inherit their abstract methods.
17. What is the purpose of the ClassLoader in Java, and how does it work?
Answer: The ClassLoader is responsible for loading classes into memory at runtime.
It follows a hierarchical structure and can load classes from various sources like the file system or network.
18. What are lambda expressions in Java, and how are they used?
Answer: Lambda expressions provide a concise way to define anonymous functions
and are used mainly for functional programming and the Stream API.
19. What is the difference between composition and inheritance in Java?
Answer:Composition is a design principle where one class contains an instance of another class,
while inheritance is an "is-a" relationship between classes.
20. What is the Java Collections Framework, and why is it important?
Answer: The Collections Framework provides classes and interfaces for working with
collections of objects, offering reusable data structures and algorithms.
Comments
Post a Comment