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.  FALSE (they are detected by the compiler)
  2. It is possible that the body of a conditional statement will not be executed.  TRUE
  3. All C++ operators are left associative. FALSE (assignment is not)
  4. Assuming x is equal to 8, the following expression has the value of 10:  (x+1)++  FALSE (this is a syntax error)
  5. Any for loop can be rewritten as a while loop.  TRUE (although our exam will not cover for loops)

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;
  }
}

OUTPUT:

4 10
nor there
3 7
2 5
1 4
0 4
 

Question 3:  Short Answers

  1. What does it mean to "send a message to an object?"
    • It means you are calling a method that is associated with that object.  All objects have a set of messages that they can either do or respond to.  An example is when you need to know the length of a string:  if s is a string, you can get its size by sending the "size" message:  cout << "your string is " << s.size() << " characters long.";
  2. Give two benefits that you get by using const declarations.
    • It makes your code easier to read.
    • It makes it easier to do "mass" changes (it allows you to change the const definition at the top of your program rather than finding every occurence of the value in the program and changing those individually).
  3. What is mixed-mode?  Give an example of an expression that is in mixed mode.
    • This occurs when an expression contains at least one integer argument (or short, char, etc.) and at least one float (or double) argument.  The compiler must "upgrade" the ints to floating points, and the resulting type will be float (or double, if double was used in the expression).
  4. What is the difference between relational operators and logical operators?  Give an example of a single boolean expression using both of these.
    • Relational operators relate two values (like ints or chars) while logical operators allow you to combine boolean values to get another boolean value.  EXAMPLE:  ((x>0) && (x%3==0))  (this says x is positive and is a multiple of 3).
  5. What is the biggest difference between while-do  and a do-while constructs?
    • A while loop does its test before the loop body whereas a do-while loop has its test after the loop body.  Thus, the body of a while loop may not be executed, but the body of a while-do loop always executes at least once.

Question 4:  Write C++ code

  1. Write a variable declaration for a floating point variabled called fVal initialized to 3.505.
    • float fVal = 3.505;
  2. Write a const declaration for the number of ounces in a cup (which is 8).
    • const int OuncesInCup = 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.
    • ( (x<0) || ( (x>=10.0) && (x<=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 print out 20 (8+6+4+2).
// fill in the headers...
int main() {
   int val, sum=0;

   cout << "Enter a value: ";
   cin >> val;
   
   // make an odd number even
   if (val%2)
     val--;

   // val must be even now, build the sum with a loop
   while (val > 0) {
     sum += val;      // increase the sum
     val -= 2;        // go to the next lower even number
   }

   // print the result
   cout << "the sum is " << sum << endl;
}

You should test this program with a few input values to make sure it works.
Last Updated: 6/9/01 by H. Chad Lane, hcl@cs.pitt.edu
© 2000-2001 Jim Skrentny, University of Wisconsin