Quiz Two

  1. Give the declaration for two variables called cout and distance. cout is a whole number and should be initialized to zero. distance is a floating point number and should be initialized to 1.5 (Initialize both as part of the declaration.)

    1. int count = 0;

    2. double distance = 1.5;

  2. What is the output of the following program lines, when embedded in a correct program that declares all variables to be of type char?

        a = 'b';
        b = 'c';
        c = a;
        cout << a << b << c << 'c';
    

    bcbc

  3. Write the equivalent form of the following expressions:

    1. count += 2;

      count = count + 2;

    2. time /= rush_factor;

      time = time / rush_factor;

    3. amount *= cnt1 + cnt2;

      amount = amount * (cnt1 + cnt2)

  4. Is there anything wrong with the following code examples?

    1.     if (apple > grape)
              eat = true;
              grape++;
          else
              grape--;

      You need to enclose the code body following the if inside { and } if you have more than two statements.

    2.     if (apple = grape)
              eat++;
          else if (apple > grape)
              grape++;
          else
              apple++;

      The first if expression will always be true because the assignment operator = is used instead of the equality operator ==.

    3.     if ((apple > grape) || (grape < apple))
              eat++;
          else
              eat--;

      This is simply an odd way of testing whether apple and grape are not equal.

  5. What is the most important difference between a while loop and a do-while loop?

    With a do-while statement the loop body is always executed at least once. With a while statement there can be conditions under which the loop body is not executed at all.

  6. What will the following code do?

        x = 1;
        
        cout << " I will now count to 10 : ";
        
        while (x != 10)
        {
            cout << x;
            x = x + 2;
        }

    This is an infinite loop. The output would begin with the following and go on forever: I will count to 10 : 1 3 5 7 9 11 ...

  7. Write a function prototype and a function definition for a function that takes one argument of type double. The function returns the character value 'P' if its argument is positive and returns 'N' if its argument is zero or negative.

        char positive_test(double number);
        
        char positive_test(double number)
        {
            if (number > 0)
                return 'P';
            else
                return 'N';
        }
    
  8. If you use a variable in a function definition, where should you declare the variable? In the function definition? In the main part of the program? In any place that is convenient?

    If you use a variable in a function definition, you should declare the variable in the body of the function definition.

  9. What does it mean when we say the programmer who uses a function should be able to treat the function like a black box?

    We mean the programmer should not need to look at the body of the function definition to see how the function works. The function prototype and accompanying comment should be all the programmer needs to know in order to use the function.

  10. What is a local variable?

    Variables that are declared within the body of a function definition are said to be local to that function, or to have that function as their scope. Two variables can have the same name if they are declared in two different functions, and they will remain independent of each other.

  11. What does "global" mean in the context of C++?

    A variable or constant declared at the beginning of your program outside the scope of any function (including main). You can use the global value inside any function after its declaration.

  12. Why are global variables evil?

    It is very easy to accidentally change the value of a global variable when you don't mean to. Local variables help you implement the "black box" model of functions, data encapsulation, information hiding, and other good things with odd names.