You are on page 1of 9

Gaddis Starting Out With Java 5 From Control Structures to Objects Chapter 11 Inheritance Multiple Choice

1. When an is a relationship exists between objects, it means that the specialized object has a. Some of the characteristics of the general class, but not all, plus additional characteristics b. Some of the characteristics of the general object, but not all c. None of the characteristics of the general object d. All the characteristics of the general object, plus additional characteristics ANS: D 2. Which of the following statements declares Salaried as a subclass of PayType? a. public class Salaried extends PayType b. public class Salaried implements PayType c. public class Salaried derivedFrom(Paytype) d. public class PayType derives Salaried ANS: A 3. If ClassA is derived from ClassB, then a. Public and private members of ClassB are public and private, respectively, in ClassA b. Public members in ClassB are public in ClassA, but private members in ClassB cannot be directly accessed in ClassA c. Neither public or private members in ClassB can be directly accessed in ClassA d. Private members in ClassB are changed to protected members in ClassA ANS: B 4. In UML diagrams, inheritance is shown a. With a line that has an open arrowhead at one end that points to the superclass b. With a line that has an open arrowhead at one end that points to the subclass c. With a line that has a closed arrowhead at one end that points to the superclass d. With a line that has a closed arrowhead at one end that points to the subclass ANS: A 5. It is not possible for a superclass to call a subclasss method. a. True b. False ANS: A 6. If a subclass constructor does not explicitly call a superclass constructor, a. It must include the code necessary to initialize the superclass fields b. The superclass fields will be set to the default values for their data types c. Java will automatically call the superclasss default constructor immediately after the code in the subclasss constructor executes

d. Java will automatically call the superclasss default constructor just before the code in the subclasss constructor executes ANS: D 7. If a method in a subclass has the same signature as a method in the superclass, the subclass method overloads the superclass method. a. True b. False ANS: B 8. A protected member of a class may be directly accessed by a. Methods of the same class b. Methods of a subclass c. Methods in the same package d. All of the above ANS: D 9. When declaring class data members, it is best to declare them as a. Private members b. Public members c. Protected members d. Restricted members ANS: A 10. If you do not provide an access specifier for a class member, the class member is given ____ access by default. a. Private b. Public c. Protected d. Package ANS: D 11. If ClassC is derived from ClassB, which is derived from ClassA, this would be an example of a. Multiple inheritance b. A chain of inheritance c. A family tree d. Packaging ANS: B 12. Every class is either directly or indirectly derived from the Object class. a. True b. False ANS: A

13. If a class contains an abstract method, a. You cannot create an instance of the class b. The method will have only a header, but not a body, and end with a semicolon c. The method must be overridden in subclasses d. All of the above ANS: D 14. An abstract class is not instantiated, but serves as a superclass for other classes. a. True b. False ANS:A 15. Given the following statement which of the following is true? public class ClassB implements ClassA{} a. ClassA must override each method in ClassB b. ClassB must override each method in ClassA c. ClassB is derived from ClassA d. ClassA is derived from ClassB ANS: B 16. All fields declared in an interface a. Are final and static b. Have protected access c. Must be initialized in the class implementing the interface d. Have private access ANS: A 17. When one object is a specialized version of another object, there is a(n) ____ relationship between them. a. has a b. is a c. Direct d. contains a ANS: B 18. A derived class can directly access a. All members of the base class b. Only public and private members of the base class c. Only protected and private members of the base class d. Only public and protected members of the base class ANS: D 19. In the following statement, which is the base class? public class ClassA extends ClassB implements ClassC a. ClassA

b. c. d. ANS: B

ClassB ClassC Cannot tell

20. In UML diagrams, inheritance is shown a. With a line that has an open arrowhead at one end that points to the base class b. With a line that has an open arrowhead at one end that points to the derived class c. With a line that has a closed arrowhead at one end that points to the base class d. With a line that has a closed arrowhead at one end that points to the derived class ANS: A 21. In an inheritance relationship, the derived class constructor always executes before the base class constructor. a. True b. False ANS: B 22. If a base class does not have a default constructor, a. Then a class that is derived from it, must initialize the base class values b. Then a class that is derived from it, must call one of the constructors that the base class does have c. Then a class that is derived from it, does not inherit the data member fields from the base class d. Then a class that is derived from it, must contain the default constructor for the base class ANS: B 23. If two methods in the same class have the same name but different signatures, the second overrides the first. a. True b. False ANS: B 24. Protected members are a. Not quite private b. Not quite public c. Both (a) and (b) d. Neither (a) or (b) ANS: C 25. Protected class members are denoted in a UML diagram with the symbol a. * b. # c. d.

ANS: B 26. The difference between protected access and package access is a. Protected members may be accessed by methods in the same package or in a derived class, even when the derived class is in a different package b. Packaged members may be accessed by methods in the same package or in a derived class, even when the derived class is in a different package c. Protected members cannot be accessed by method in other classes in the same package, but packaged members can be accessed d. There is no difference in accessibility between protected and packaged members ANS: A 27. In a class hierachy a. The more general classes are toward the bottom of the tree and the more specialized are toward the top b. The more general classes are toward the top of the tree and the more specialized are toward the bottom c. The more general classes are toward the left of the tree and the more specialized are toward the right d. The more general classes are toward the right of the tree and the more specialized are toward the left ANS: B 28. Every class has a toString method and an equals method inherited from the Object class. a. True b. False ANS: A 29. If a class contains an abstract method, a. You must create an instance of the class b. The method will have only a header, but not a body, and end with a semicolon c. The method cannot be overridden in derived classes d. All of the above ANS: B 30. All methods in an abstract class must also be declared abstract. a. True b. False ANS: B 31. In an interface all methods have a. Private access b. Protected access c. Public access d. Packaged access

ANS: C 32. Which of the following statements correctly specifies three interfaces: a. public class ClassA implements Interface1, Interface2, Interface3 b. public class ClassA implements [Interface1, Interface2, Interface3] c. public class ClassA implements (Interface1, Interface2, Interface3) d. public class ClassA implements Interface1 Interface2 Interface3 ANS: A 33. When an interface variable references an object, you can use the interface variable to call all the methods in the class implementing the interface. a. True b. False ANS: B

Gaddis Starting Out With Java 5 From Control Structures to Objects Chapter 15 Recursion Multiple Choice
1. A recursive method is a method that a. Uses four-letter words to tell you when you have an error b. Keeps recurring in your program code c. Calls itself d. Is a method that has a loop in it ANS: C 2. Like a loop, a recursive method must have a. A counter b. Some way to control the number of times it repeats itself c. A return statement d. A predetermined number of times it will execute before terminating ANS: B 3. How many times will the following method call itself, if 10 is passed as the argument public static void message(int n) { if (n 0) { System.out.println(Print this line.\n); message(n 1); }

} a. b. c. d.

1 9 10 An infinite number of times

ANS: D 4. The depth of recursion is a. The number of times that a method calls itself b. The value returned from the last recursive call c. The value that will terminate the recursive calls d. There is no such term ANS: A 5. Any problem that can be solved recursively can also be solved iteratively. a. b. True False

ANS: A 6. The actions that the JVM must performed any time a method is called is called a. Stack frame b. Overhead c. Housekeeping d. Method calls ANS: B 7. To solve a program recursively, you need to identify at least one case in which the problem can be solved without recursionthis is known as a. The recursive case b. The terminal case c. The base case d. The final case ANS: C 8. True/False A problem can be solved recursively if it can be broken down into successive smaller problems that are unique within the overall problem. a. b. ANS: B 9. Indirect recursion occurs when a method calls another method that in turn calls the first method. True False

a. b.

True False

ANS: A 10. The Towers of Hanoi is a. A mathematical game b. Often used in computer science textbooks c. Demonstrates the power of recursion d. All of the above ANS: D 11. A ____ method is a method that calls itself. a. Looping b. Circular c. Recursive d. Reoccurring ANS: C 12. Like ____, a recursive method must have some way to control the number of times it repeats. a. A loop b. Any method c. A GUI method d. A rumor ANS: A 13. How many times will the following method call itself, if 10 is passed as the argument public static void message(int n) { if (n 0) { System.out.println(Print this line.\n); message(n 1); } } a. 0 b. 9 c. 10 d. An infinite number of times ANS: A 14. The number of times that a method calls itself is known as the a. Height of recursion b. Depth of recursion c. Width of recursion d. Length of recursion

ANS: B 15. Recursive algorithms are usually less efficient than iterative algorithms. a. b. ANS: A 16. Which of the following does the JVM not have to perform when a method is called? a. Allocate memory for parameters b. Store the address of the program location where to return after the method terminates c. Terminate the calling program d. Allocate memory for local variables ANS: C 17. In the ____ , we must always reduce the problem to a smaller version of the original problem. a. Terminal case b. Final case c. Re-occurring case d. Recursive case ANS: D 18. A problem can be solved recursively if it can be broken down into successive smaller problems that are identical to the overall problem. a. b. ANS: A 19. If Method A calls Method B which in turn calls Method A, it is called indirect recursion. a. b. True False True False True False

ANS: A 20. Which of the following problems cannot be programmed recursively? a. Towers of Hanoi b. Greatest Common Denominator c. Binary search d. All of the above can be programmed recursively ANS: D

You might also like