core java interview questions part 9
Q 43 The equals() method is used to determine if two
reference variables point to the same object: True or False? If false, explain
why.
A- False. You can use the equals() method to compare two
objects for equality. You can use the equality operator (==) to determine if
two reference variables point to the same object.
Q 44- The equals() method is used to determine if two
separate objects are of the same type and contain the same data. The method
returns false if the objects are equal and true otherwise. True or False? If
false, explain why.
A - False. The method returns true if the objects are equal
and false otherwise.
Q 45 - The equals() method is defined in the Object class:
True or False? If false, explain why.
A - True.
Q 46- Your classes can override the equals() method to make
an appropriate comparison between two objects of a type that you define: True
or False? If false, explain why.
A - True.
Q 47- You must override the equals() method to determine if two
string objects contain the same data: True or False? If false, explain why.
A - False. The system already knows how to apply the
equals() method to all of the standard classes and objects of which the
compiler has knowledge. For example, you can already use the method to test two
String objects or two array objects for equality.
Q 48- Given an object named obj1, provide a code fragment
that shows how to obtain the name of the class from which obj1 was instantiated
and the name of the superclass of that class.
A - See code fragment below:
System.out.println("Name of class for obj1: " + obj1.getClass().getName());
System.out.println("Name of superclass for obj1: " + obj1.getClass().getSuperclass());
Q 49- Given an object named obj2, provide a code fragment
that shows how to use the newInstance() method to create a new object of the
same type
A - See code fragment below:
obj2 =
obj1.getClass().newInstance();
Q 50- By overriding the getClass() method, you can use that
method to determine the name of the class from which an object was
instantiated: True or False? If false, explain why.
A - False. The getClass() method is a final method and
cannot be overridden.
Q 51- You must use the new operator to instantiate an object
of type Class: True or False? If false,explain why.
A - False. There is no public constructor for the class
Class. Class objects are constructed automatically by the
Java Virtual Machine as classes are loaded and or by calls
to the defineClass method in the class loader.
Q 52- The Class class provides a toString() method which can
be used to convert all objects known to the compiler to some appropriate string
representation. The actual string representation depends on the type of object:
True or False? If false, explain why.
A - False. The Object class (not the Class class) provides a
toString() method which can be used to convert all objects known to the
compiler to some appropriate string representation. The actual string
representation depends on the type of object.
Q 53- You can override the toString() method of the Class
class to cause it to convert objects of your design to strings: True or False?
If false, explain why.
A - False. The toString() method is a method of the Object
class, not the Class class.
Q 54- By default, all classes in Java are either direct or
indirect descendants of the Class class which is at the top of the inheritance
hierarchy: True or False? If false, explain why.
A - False. By default, all classes in Java are either direct
or indirect descendants of the Object class (not the Class class) which is at
the top of the inheritance hierarchy.
=====
Q 55 - To a limited extent, the interface concept allows you
to treat a number of objects, instantiated from different classes, as if they
were all of the same type: True or False? If false, explain why.
A - True.
Q 56- At its simplest level, an interface definition has a
name, and declares one or more methods: True or False? If false, explain why.
A - True.
Q57- In an interface definition, both the method signatures
and the actual implementations (bodies) of the methods are provided: True or
False? If false, explain why.
A - False. Only the method signatures are provided. The
actual implementations (bodies) of the methods are not provided.
Q 58- An interface definition can contain only method
declarations: True or False? If false, explain why.
A - False. In addition to method declarations, an interface
can also declare constants. Nothing else may be included inside the body of an
interface definition.
Q59 - If classes P, D, and Q all implement interface X, a
reference variable for an object of class P, D, or Q could be assigned to a
reference variable of type X: True or False? If false, explain why.
A - True.
Q 60- If classes P, D, and Q all implement interface X, then
all of the methods declared in X must be exactly the same in classes P, D, and
Q: True or False? If false, explain why.
A - False. The interface simply declares the
signatures for methods. Classes that implement the interface are free to
provide a body for those methods which best suits the needs of the class.
0 comments:
Post a Comment