core java interview questions part 8
Q 24- In Java, you can instantiate objects in static memory
at compile time, or you can use the new operator to request memory from the
operating system at runtime and use the constructor to instantiate the object
in that memory: True or False? If false, explain why.
A - False. In Java, objects can only be instantiated on the
heap at runtime.
Q25 - Provide a code fragment consisting of a single statement
that illustrates how the constructor is typically used in Java to declare,
instantiate, and initialize an object. Assume a parameterized constructor with
three parameters of type int.
A - MyClass myObject = new MyClass(1,2,3);
Q26 - Provide a code fragment consisting of a single
statement that illustrates how the default constructor is typically used in
Java to declare and instantiate an object.
A - MyClass myObject = new MyClass();
Q 27- What are the three actions performed by the following
statement?
MyClass myObject = new MyClass(1,2,3);
A - This statement performs three actions in one.
The object is
declared by notifying the compiler of the name of the object.
The object is
instantiated by using the new operator to allocate memory space to contain the
new object.
The object is
initialized by making a call to the constructor named MyClass.
Q 28- In Java, if you attempt to instantiate an object and
the Java Virtual Machine cannot allocate the
requisite memory, the system will:
________________________________________.
A - Throw an OutOfMemoryError.
Q 29- The following is a valid method call: True or False.
If false, explain why.
obj.myFunction(new myClassConstructor(1,2,3) );//Java version
A - True.
Q30 - In Java, when a method begins execution, all of the
parameters are created as local automatic
variables: True or False? If false, explain why.
A - True.
Q31 - In the following statement, an object is instantiated
and initialized and passed as a parameter to a
function. What will happen to that object when the function
terminates? obj.myFunction(new
myClassConstructor(1,2,3) );//Java version
A - It will become eligible for garbage collection.
Q 32- In Java, you declare and implement a constructor just
like you would implement any other method in your class, except that:
_______________________________________________
A - you do not specify a return type and must not include a
return statement.
Q33 - The name of the constructor must be the same as the
name of the ___________________.
A - class.
Q 34- Usually in cases of inheritance, you will want the
subclass to cause the constructor for the superclass to execute last to
initialize those instance variables which derive from the superclass:True or
False? If false, explain why.
A - False. You will want the subclass to cause the
constructor for the superclass to execute first.
Q 35- Provide a code fragment that you would include at the
beginning of your constructor for a subclass to cause the constructor for the
superclass to be invoked prior to the execution of the body of the constructor.
A - super(optional parameters);
Q 36- Every object has a finalize method which is inherited
from the class named ________________.
A - object.
Q 37- Before an object is reclaimed by the garbage
collector, the _______________ method for the
object is called.
A - finalize
Q 38- In Java, the destructor is always called when an
object goes out of scope: True or False? If false, explain why.
A - False. Java does not support the concept of a
destructor.
=====
Q 39- The class at the top of the inheritance hierarchy is
the Object class and this class is defined in the package named java.Object:
True or False? If false, explain why.
A - False. The Object class is defined in the package named
java.lang.
Q40 - We say that an object has state and behavior: True or
False? If false, explain why.
A - True.
Q 41- In Java, a method can be defined as an empty method,
normally indicating that it is intended to be overridden: True or False? If false,
explain why.
A - True.
Q 42- Including an empty method in a class definition will
make it impossible to instantiate an object of that class: True or False.
A - False.
Q 43- A subclass can invoke the constructor for the
immediate superclass by causing the last line of of the subclass constructor to
contain the keyword super followed by a parameter list as though calling a
function named super() and the parameter list must match the method signature
of the superclass constructor: True or False? If false, explain why.
A - False. This can only be accomplished by causing the
first line of the constructor to contain the keyword super followed by a
parameter list as though calling a function named super(). The parameter list
must match the method signature of the superclass constructor.
0 comments:
Post a Comment