Basic Java Interview Questions and Answers - 5

25. How are destructors defined in Java?

In Java, there are no destructors defined in the class as there is no need to do so. Java has its own garbage collection mechanism which does the job automatically by destroying the objects when no longer referenced.

26. Can we have static methods in an Interface?

Static methods can't be overridden in any class while any methods in an interface are by default abstract and are supposed to be implemented in the classes being implementing the interface. So it makes no sense to have static methods in an interface in Java.

Interface Methods are required to be implemented in the classes that implement the interface. But Static methods can't be overridden in any class. So, we can't have static methods in an Interface.

27. How does Java reduce the chances of a program going out of memory?

Java provides memory management through automatic garbage collection mechanism. This helps in reducing the chances of a program going out of memory. But it doesn't ensure that a Java program will not go out of memory. When Java objects are created at a faster pace compared to garbage collection, a program can go out of memory.

28. Can we increase the size of an array after its declaration?

No, Arrays are static, we can't change the size after its declaration. In this case, we should use vector over array.

29. What is the base class of all exception classes?

In Java, Java.lang.Throwable is the super class of all exception classes and all exception classes are derived from this base class.

30. What is the order of call of constructors in inheritance?

When we create a new object of a derived class, the constructor of the super class is invoked first and then the constructor of the derived class is invoked.