Set - 9

Question 1 :

How can you minimize the need of garbage collection and make the memory use more effective?

Answer :

Use object pooling and weak object references.


Question 2 :

There are two classes: A and B. The class B need to inform a class A when some important event has happened. What Java technique would you use to implement it?

Answer :

If these classes are threads I'd consider notify() or notifyAll(). For regular classes you can use the Observer interface.


Question 3 :

What access level do you need to specify in the class declaration to ensure that only classes from the same directory can access it?

Answer :

You do not need to specify any access level, and Java will use a default package access level.


Question 4 :

What is the difference between an Interface and an Abstract class?

Answer :

An abstract class can have instance methods that implement a default behavior. An Interface can only declare constants and instance methods, but cannot implement default behavior and all methods are implicitly abstract. An interface has all public members and no implementation. An abstract class is a class which may have the usual flavors of class members (private, protected, etc.), but has some abstract methods.


Question 5 :

What is the purpose of garbage collection in Java, and when is it used?

Answer :

The purpose of garbage collection is to identify and discard objects that are no longer needed by a program so that their resources can be reclaimed and reused. A Java object is subject to garbage collection when it becomes unreachable to the program in which it is used.