Key for Midterm Test Form B Q A Reason ----------------------------------------------------------------------------------- For questions 1 and 2 you have the input stream: 47A321 and are executing the code; iFile >> ch1 // extracts '4' into ch1 >> i1 // extracts 7 into i1 >> ch2 // skips over the tab; extracts 'A' into ch2 >> ch3; // extracts '3' into ch3 1 3 2 5 For questions 3 through 5 we have the following int variables: int a = 5, b = 0, c = 3; 3 1 2 + b < c is true since 2 + b == 2 and c == 3 4 2 a + b < c is false since a + b == 5 and c == 3. a + b == c is also false. Therefore, the OR of the two expressions is false since both parts of it are false. 5 1 a != 0 is true since a == 5. That's actually all you need to read the conclusion, since the expression groups (by the precedence rules) as: (a != 0) || ( (a <= b) && (c < b) ) -------- ----------------------- Since it's just a big OR, and the first part is true, the OR is true. 6 1 19.0 / 5 == 19.0 / 5.0 // mixed int and double arithmetic == 3.8 The assignment to an int variable truncates this result to 3. 7 2 5 / 2 == 2 // integer division The assignment to a double variable converts this result to 2.0. For questions 8 through 11, the following code is executed on the given input file. You just have to trace the value of the variables as the statements are executed. Input file: 55 23 72 40 Gomer 17 30 95 28 Goober 6 34 82 66 Opie 19 62 36 21 Floyd 8 49 45 33 Bea Code: In >> Zero // extracts 55 into Zero >> One // extracts 23 into One >> Two // extracts 72 into Two >> Two; // extracts 40 into Two, replacing 72 In.ignore(100, '\n'); // skips to beginning of second input line In >> Three // extracts 17 into Three >> Four // extracts 30 into Four >> Zero; // extracts 95 into Zero, replacing 55 In.ignore(100, '\n'); // skips to beginning of third input line In.ignore(100, '\n'); // skips to beginning of fourth input line In.ignore(100, '\t'); // skips to (just before) '6' in fourth line In >> First; // extracts "62" into First In.ignore(100, '\n'); // skips to beginning of fifth input line In >> Second; // extracts "8" into Second (skips leading space) 8 4 9 3 10 3 11 3 12 1 7 / 5 + 4 / 5 == 1 + 0 // integer division == 1 13 4 16 / 4 * 2 + 1 == ((16 / 4) * 2) + 1 // precedence rules == (4 * 2) + 1 == 9 14 2 1.0 + 7 / 2 == 1.0 + 3 // integer division == 1.0 + 3.0 // mixed int and double arithmetic == 4.0 15 1 Straight from the notes. Polya's Four-step Process consists of the steps: 1. Understand the problem 2. Devise a plan 3. Implement the plan 4. Test the plan For questions 16 through 20, you have the input stream: 3.14 2.76 828 0.43 0 27 and are executing the code: In >> anInt1 // extracts 3 into anInt1 >> aFloat1; // extracts .14 into aFloat1 cout << anInt1 << endl; // A cout << setprecision(3) << aFloat1 << endl; // B In >> aFloat2 // extracts 2.76 into aFloat2 >> aFloat3; // extracts 828, converted to a float, into aFloat3 cout << setprecision(1) << aFloat2 << endl; // C cout << setprecision(3) << aFloat3 << endl; // D 16 1 17 3 From above, aFloat1 == .14. The precision setting causes a trailing zero to be printed. (A leading zero is printed automatically.) 18 4 From above, aFloat2 == 2.76. The precision setting causes the value to be rounded to 2.8 for printing (although the stored value for aFloat2 isn't changed.) 19 5 From above, aFloat3 == 828.0. The precision setting causes three trailing zeros to be printed. For questions 20 and 21, you have to consider the specification of the input line: 20 3 The first team name is guaranteed to end with a tab. 1 won't work because the team name may very well contain spaces. 2 won't work because it would read the entire line. 21 1 The score is an integer, so you can't read it (as a number) using getline. For questions 22 and 23, you have to consider the specific input line: <61><7> 22 9 In >> Team1; // extracts "Florida" into Team1 In >> Score1; // attempts to extract an int, and fails 23 2 getline(In, Team1, '\t'); // reads "Florida State" into Team1 In >> Score1; // extracts 61 into Score1 getline(In, Team2, '\t'); // reads "\tVirginia" into Team2 You have to be careful here... getline() doesn't ignore leading whitespace the way >> does, so the tab that follows the first score causes the second getline() to stop after removing that tab. For questions 24 and 25, consider executing the code: int Enter = 10; cin >> Enter; switch (Enter) { case 1: Enter = -4; break; case 2: Enter = -6; case 4: break; case 6: Enter = -8; break; default: Enter = -1; } 24 6 The user sets Enter == 4. The switch jumps to "case 4", but the code there is just a break, which exits the switch without changing Enter. 25 1 The user sets Enter == 3. The switch jumps to the default case, which sets Enter == -1. For questions 26 through 28, we have the variable declarations: string FName = "Foghorn", LName = "Leghorn"; int Age = 74; 26 3 The "+" operator does concatenation on strings. Given the values above, FName + LName == "FoghornLeghorn" 27 2 FName.length() gives the number of characters in the string FName, which is 7. On the other hand, if you are sharp-eyed, this is Fname.length() which is actually not going to compile since Fname is an undeclared identifier. So, I'll also count #4 correct. 28 2 This is a rehash of formatting code we discussed in class for P1 and P2. 29 7 The key here is that the else matches up with the second if. Properly grouped, the code is equivalent to: if (Score >= 95) { if (Rand <= 5) cout << "Nice job!" << endl; else cout << "Good job!" << endl } Since Score == 87, the if-condition is false. There is no matching else clause, so the code doesn't print anything at all. 30 5 Trace the execution of the code: int W = 5, X = 9, Y = 5, Z = 1; if (X + Y >= 2 * W) { // if-condition is true, so go to if-clause Z++; // increment Z to 2 if (Y - 3*W >= -X) // if-condition is false, so go to else-clause Z--; else Z++; // increment Z again, to 3 } else { Z = -1; } 31 7 Trace the execution: int Delta = 0, X = 4; if ( X % 2 == 1 ) // X % 2 == 4 % 2 == 0 Delta = Delta + X; // not executed X++; // increment X to 5 if ( X % 2 == 0 ) // X % 2 == 5 % 2 == 1 Delta = Delta + X; // not executed X++; // increment X to 6 if ( X % 2 == 0 ) // X % 2 == 6 % 2 == 0 Delta = Delta + X; // executed, sets Delta to 6 32 3 The first value printed by the loop must be 2. Since the loop counter isn't updated until the end of the loop body, that means that we must initialize N to 2. 33 2 From the output, N must be increased by 2 on each pass. For questions 34 and 35, consider the program: int main() { const int LIMIT = 10; int howMany; int Output = 5; while ( howMany < LIMIT ) { cout << Output << endl; howMany = howMany + 1; } return 0; } 34 2 The loop control variable is the variable whose value determines whether the body of the loop is executed. 35 1 The variable howMany is never assigned a value before the loop is reached. 36 7 The loop must run until N == 21, and then terminate. So we want the loop to exit after the pass on which N is set to 21. At the beginning of that pass, N == 18, so the exit test can be either N <= 18 or N < 21. N == 21 would cause an extra pass through the loop, printing 24. 37 4 Straight from class discussion. For questions 38 through 40, consider the program: int main() { const int stopValue = -1; // Line 2 int nextValue = 0; // Line 3 ifstream In("Data.txt"); // Line 4 In >> nextValue; // Line 5 while ( nextValue != stopValue ) { // Line 6 cout << nextValue << ' '; // Line 7 In >> nextValue; // Line 8 } In.close(); // Line 9 return 0; } 38 2 We have the input: 3 10 7 25 -1 31 -1 Line 5 will extract the value 3 into nextValue. We'll enter the loop and print 3, then extract 10. We'll re-enter the loop and print 10, then extract 7. We'll re-enter the loop and print 7, then extract 25. We'll re-enter the loop and print 25, then extract -1. That will terminate the loop since now nextValue == stopValue. 39 5 We have the input: 3 10 7 25 31 Line 5 will extract the value 3 into nextValue. We'll enter the loop and print 3, then extract 10. We'll re-enter the loop and print 10, then extract 7. We'll re-enter the loop and print 7, then extract 25. We'll re-enter the loop and print 25, then extract 31. We'll re-enter the loop and print 31, then have an input failure. The input failure will leave nextValue unchanged. The loop will never terminate since now nextValue will remain 31. So we'll now print 31 over and over and over... 40 1 Trace the execution of the modified code with input: 3 10 7 25 -1 31 -1 int main() { const int stopValue = -1; // Line 2 int nextValue = 0; // Line 3 ifstream In("Data.txt"); // Line 4 while ( nextValue != stopValue ) { // Line 6 In >> nextValue; // Line 8 cout << nextValue << ' '; // Line 7 } In.close(); // Line 9 return 0; } We'll enter the loop when we reach it since nextValue == 0. On the first pass, 3 is extracted and printed. On the second pass, 10 is extracted and printed. On the third pass, 7 is extracted and printed. On the fourth pass, 25 is extracted and printed. On the fifth pass, -1 is extracted and printed. The loop then terminates.