Quiz Three

  1. Determine the value, true or false, of each of the following logical expressions.The actual value returned will be either the integer 0 for true or the integer 1 for false, but give your answer as one of the words true or false. If an error might prevent the expression from evaluating, explain why it is or is not a problem.

    Assume that the value of the variable cout is 0 and the value of the variable limit is 10. Boolean expressions are evaluated from left to right.

    1. (count == 0) && (limit < 20)

      True.

    2. !( ((count < 10) || (x < y)) && (count >= 0))

      False. It does not matter what the values of x and y are, since (count < 10) is true, so the entire expression is the same as ! (true && true).

    3. (limit < 20) || ((limit/count) > 7)

      True. Since the value of the first subexpression (limit < 20) is true, you know that the entire expression is true without bothering to evaluate the second subexpression. This short-circuit evaluation means the division by zero is never noticed by the computer.

  2. What is the output of the following (when embedded in a complete program)?

    int count = 3;
    while (count-- > 0)
        cout << count << " ";

    2 1 0

  3. What is the output of the following (when embedded in a complete program)?

    int n = 1;
    do
        cout << n << " ";
    while (++n <= 3);

    1 2 3

  4. For each of the following situations, tell which type of loop (while, do-while, or for would work best:

    1. Summing a series, such as 1/2 + 1/3 + 1/4 + 1/5 + ... + 1/10.

      A for loop works best for numerical iterations like this.

    2. Reading in the number of days of sick leave taken by employees in a department.

      A while loop, since the list may be empty.

    3. Testing a function to see how it performs for different values of its arguments

      A do-while loop can be used since at least one test will be performed.

    4. Reading in the list of (zero or more) exam scores for one student

      A while loop, since the list may be empty.

  5. What is a block?

    A compound statement is a list of statements enclosed in a pair of braces. A block is a compound statement that contains variable declarations. The variables declared in a block are local to the block and so the variable names can be used outside of the block for something else (such as being reused as the name for a different variable.)

  6. What is the fundamental rule (according to Savitch) for testing functions? Why is this a good way to test functions?

    The fundamental rule for testing functions is that every function should be tested in a program where every other function has already been fully tested and debugged. This is a good way to test a function because if you follow this rule, then when you find a bug, you will know which function contains the bug.

  7. Give a definition for the following terms:

    1. formal parameter

      The formal parameters for a function are listed in the function prototype and header and are used in the body of the function definition. A formal parameter (of any type) is a kind of blank or place holder that is filled in with something when the function is called.

    2. argument

      An argument is something that is filled in for a formal parameter. When you write down a function call, the arguments are listed in parenthesis after the function name. When the function call is executed, the arguments are "plugged in" for the formal parameters. Changes to the parameters in the function definition do not affect the values of the arguments.

    3. call-by-value

      In the call-by-value method only the value of the argument is used when calling a function. The formal parameter is a local variable that is initialized to the value of the corresponding argument.

    4. call-by-reference

      In the call-by-reference method the argument is a variable which is "referred" to in the function definition by the corresponding parameter. In the call-by-reference mechanism the argument variable is substituted for the formal parameter so any change that is made to the formal parameter is actually made to the argument variable.

  8. Write a void function definition for a function called zero_both that takes two arguments, both of which are variables of type int, and sets the values of both variables to 0. Calling this function should affect the arguments in the calling function.

    void zero_both(int& n1, int& n2)
    {
        n1 = 0;
        n2 = 0;
    }