QUIZ

CHAPTER 9

Your Name:


9.1 For polymorphism, inheritance would be preferred over interfaces when

(a) The types will have fields and methods
(b) There is a vertical relationship between the types
(c) Neither (a) nor (b): the two options are equally applicable
(d) Both (a) and (b)

9.2 The difference between the interfaces IComparable and IComparer is

(a) IComparable defines a method to be called by Sort directly; IComparer defines a method to be passed to Sort as a parameter and called indirectly
(b) The difference is as in (a), but with the two interfaces interchanged
(c) IComparable has a default Compare method, but in IComparer it always has to be defined
(d) nothing - they are two names for the same interface

9.3 In Example 9.1, c1 is
(a) a class
(b) an object
(c) an interface
(d) a method


9.4 In Example 9.2, what would be the output from the following?

	I[] A = new I[10];
	A[0] = new C1(23);
	A[1] = new C2("today");
	Poly(A[0], A[1]);

(a) I am good old C1 and you are happy C2
(b) am good old A[0] aged 23 you are happy A[1] today
(c) I am good old C1 aged 23 you are happy C2 today
(d) none of the above

9.5 If IAnything is an interface, and Type1 and Type2 are structs which implement the interface, which statement about the following assignment sequence is correct?

	IAnything var1;
	Type1 var2 = new Type1();
	var1 = var2; // 1
	var2 = var1; // 2
	var2 = (Type1) var1; // 3

(a) The sequence will always execute correctly
(b) 1 will execute correctly; 2 and 3 might fail at runtime
(c) 1 will fail at runtime because var1 has not been instantiated yet; 2 and 3 will execute correctly
(d) 1 will execute correctly; 2 will not compile; 3 might fail at runtime

9.6 If IAnything is an interface specifying a method Q, then a class C in the same namespace as Q that implements IAnything must apply the following accessibility to its version of Q

(a) internal
(b) private
(c) public
(d) protected internal

9.7 Overloading of methods means that

(a) They have the same names
(b) They have the same names but are different classes in a hierarchy
(c) They have the same names and the same parameter lists
(d) They have the same names and different parameter lists

9.8 If a method is declared as virtual, then any derived class

(a) may provide an alternative (overridden) version of it with exactly the same parameters
(b) must provide an alternative (overridden) version of it with exactly the same parameters
(c) may provide an alternative (overridden) version of it with the same or different parameters
(d) must provide an alternative (overridden) version of it with the same or different parameters

9.9 A program calls the Save and Restore serialization methods described in Section 9.5 as follows:

	DateTime[] holidays = new holidays[20];
	Save(holidays);
	holidays = Restore();
There is a compilation error. Why?

(a) DateTime does not have the attribute [Serializable]
(b) The result of Restore needs to be downcast to DateTime[]
(c) The filename has not been specified
(d) holidays does not have any values in it yet.

9.10 If B which has a constructor with two fields initialized in its constructor and C derives from this class and has one additional field cField of its own, then which of these C constructors would instantiate a C variable and initialize all three fields?

(a)

C(int i, int j, int k) : base(i, j) {
	cField = k;
}
(b)
C(int i, int j, int k) {
	B(i, j);
	cField = k;
}
(c)
C(int i, int j, int k) {
	base(i, j)
	cField = k;
}
(d)
C(int i, int j, int k) {
	super(i, j)
	cField = k;
}