core java interview question part 10
Q 61- 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 and that reference variable could be used to
access all of the methods of the class (which are not excluded using public,
private, or protected): True or False? If false, explain why.
A - False. If one or more of the classes P, D, and Q, define
instance methods which are not declared in the interface X, then a variable of
type X cannot be used to access those instance methods. Those methods can only
be accessed using a reference variable of the class in which the method is
defined. Reference variables of the type X can only be used to access methods
declared in the interface X (or one of its superinterfaces).
Q 61 - The new operator must be used to instantiate an
object which is of the type of an interface: True or False? If false, explain why.
A - False. Even though you can consider the interface name
as a type for purposes of storing references to objects, you cannot instantiate
an object of the interface type itself.
Q 63- One of the difficulties of implementing interfaces is
the requirement to coordinate the definition of interface methods among the
classes that implement the interface: True or False? If false, explain why.
A - False. In defining interface methods, each class defines
the methods in a manner that is appropriate to its own class without concern
for how it is defined in other classes.
Q 64- As with classes, multiple interface definitions can be
combined into the same source file: True or False? If false, explain why.
A- False. The compiler requires interface definitions to be
in separate files.
Q 65- List four ways in which interfaces are useful:
A - See the following list:
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
Capturing
similarities between unrelated classes without forcing a class relationship
Declaring methods
that one or more classes are expected to implement
Revealing an
object's programming interface without revealing its class (objects such as
these are called
anonymous objects
and can be useful when shipping a package of classes to other developers)
Q 66- A minimum interface declaration contains the Java
keyword interface, the name of the interface, and the name of the interface
that it extends: True or False? If false, explain why.
A - False. A minimum interface declaration contains the Java
keyword interface and the name of the interface. There is no requirement to
specify the name of the interface that it extends, because it may not extend
another interface.
Q 67 - An interface can extend any number of other
interfaces: True or False? If false, explain why.
A - True.
Q 68- Just like a class definition can extend any number of
other classes, an interface can extend any number of other interfaces: True or
False? If false, explain why.
A - False. A class can extend only one other class.
Q 69- An interface can extend any number of other interfaces
but not more than one class: True or False? If false, explain why.
A - False. An interface cannot extend a class.
Q 70- An interface inherits all constants and methods from
its superinterface: True or False? If false, explain why.
A - False. See reasons below:
An interface inherits all constants and methods from its
superinterface unless:
the interface
hides a constant with another of the same name, or redeclares a method with a new method
declaration.
Q 71- The method declaration in an interface consists of the
method signature followed by a pair of
empty curly braces: True or False? If false, explain why.
A - False. The method declaration is terminated by a
semicolon and no body (no curly braces) is provided for the method.
Q 72- The keyword private is used to restrict access to the
members of an interface only to classes within the same package: True or False?
If false, explain why.
A - False. You may not use private in a member declaration
in an interface.
Q 73- All methods declared in an interface are implicitly
public and abstract: True or False? If false, explain why.
A - True.
Q 74- In addition to declaring methods, the body of the
interface may also define constants. Constant values defined in an interface
are implicitly public, static, and final: True or False? If false, explain why.
A - True.
Q 75- You use an interface by defining a class that extends
the interface by name: True or False? If false, explain why.
A - False. You use an interface by defining a class that
implements (not extends) the interface by name.
Q 76- When a class claims to implement an interface, it must
provide a full definition for all the methods declared in the interface as well
as all of the methods declared in all of the superinterfaces of that interface:
True or False? If false, explain why.
A - True.
Q 77- A class can implement more than one interface by
including several interface names in a comma-separated list of interface names,
and by providing a full definition for all of the methods declared in all of
the interfaces listed as well as all of the superinterfaces of those
interfaces: True or False? If false, explain why.
A - True.
Q 78- Whenever a class implements an interface, it is
allowed to define only those methods declared in the interface: True or False?
If false, explain why.
A - False. Whenever a class implements an interface, the
class must define all of the methods declared in the interface, but is also
free to define other methods as well.
Q 79- The definition of an interface is a definition of a
new reference data type. You can use interface names just about anywhere that
you would use other type names, except that you cannot A - You cannot instantiate objects of the interface type.
Q 80- Explain in your own words the "bottom line"
benefits of the use of an interface.
A - The interface makes it possible for a method in one
class to invoke methods on objects of other classes,
without the requirement to know the true class of those
objects, provided that those objects are all instantiated from classes that
implement one or more specified interfaces. In other words, objects of classes
that implement specified interfaces can be passed into methods of other objects
as the generic type Object, and the methods of the other objects can invoke
methods on the incoming objects by first casting them as the interface type.
0 comments:
Post a Comment