Chapter 5 Flashcards
- Which the following statements about the base keyword is false?
a. A constructor can use at most one base statement.
b. A constructor cannot use both a base statement and a this statement.
c. The base keyword lets a constructor invoke a different constructor in the same class.
d. If a constructor uses a base statement, its code is executed after the invoked constructor is executed.
c. The base keyword lets a constructor invoke a different constructor in the same class.
- Which the following statements about the this keyword is false?
a. A constructor can use at most one this statement.
b. A constructor can use a this statement and a base statement if the base statement comes first.
c. The this keyword lets a constructor invoke a different constructor in the same class.
d. If a constructor uses a this statement, its code is executed after the invoked constructor is executed.
b. A constructor can use a this statement and a base statement if the base statement comes first.
- Suppose you have defined the House and Boat classes and you want to make a HouseBoat class that inherits from both House and Boat. Which of the following approaches would not work?
a. Make HouseBoat inherit from both House and Boat.
b. Make HouseBoat inherit from House and implement an IBoat interface.
c. Make HouseBoat inherit from Boat and implement an IHouse interface.
d. Make HouseBoat implement both IHouse and IBoat interfaces.
a. Make HouseBoat inherit from both House and Boat.
- Suppose the HouseBoat class implements the IHouse interface implicitly and the IBoat interface explicitly. Which of the following statements is false?
a. The code can use a HouseBoat object to access its IHouse members.
b. The code can use a HouseBoat object to access its IBoat members.
c. The code can treat a HouseBoat object as an IHouse to access its IHouse members.
d. The code can treat a HouseBoat object as an IBoat to access its IBoat members.
b. The code can use a HouseBoat object to access its IBoat members.
- Which of the following is not a good use of interfaces?
a. To simulate multiple inheritance.
b. To allow the code to treat objects that implement the interface polymorphically as if they were of the interface’s “class.”
c. To allow the program to treat objects from unrelated classes in a uniform way.
d. To reuse the code defined by the interface.
d. To reuse the code defined by the interface.
- Suppose you want to make a Recipe class to store cooking recipes and you want to sort the Recipes by the MainIngredient property. In that case, which of the following interfaces would probably be most useful?
a. IDisposable
b. IComparable
c. IComparer
d. ISortable
b. IComparable
- Suppose you want to sort the Recipe class in question 6 by any of the properties MainIngredient, TotalTime, or CostPerPerson. In that case, which of the following interfaces would probably be most useful?
a. IDisposable
b. IComparable
c. IComparer
d. ISortable
c. IComparer
- Which of the following statements is true?
a. A class can inherit from at most one class and implement at most one interface.
b. A class can inherit from any number classes and implement any number of interfaces.
c. A class can inherit from at most one class and implement any number of interfaces.
d. A class can inherit from any number of classes and implement at most one interface.
c. A class can inherit from at most one class and implement any number of interfaces.
- A program can use the IEnumerable and IEnumerator interfaces to do which of the following?
a. Use MoveNext and Reset to move through a list of objects.
b. Use foreach to move through a list of objects.
c. Move through a list of objects by index.
d. Use the yield return statement to make a list of objects for iteration.
a. Use MoveNext and Reset to move through a list of objects.
- Which of the following statements about garbage collection is false?
a. In general, you can’t tell when the GC will perform garbage collection.
b. It is possible for a program to run without ever performing garbage collection.
c. An object’s Dispose method can call GC.SuppressFinalize to prevent the GC from calling the object’s destructor.
d. Before destroying an object, the GC calls its Dispose method.
d. Before destroying an object, the GC calls its Dispose method.
- Which of the following statements about destructors is false?
a. Destructors are called automatically.
b. Destructors cannot assume that other managed objects exist while they are executing.
c. Destructors are inherited.
d. Destructors cannot be overloaded.
c. Destructors are inherited.
- If a class implements IDisposable, its Dispose method should do which of the following?
a. Free managed resources.
b. Free unmanaged resources.
c. Call GC.SuppressFinalize.
d. All of the above.
d. All of the above.
- If a class has managed resources and no unmanaged resources, it should do which of the following?
a. Implement IDisposable and provide a destructor.
b. Implement IDisposable and not provide a destructor.
c. Not implement IDisposable and provide a destructor.
d. Not implement IDisposable and not provide a destructor.
b. Implement IDisposable and not provide a destructor.
- If a class has unmanaged resources and no managed resources, it should do which of the following?
a. Implement IDisposable and provide a destructor.
b. Implement IDisposable and not provide a destructor.
c. Not implement IDisposable and provide a destructor.
d. Not implement IDisposable and not provide a destructor.
a. Implement IDisposable and provide a destructor.
E1. What keyword do you use to make a constructor invoke a base class constructor?
a. base
b. mybase
c. MyBase
d. this
e. parent
f. constructor
a. Implement IDisposable.
E2. What keyword do you use to make a constructor invoke a different constructor in the same class?
a. base
b. mybase
c. MyBase
d. this
e. parent
f. constructor
d. this
E3. Suppose the Person class provides two constructors that do different things and you derive the Student class from Person. Suppose you want to make a Student constructor that does what both of the Person constructors do. Which of the following strategies would give you this result?
a. Use multiple base clauses.
b. Move the tasks performed by the Person constructors into methods and invoke them from the Student constructor.
c. Make a third Person constructor that invokes the other two Person constructors. Then have the Student constructor invoke the new Person constructor.
d. Move the tasks performed by the Person constructors into methods, and make a third Person constructor that invokes those methods. Then have the Student constructor invoke the new Person constructor.
b. Move the tasks performed by the Person constructors into methods and invoke them from the Student constructor.
d. Move the tasks performed by the Person constructors into methods, and make a third Person constructor that invokes those methods. Then have the Student constructor invoke the new Person constructor.
E4. Suppose you have created Boat and Car classes, and you want to make an AquaticCar class. Which of the following approaches would work?
a. Make AquaticCar inherit from both Boat and Car.
b. Make AquaticCar inherit from Boat and implement an ICar interface.
c. Make AquaticCar inherit from Car and implement an IBoat interface.
d. Make AquaticCar implement ICar and IBoat interfaces.
b. Make AquaticCar inherit from Boat and implement an ICar interface.
c. Make AquaticCar inherit from Car and implement an IBoat interface.
d. Make AquaticCar implement ICar and IBoat interfaces.
E5. Suppose you have created a Boat class that contains several hundred lines of code and a Car class that contains only a few dozen lines of code. Now suppose you want to make an AquaticCar class. Rank the following approaches so that the best approach comes first.
a. Make AquaticCar inherit from both Boat and Car.
b. Make AquaticCar inherit from Boat and implement an ICar interface.
c. Make AquaticCar inherit from Car and implement an IBoat interface.
d. Make AquaticCar implement ICar and IBoat interfaces.
b. Make AquaticCar inherit from Boat and implement an ICar interface.
c. Make AquaticCar inherit from Car and implement an IBoat interface.
d. Make AquaticCar implement ICar and IBoat interfaces.
E6. Suppose you’re building a network application. The Node and Link classes represent a network’s nodes and connecting links. Assuming both of those classes use managed resources, which of the following tasks should the classes do to manage those resources?
a. Implement IDisposable.
b. Provide a Dispose method.
c. Provide a destructor.
d. Implement a Finalize method.
a. Implement IDisposable.
b. Provide a Dispose method.
E7. Suppose the Polygon class uses managed resources. Which of the following tasks should the class do to manage its resources?
a. Implement IDisposable.
b. Provide a Dispose method.
c. Provide a destructor.
d. Implement a Finalize method.
a. Implement IDisposable.
b. Provide a Dispose method.
E8. Suppose the PriceData class uses unmanaged resources. Which of the following tasks should the class do to manage its resources?
a. Implement IDisposable.
b. Provide a Dispose method.
c. Provide a destructor.
d. Implement a Finalize method.
a. Implement IDisposable.
b. Provide a Dispose method.
c. Provide a destructor.
E9. After freeing resources, which of the following should the Dispose method do?
a. Call GC.Finalize.
b. Call GC.SuppressFinalize.
c. Set GC.Finalize = false.
d. Call the object’s destructor.
b. Call GC.SuppressFinalize.
E10. Under which of the following circumstances should a program not call an object’s Dispose method?
a. The object was initialized in a using statement.
b. The object has no Dispose method.
c. The object implements IDisposable.
d. The program invoked the object’s destructor.
a. The object was initialized in a using statement.
b. The object has no Dispose method.