CS 302 Quiz 1

CS 302 - Algebraic Language Programming Name: Colby O'Donnell
Section 22
Instructor: Colby O'Donnell Score: 20 / 20 Points
Date: Friday, September 19, 1997 Percentage of Class Grade: 1%

For questions that ask for you to write C++ code be sure to use the elements of good style (indentation, descriptive names, constants, etc.). If you have any questions, ask me!

  1. (____ / 4 points) Fill in the blank

      a) In C++, each statement in the source code ends with a semicolon (;)

      b) What include file is necessary to use to objects cout and cin? iostream.h

      c) Which of these three operators has the highest precedence? +, *, > times

      d) In the last line of the following code, the action that enables the compiler to perform the multiplication is called "Type PROMOTION".

          int i = 3;
          float b = 2.0 * i;
          
  2. (____ / 8 points) Short Answer

    a) Give three examples of compound statements ("smooth operators"):

         ++   --
         +=   -+   *=   /=   %=
         
    b) Why is the following code invalid?
         int a = 5, b = 4, c;
         c = ++(--a * b--);
         
    The ++ operator must be applied to an lvalue, such as a variable. It can't be applied to an arbitrary expression. The expression (--x * y--) does not have a valid storage location in memory.
    c) Explain the difference between int and float.
    int - stores integer type numbers
    float - stores decimal type numbers
    d) Is the following code valid? Why or why not? Hint: Your answer should contain the word 'operator'.
        int a, b = 3, c;
        c = a = b;
        
    Valid. The assignment operator, like all operators, is also results in an expression. The expression (a = b) evaluated to the value assigned, in this case 3. So the statement c = (a = b) is legal, because (a = b) has a value, 3.
  3. (____ / 3 points) Give the values of a, b, and x after the execution of the following code:

      int a = 7, b;
      float x;
      b = ++a - 2;
      x = ++a % b--;
      x /= 2;
    
    a: (9, int)
    b: (5, int)
    x: (1.5, float)
  4. (____ / 5 points) Write a C++ program that inputs a distance in inches and outputs that distance in yards. (1 yard = 36 inches)

    // Converts feet to miles
    #include <iostream.h>
    void main () {
      cout << "Enter number of feet: ";
      int feet;
      cin >> feet;
      cout << "That would be " << feet / 5280.0 << " miles." << endl;
    }