COM 1100 Foundations of Computer Science

Answers for Midterm Exam -- Thursday, November 2nd

Fall 2000 -- Professor Futrelle
College of Computer Science, Northeastern U., Boston, MA
Contains answers for the red exam. and for the blue exam.

 

The answers below are primarily for the red exam, because the blue exam had very similar questions, which are cross-referenced by number. If there were important differences, answers for both are noted. The complete original exams, as given, are also posted: The red exam and the blue exam. This was a closed-book, no calculators exam lasting 65 minutes (minus the last five minutes due to the false-alarm fire alarm and evacuation! Grades have been adjusted accordingly.)

Question 1 (Blue Question 3)
    Write a while statement that involves the following:

Answer for 1/3:

int r = 10;
int s = 20;
const int NC = 60;

while((r < NC) && (s > NC/4))
   {
   cout << "r is: " << r << " and s is: " << s << endl;
   r = r + 4;  // or r += 4;
   s = s - 1;  // or s -= 1; or just s--;
   }
   
/* Execution of the above (you weren't asked for this) gives:
r is: 10 and s is: 20
r is: 14 and s is: 19
r is: 18 and s is: 18
r is: 22 and s is: 17
r is: 26 and s is: 16
*/

The blue question 3 was very similar, using different variable names and values.

Question 2 (Blue Question 7)
    Create a for loop that does the following:

Answer for 2/7:

int k;

for( k = 8; k > 1 ; k--)   // or less often, k -= 1 or  k = k - 1
   {
   cout << "I'm a for loop." << endl;
   cout << "I'll quit eventually." << endl;
   }

The loop prints 7 pairs of lines, for k = 8 down to k = 2 inclusive, that is, including the 2. There are various ways to get this count exactly right. Clearly 8 down to 1 would be the same as 1 up to 8 which is obviously 8 values. But this case is 1 less. A more rigorous way is to note that a sequence starting at n1 and ending in and including n2, contains |n2 - n1| + 1 elements. In our case that would be |2 - 8| + 1 = 6 + 1 = 7.

The blue question 7 was very similar, using different variable names and values. It also counted down from a larger value. It printed 5 pairs of lines corresponding to 10, 8, 6, 4, and 2 (since its counting variable i had to be > 0).

Question 3 (Blue Question 4)
    What is the value of the following logical expression?

   ((true == false) && ("mouse" >= "cat")) || (7 < 40)
Explain, step-by-step, how you arrived at your answer.

Answer for 3/4:

   (true == false) is false
   ("mouse" >= "cat") is true, since 'm' comes after 'c' just as in the dictionary.
   Combining the above two gives false && true which is read as
   false logical-and true, which is false (both are not true)
   (7 < 40) is obviously true
   Finally, we use the values of the first and second expressions 
   and compute false || true false logical-or true) which is true.
   So the answer is true.
   (If you want to test the expression in a cout statement, you may need to
   enclose the entire thing in parentheses, in which case it prints '1' which
   is "true".)

The blue question, 4, was very similar. Briefly, the question and answer were:

   (('a' > 'm') || ("dog" < "mouse")) && (false != true)

   which reduces to (false logical-or true) logical-and true, 
   which is  true logical-and true,
   which is true.  So the answer is true.

Question 4 (Blue Question 5)
    Write three versions of a function demo.

  1. The function prototype.
  2. The function definition.
  3. A call to the function using the real variable r and the number 16. The integer variable aa should be defined and used to receive the returned value.
Description of the function:

Answer for 4/5:

// Prototype:
   int demo(float, int);
   
// Definition:
   int demo(float realNum, int intNum)
      {
      return (intNum + 3);
      }
      
// Calling the function:
   int aa;
   float r = 31.6;  // you were not required to include this declaration or a value
   aa = demo(r, 16);  // returns 19 and assigns it to aa, irrespective of the value of r
   

The blue question, 5, was very similar, with different names, types and values and a slightly different return specification.

Question 5 (Blue Question 2)
    Describe what each of the following four input statements do, or if any of them do not work, what's wrong with them. Assume k and s are integer variables with initial values 2 and 19, respectively. Assume that the user types in a sequence of one or more pairs of characters, a 4 followed by a blank, as needed, when the cin's require input.

       A: cin >> k;
       B: cin >> 19;
       C: cin >> k >> s;
       D: cin >> k >> k;

Answer for 5/2:

       A: cin >> k; // Assigns 4 to k.
       B: cin >> 19;  // illegal, will not compile, must read into a variable.
       C: cin >> k >> s; // Assigns 4 to k and 4 to s.
       D: cin >> k >> k; // Assigns 4 to k and then assigns 4 to k again.
                         // Result is simply that k == 4.
       // In A, C, and D, the initial values of the variables are irrelevant,
       // since each input operation overwrites whatever value existed previously. 

The blue question, 2, was almost identical, but 6's were input by the user.

Question 6 (Blue Question 8)
    This is a problem about the scope of identifiers. List what the values are at the points in the code marked below as "I" and "II" and explain your reasoning. Note that there may be more than one scope for a single identifier, so more than one value may be involved. If you analyze this problem carefully and methodically, you should not have any problems with this question.

       int m = 7;
       void voidFun(int r)
          {m = r + m; 
          }
       void main ()
          {int m = 10; // point I, what are the values of m?
           voidFun(m); // point II, what are the values of m inside the function voidFun
           		       // and what is the value of r during this call to voidFun?
          }

Answer for 6/8:

   At point I, the global value of m remains 7
   and the local value in main is 10.
   
   At point II, voidFun has been passed the value 10.
   Inside the function voidFun, the first thing that happens is the 
   formal argument r, which acts as a local variable within the function,
   is assigned the value 10 because that was the actual value passed via this argument.
   Then it assigns the global value of m to the sum of r (which is 10)
   and the global value 7, resulting in assigning 17 to the global value of m.
   
   (This was probably the most difficult question on the Midterm.)
   

The blue question, 8, was virtually identical, with only the names changed.

Question 7 (Blue Question 6)
    Just what is the problem with the following if statement? (Hint: I gave examples like this in the lectures on Monday and Tuesday.)

       int mm = 10;
       if ( mm > 7);
          {
           cout << mm << endl;
          }

Answer for 7/6:

This is a trivial question. The logical expression for the if is immediately followed by a semicolon. Thus the body of the if is an empty statement. The cout statement is simply the first regular statement following the entire if statement and is executed regardless of the value of the test expression. The blue question, 6, had the same type of empty body, but for a while loop. This would cause it to run forever, an "infinite loop".

Question 8 (Blue Question 1)
    What does the following bit of code produce? Carefully show all blank spaces where they occur using the  symbol. ( This question is not hard, but it is tricky. )

       int kk = 683;
       cout << " kk is: << kk" << endl << kk << "That's all >> kk" << "all done";
    

Answer for 8/1:

  // The  annotated output is:

kkis:<<kk
683That'sall>>kkalldone

The blue question, 1, was quite similar.

// The cout statements were:

       int i = 37;
       cout << "<< i is:" << i << endl << "That's all << endl;" << "all done";

// and the  annotated output is:

<<iis:37
That'sall<<endl;alldone

BTW: To be sure these answers were correct, I produced the output by executing the statements and replaced each blank by a ''. Also, you might wonder why you'd want to print things such as "<<" and "endl;". It is not unusual in generating explanations about programming or in generating HTML code, to want to be able to display the very structures that are used internally in the coding. That's why it's good to know. Also it helps you been on guard about malformed statements.

EXTRA CREDIT -- Question 9 (Blue Question 9)
    Give a simple example of a multiple-alternative decision structure as a way to deal with nested if statements. (Hint: This form doesn't have to use more and more indententation at each step.)

Answer for 9/9:

These are the if ... else if ... else if .... else structures described in the text on page 205. The example there is:

   if (x > 0)
      numPos = numPos + 1;
   else if (x < 0)
      numNeg = numNeg + 1;
   else // x == 0
      numZero = numZero + 1;

The blue exam question 9 is:
    Explain the purpose of the break statement in the switch statement and give a small example of its use.

Section 4.8 of the text is devoted to the switch statement. One example there, in part, is:

   switch (musicalNote)
   {
      case 'c':
         cout << "do";
         break;
      case 'd':
         cout << "re";
         break;
      case 'e':
         cout << "mi";
         break;
      case 'f':
         cout << "fa";
         break;
   }
         

switch is rather odd, because for example, if you removed all break statements from the example above and set musicalNote = 'd', it would proceed to print out the 'd' case and all following cases, printing "remifa" ("re" then "mi" then "fa", with no spaces). It would do this in spite of the fact that the cases for the others appear to apply to 'e' and 'f'. You just have to get used to this one. It even works this way in Java, which I find a bit surprising. Another way to lay out the switch example above is:

   switch (musicalNote)
   {
      case 'c': cout << "do"; break;
      case 'd': cout << "re"; break;
      case 'e': cout << "mi"; break;
      case 'f': cout << "fa"; break;
   }