QUIZ

CHAPTER 4

Your Name:


4.1 If we want to set a Boolean variable freePass for children under 15 or for students under 25, which would be correct?

(a) freePass = age < 15 || age < 25 || student;
(b) freePass = age < 25 && student;
(c) freePass = age < 15 || (age <25 && student);
(d) freePass = student || age < 15;

4.2 What is the correct framework for a while loop which is intended to repeat until the user types ‘q’ for Quit?

(a)

bool more = false;
while (!more) {
	... statements
	more = Console.ReadLine() != "q";
}
    

(b)
bool quit = true;
while (!quit) {
	... statements
	quit = Console.ReadLine() != "q";
}
    

(c)
bool quit = true;
while (!quit) {
	... statements
	more = Console.ReadLine() == "q";
}
    

(d)
bool more = true;
while (more) {
	... statements
	more = Console.ReadLine() != "q";
}
    

4.3 What sequence of values does the follow code fragment print out?

int i = 17;
while(i != 1) {
	Console.Write("{0}", i);
	i = 3*i + 1;
	while(i%2 == 0)
		i /= 2;
}
Console.WriteLine();


(a) 17 13 5 1
(b) 17 15 13 11 9 7 5 3 1
(c) 17 13 5
(d) 17 13 5 4 1


4.4 What is important to include with a multiple-exit loop?

(a) a break statement
(b) an if statement afterwards
(c) a continue statement
(d) a return statement


4.5 What do the following statements print?
	string s = "\"hello!\"\n";
	Console.WriteLine("{0}",s.Length);

(a) 9
(b) 8
(c) 6
(d) 12


4.6 If an array is declared as follows:
DateTime[] myExamDays = {new DateTime(2003,11,4),
                         new DateTime(2003,11,7),
                         new DateTime(2003,11,12),
                         new DateTime(2003,11,19)};
then the day of my second exam is given by

(a) myExamDays[2]
(b) myExamDays[1].Day
(c) myExamDays.Day[1]
(d) myExamDays[2].Day


4.7 If a switch statement does not include a default clause, the effect is

(a) a compilation error
(b) that if the value of the selector expression does not match any of the labels, then the case with a label closest to the selector value is chosen
(c) that if the value of the selector expression does not match any of the labels, then execution continues at the statement after the switch
(d) an execution error


4.8 A valid switch statement for setting the days in a month (ignoring leap years) would be:

(a)
int DaysIn(int month) {
	switch (month) {
		case 9: case 4: case 6: case 11: return 30;
			break;
		case 2 : return 28;
			break;
		else return 31;
			break;
	}
	else return 31;
}

(b)
int DaysIn(int month) {
	switch (month) {
		case 9, 4, 6, 11: return 30;
			break;
		case 2 : return 28;
			break;
		default: return 31;
			break;
	}
}

(c)
int DaysIn(int month) {
	switch (month) {
		case 9: case 4: case 6: case 11: return 30;
		case 2 : return 28;
		default: return 31;
	}
}

(d)
int DaysIn(int month) {
	int days;
	switch (month) {
		case 9: case 4: case 6: case 11: days = 30;
			break;
		case 2 : days = 28;
			break;
		default: days = 31;
			break;
	}
}



4.9 In the ComputeGrade2 program (Example 4.6) how best would the two arrays be printed out ? The output required is:
	90.0    A+
	85.0    A


(a)
foreach (double mark in boundary)
	Console.WriteLine(mark+"    "+grade[mark]);

(b)
for (int i=0; i<boundary.Length; i++)
	Console.WriteLine(boundary[i]+"    "+grade[i]);

(c)
foreach (double mark in boundary)
	Console.WriteLine(mark+"    "+grade[boundary[mark]]);

(d)
foreach (double mark in boundary, string symbol in grade)
	Console.WriteLine(mark+"    "+symbol);

4.10 If we had the string s=" by A A Milne " and want to return just the name "A A Milne", which of the following would do it?

(a) s.TrimStart().Substring(s.IndexOf(’A’)).TrimEnd();
(b) s.Substring(s.IndexOf(’A’)).Trim();
(c) s.Substring(s.IndexOf(’A’));
(d) s.TrimStart().Substring(s.IndexOf(’A’),s.Length).TrimEnd();