Chapter 13 Interfaces and Inner Classes – absolute java test bank
Download file with the answers
Chapter 13 Interfaces and Inner Classes - absolute java test bank
1 file(s) 78.44 KB
Not a member!
Create a FREE account here to get access and download this file with answers
Chapter 13
Interfaces and Inner Classes
Multiple Choice
1) A Java interface is an example of:
(a) encapsulation
(b) abstraction
(c) polymorphism
(d) all of the above
2) A class that uses an interface must use the keyword:
(a) Extends
(b) Inherits
(c) Super
(d) Implements
3) An interface and all of its method headings are normally declared to be:
(a) public
(b) private
(c) protected
(d) package access
4) In Java, a derived class can have ________ base class(es).
(a) one
(b) two
(c) three
(d) there is no limit
5) A class that implements an interface but only gives definitions for some of the method headings given in the interface is called a/an:
(a) concrete class
(b) abstract class
(c) discrete class
(d) friendly class
6) The method that must be implemented in a class using the Comparable interface is:
(a) public Object compareTo(Object other)
(b) public Boolean compareTo(Object other)
(c) public int compareTo(Object other)
(d) none of the above
7) The Comparable interface is in the ______________ package.
(a) java.util
(b) java.io
(c) java.awt
(d) java.lang
8) The compareTo method should return _____________ if the calling object equals the parameter.
(a) A negative number
(b) Zero
(c) A positive number
(d) Null
9) An interface may contain:
(a) instance variables
(b) primitive variables
(c) constant variables
(d) all of the above
10) Any constant variables defined in an interface must be defined as:
(a) public
(b) private
(c) protected
(d) package access
11) When using the clone method from an inherited base class you should account for a _________ exception.
(a) CloneNotFoundException
(b) CloneEmptyException
(c) CloneNotSupportedException
(d) CloneNotEmptyException
12) A common use of inner classes is :
(a) clean up
(b) set up
(c) helper
(d) all of the above
13) When defining an inner class to be a helper class for an outer class, the inner classes access should be marked as:
(a) Public
(b) Private
(c) Protected
(d) Package access
14) If an inner class contains non static members, then the class name must be modified by the keyword:
(a) Final
(b) Static
(c) Void
(d) Protected
15) Inner classes available outside the scope of their outer class are modified by the keyword:
(a) Public
(b) Private
(c) Protected
(d) Package access
True/False
1) Java interfaces are a way of simulating multiple inheritance.
2) An interface specifies the headings and definitions for methods that must be defined in any class that implements the interface.
3) A class may only implement one interface.
4) An interface is a type.
5) You can not derive an interface from a base interface.
6) The compiler and the run-time system enforces semantics on the Comparable interface.
7) An interface can contain defined constants as well as method headings or instead of method headings.
8) The Serializable interface is like all the other Java interfaces in that it contains defined constants and method headings.
9) An inner class definition is local to the outer class definition.
10) Inner and outer classes do not have access to each other’s private members.
11) Java source code that contains a class with an inner class, when compiled, will produce a separate .class file for the inner class.
Short Answer/Essay
1) What are the obligations of a class that implements a specific interface?
Answer: To implement an interface, a programmer must do two things. First, the phrase implements interface_name must be included at the start of the class definition. To implement more than one interface, the interface names must be separated by commas. The programmer must then implement all the methods listed in the definition of the interface.
2) What are the semantics of the Comparable interface?
Answer: The semantics of the Comparable interface with respect to the compareTo method returns says that if the calling object “comes before” the parameter, then a negative number is returned. If the calling object “equals” the parameter, then zero is returned. Finally, if the calling object “comes after” the parameter, a positive number is returned.
3) What are the rules of total ordering?
Answer: Total ordering says that the following rules must be satisfied:
1. (Irreflexive) For no object o does o come before o.
2. (Trichotomy) For any two objects o1 and o2, one and only one of the following holds true: o1 comes before o2, o1 comes after o2, or o1 equals o2.
3. (Transitivity) If o1 comes before o2 and o2 comes before o3, then o1 comes before o3.
4) Why does Java only allow the inheritance of only one base class?
Answer: Java allows only one base class to be inherited because it eliminates inconsistent definitions of a single method.
5) What are two advantages to using inner classes?
Answer: There are two big advantages to using inner classes. First, because they are defined within a class, they can be used to make the outer class self-contained. The second advantage is that the inner and outer classes’ methods have access to each other’s private methods and private instance variables.
6) What is an anonymous class?
Answer: An anonymous class is a class that does not have a class name. It is embedded within a class definition using the new operator.
Leave a reply