Professional Documents
Culture Documents
Oops Unit II
Oops Unit II
COLLEGE OF ENGINEERING
DEPARTMENT OF INFORMATION TECHNOLOGY
QUESTION BANK –R 2017
CS8392-OBJECT ORIENTED PROGRAMMING
5. Why does not java support destructor and how does the finalize method help in garbage
collection?
Java does not support destructor because it handles deallocation for you automatically. The
technique that accomplishes this is called garbage collection. when no references to an object
exist, that object is assumed to be no longer needed, and the memory occupied by the object can
be reclaimed.
Specific actions that will occur when an object is just about to be reclaimed by the garbage
collector. The Java run time calls that method whenever it is about to recycle an object of that
class. Inside the finalize( ) method, those actions that must be performed before an object is
destroyed is specified. The garbage collector runs periodically, checking for objects that are no
longer referenced by any running state or indirectly through other referenced objects. Right
before an asset is freed, the Java run time calls the finalize( ) method on the object. The
finalize() method has this general form:
protected void finalize( )
{
// finalization code here
}
6. Distinguish between static and dynamic binding.
Static binding Dynamic binding
The binding which can be resolved at When compiler is not able to resolve the
compile time by compiler is known as static call/binding at compile time, such binding is
or early binding. known as Dynamic or late Binding.
Binding of private, static and final methods When the method overriding is actually
always happen at compile time since these happening and the reference of parent type is
methods cannot be overridden. assigned to the object of child class type then
such binding is resolved during runtime.
The binding of overloaded methods is static The binding of overridden methods is
dynamic.
7. How does declaration of a local inner class differ from declaration of an anonymous inner
class?
A local inner class can be declared within a block. This block can be either a method body,
initialization block, for loop or even an if statement.
Anonymous inner classes are defined at the same time they are instantiated with new. They are
not declared as local classes are done rather anonymous inner classes are defined in
the new expression itself, as part of a statement. The syntax of an anonymous class expression is
like the invocation of a constructor, except that there is a class definition contained in a block of
code.
8. Difference between static and inner(non-static nested) classes.
Static nested classes do not directly have access to other members(non-static variables and
methods) of the enclosing class because as it is static, it must access the non-static members
of its enclosing class through an object. That is, it cannot refer to non-static members of its
enclosing class directly. Because of this restriction, static nested classes are seldom used.
Non-static nested classes (inner classes) has access to all members(static and non-static
variables and methods, including private) of its outer class and may refer to them directly in
the same way that other non-static members of the outer class do.
22. What is the difference between the String and StringBuffer classes?
String StringBuffer
String class is immutable. StringBuffer class is mutable.
String is slow and consumes more memory when StringBuffer is fast and consumes less
you concat too many strings because every time it memory when you cancat strings.
creates new instance.
String class overrides the equals() method of Object StringBuffer class doesn't override the
class. So you can compare the contents of two equals() method of Object class.
strings by equals() method.
25. Define Object class and List the methods defined by it.
Object class is a special class, Object, defined by Java. All other classes are subclasses of
Object. Object is a superclass of all other classes. This means that a reference variable of
type Object can refer to an object of any other class.
Object defines the following methods, which means that they are available in every object.
Abstract class can have abstract and non-abstract Interface can have only abstract
methods. methods.
Abstract class doesn't support multiple inheritance. Interface supports multiple
inheritance.
Abstract class can have final, non-final, static and Interface has only static and final
non-static variables. variables.
Abstract class can provide the implementation of Interface can't provide the
interface. implementation of abstract class.
An abstract class can extend another Java class and An interface can extend another Java
implement multiple Java interfaces. interface only.
A Java abstract class can have class members like Members of a Java interface are
private, protected, etc. public by default.