CS401 - Self-Check Quiz #1

Give yourself about 30 minutes to do these problems.  Try not to look at your notes or the book until you're done.  Solutions will be made available online near the end of the week.
 

Question 1:  True/False

Circle T or F for the following statements:
  1. Syntax errors are detected by the linker.
  2. It is possible that the body of a conditional statement will not be executed.
  3. All C++ operators are left associative.
  4. Assuming x is equal to 8, the following expression has the value of 10:  (x+1)++
  5. Any for loop can be rewritten as a while loop.

Question 2:  Output

Determine the output of the following program:
 
#include <iostream>
#include <string>
using namespace std;

int main() {
  int x=4, y=10;
  
  cout << x << " " << y << endl;

  if (y/2 == 0)
    cout << "neither here" << endl;
  else
    cout << "nor there" << endl;

  while ( (x > 0) && (y > 0) ) {
    x--;
    cout << x << " ";
    y -= x;
    cout << y << endl;
  }
}

Question 3:  Short Answers

  1. What does it mean to "send a message to an object?"
  2. Give two benefits that you get by using const declarations.
  3. What is mixed-mode?  Give an example of an expression that is in mixed mode.
  4. What is the difference between relational operators and logical operators?  Give an example of a single boolean expression using both of these.
  5. What is the biggest difference between while-do  and a do-while constructs?

Question 4:  Write C++ code

  1. Write a variable declaration for a floating point variabled called fVal initialized to 3.505.
  2. Write a const declaration for the number of ounces in a cup (which is 8).
  3. Suppose x is a double.  Write a logical expression that is true if x is either negative, or between 10.0 and 25.5.
  4. Write a complete program that asks the user for a positive integer between 1 and 200, then prints out the sum of all even numbers less than or equal to that number.  For example, if the user types 9, you program would pring out 20 (8+6+4+2).
Last Updated: 6/4/01 by H. Chad Lane, hcl@cs.pitt.edu
© 2000-2001 Jim Skrentny, University of Wisconsin