Chapter 5 Flashcards

1
Q
  1. 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.
A

c. The base keyword lets a constructor invoke a different constructor in the same class.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q
  1. 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.
A

b. A constructor can use a this statement and a base statement if the base statement comes first.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q
  1. 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

a. Make HouseBoat inherit from both House and Boat.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q
  1. 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.
A

b. The code can use a HouseBoat object to access its IBoat members.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q
  1. 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.
A

d. To reuse the code defined by the interface.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q
  1. 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
A

b. IComparable

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q
  1. 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
A

c. IComparer

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q
  1. 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.
A

c. A class can inherit from at most one class and implement any number of interfaces.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q
  1. 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

a. Use MoveNext and Reset to move through a list of objects.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q
  1. 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.
A

d. Before destroying an object, the GC calls its Dispose method.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q
  1. 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.
A

c. Destructors are inherited.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q
  1. 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.
A

d. All of the above.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q
  1. 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.
A

b. Implement IDisposable and not provide a destructor.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q
  1. 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

a. Implement IDisposable and provide a destructor.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

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

a. Implement IDisposable.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

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

A

d. this

17
Q

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.

A

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.

18
Q

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.

A

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.

19
Q

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.

A

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.

20
Q

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

a. Implement IDisposable.

b. Provide a Dispose method.

21
Q

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

a. Implement IDisposable.

b. Provide a Dispose method.

22
Q

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

a. Implement IDisposable.
b. Provide a Dispose method.
c. Provide a destructor.

23
Q

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.

A

b. Call GC.SuppressFinalize.

24
Q

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

a. The object was initialized in a using statement.

b. The object has no Dispose method.

25
Q

E11. If you make a Person class and a Student class with the obvious inheritance relationship, then the Person class is which of the following?

a. A derived class
b. A parent class
c. A child class
d. A subclass
e. A superclass

A

b. A parent class

e. A superclass

26
Q

E12. If you make a Person class and a Student class with the obvious inheritance relationship, then the Student class is which of the following?

a. A derived class
b. A parent class
c. A child class
d. A subclass
e. A superclass

A

a. A derived class

c. A child class
d. A subclass

27
Q

E13. If you define a class, how many parent classes can you define for it?

a. 0
b. 1
c. 2
d. Any number

A

a. 0

b. 1

28
Q

E14. If you define a class, how many child classes can you define for it?

a. 0
b. 1
c. 2
d. Any number

A

d. Any number

29
Q

E15. If you define a class, what is the maximum number of interfaces that it can implement?

a. 0
b. 1
c. 2
d. Any number

A

d. Any number

30
Q

E16. If you define an interface, what is the maximum number of classes that can implement it?

a. 0
b. 1
c. 2
d. Any number

A

d. Any number

31
Q

E17. Suppose the IStudent interface defines the method PrintGrades. If the TeachingAssistant class explicitly implements the interface, then which of the following statements is true?

a. TeachingAssistant has a method named PrintGrades.
b. TeachingAssistant has a method named IStudent.PrintGrades.
c. If ta is a TeachingAssistant variable, then the program can call ta.PrintGrades().
d. If ta is a TeachingAssistant variable, then the program can call ta.IStudent.PrintGrades().
e. If ta is an IStudent variable, then the program can call ta.PrintGrades().
f. If ta is an IStudent variable, then the program can call ta.IStudent.PrintGrades().

A

b. TeachingAssistant has a method named IStudent.PrintGrades.
e. If ta is an IStudent variable, then the program can call ta.PrintGrades().

32
Q

E18. Suppose the IStudent interface defines the method PrintGrades. If the TeachingAssistant class implicitly implements the interface, then which of the following statements is true?

a. TeachingAssistant has a method named PrintGrades.
b. TeachingAssistant has a method named IStudent.PrintGrades.
c. If ta is a TeachingAssistant variable, then the program can call ta.PrintGrades().
d. If ta is a TeachingAssistant variable, then the program can call ta.IStudent.PrintGrades().
e. If ta is an IStudent variable, then the program can call ta.PrintGrades().
f. If ta is an IStudent variable, then the program can call ta.IStudent.PrintGrades().

A

a. TeachingAssistant has a method named PrintGrades.
c. If ta is a TeachingAssistant variable, then the program can call ta.PrintGrades().
e. If ta is an IStudent variable, then the program can call ta.PrintGrades().

33
Q

E19. The Array.Sort method can sort an array of objects if their class implements which of the following interfaces?

a. IComparer
b. IComparable
c. ISortable
d. IEquatable

A

b. IComparable

34
Q

E20. A List object’s Contains method returns true if a particular object is in the List but only if the object’s class implements which method?

a. IComparer
b. IComparable
c. ISortable
d. IEquatable

A

d. IEquatable