QUIZ

CHAPTER 8

Your Name:


8.1 The declaration below causes a compilation error because:

struct MyType {
	int i;
	double d;
	MyType t;
	string s;
}

(a) a field cannot be declared of the same type as the type it is in
(b) there are no methods or constructors in the type
(c) struct cannot contain a reference to fields of its own type
(d) a struct cannot contain references

8.2 An array type is classified as a

(a) value type
(b) reference type
(c) either value or reference
(d) neither value nor reference, it is in a group of its own

8.3 An array that can be used to keep the number of patients an emergency hospital sees every hour of the day would be declared as:

(a) int[] tally = new int[24];
(b) int[] tally = new int[23];
(c) int[][] tally = new int[24][];
(d) Patients[] tally = new Patients[24];


8.4 We have a list of objects linked by next references, and want to find an object with an x field value of 6. Four suggestions have been made:

// LOOP 1
n = list;
while (n != null && n.x!=6) {
	n = n.next;
}

// LOOP 2
n = list;
do {
	if (n == null) break;
	n = n.next;
} while (n.x != 6)

// LOOP 3
n = list;
while (n != null) {
	if (n.x == 6) break;
	n = n.next;
}

// LOOP 4
for (n = list; n != null; n = n.next) {
	if (n.x == 6) break;
}
Which of the loops will work?

(a) loops 2, 3 and 4
(b) loops 1 and 3
(c) loops 1, 2 and 3
(d) loops 1, 3 and 4

8.5 Only one of the following statements is false. Which one?

(a) Arrays are of a fixed size, but ArrayLists can grow indefinitely
(b) There is a built-in method to sort arrays but not to sort ArrayLists.
(c) Indices for arrays must be integers, but for ArrayLists can be anything.
(d) Arrays can be multi-dimensional but ArrayLists have only one dimension.

8.6 To declare a jagged array with 4 rows and 1 more column than the row index (i.e. a triangle), we can use:

(a)

int a[][];
for (int i=0; i<4; i++)
	int a[][i] = new int[4][i+1];
(b)
int a[][] = new int[4][];
for (int i=0; i<4; i++)
	a[i] = new int[i+1];
(c)
int a[][] = new int[4][Row+1];
(d)
int a[][] = new int[4][];
for (int i=0; i<4; i++)
	a[i] = new int[i];

8.7 If SL is defined as a sorted list with keys of type string, then which loop will print out all the keys?

(a)

foreach (string s in SL)
	Console.WriteLine(s.Key);
(b)
foreach (string s in SL.Keys)
	Console.WriteLine(SL[s]);
(c)
foreach (string s in SL)
	Console.WriteLine(s);
(d)
foreach (string s in SL.Keys)
	Console.WriteLine(s);

8.8 In order to use the Sort methods supplied by built-in collection types, the element type must:

(a) be a simple type
(b) define the Equals method
(c) define the CompareTo method
(d) have reference semantics

8.9 If we have declared SortedList fruits = new SortedList(10), and we have inserted some Fruit type values into the list where each value consists of a fruit name (the key) and the colour of the fruit (the value), a statement to get a value for a lemon out of the list would be:

(a) Fruit lemon = SortedList[lemon];
(b) Fruit lemon = SortedList["yellow"];
(c) Fruit lemon = (Fruit) fruits["lemon"];
(d) Fruit lemon = fruits["lemon"];

8.10 A valid declaration for an enumerated type for some planets would be:

(a) type Planets = enum {Mercury, Mars, Venus, Earth};
(b) enum Planets = {Mercury, Mars, Venus, Earth}
(c) enum Planets = {Mercury, Mars, Venus, Earth};
(d) enum Planets {Mercury, Mars, Venus, Earth}