Java Interview Questions for Experienced: Part - 2
12. If a package contains sub-packages, does importing only the main package suffice?
Yes, importing the main package suffices. It includes all classes and sub-packages within it.
13. If the code System.exit(0) is written at the end of the try block, will the finally block still be executed?
No, if System.exit(0) is called, the program terminates abruptly, bypassing the execution of the finally block.
14. What do you understand by marker interfaces in Java?
Marker interfaces in Java don’t have any methods but act as a tag to convey information to the compiler or runtime about a class. Examples include Serializable and Cloneable.
15. What is meant by "Double Brace Initialization" in Java.
Double brace initialization refers to the technique of initializing collections or objects in Java using double braces, which involves creating an anonymous inner class and initializing the collection within it.
16. Why is it said that the length() method of the String class doesn't return accurate results?
The length() method of the String class returns the number of characters in the string, but for Unicode supplementary characters, it might not accurately represent the number of code units used to store the characters.
17. What are the various methods to make an object eligible for garbage collection (GC) in Java?
Objects become eligible for garbage collection in Java when they no longer have any references pointing to them or when those references go out of scope.
18. In a given Java Program, how many objects qualify for garbage collection?
This would depend on the specifics of the program and its memory management. Without the actual code, it’s challenging to determine the number of objects eligible for garbage collection.
19. What represents the most effective way to perform dependency injection? Explain the rationale behind this choice.
Constructor injection is often considered the most effective way to perform dependency injection as it ensures that the dependencies are provided during object creation, promoting better testability and reducing coupling.
20. How can we set the spring bean scope? And what supported scopes does it have?
Spring bean scope can be set using annotations like @Scope or XML configurations. Supported scopes include singleton, prototype, request, session, global session, etc.
21. What are the distinct categories or types of Java Design patterns?
Java design patterns are categorized into three main groups: creational, structural, and behavioral patterns.
22. What is a Memory Leak? Discuss some common causes of it.
A memory leak occurs when a program unintentionally retains objects in memory that are no longer needed, preventing the garbage collector from reclaiming that memory. Causes include improper object references, caches, and listeners that are not deregistered.