Name:_______________________________

 

Take-Home Quiz 2 for CSCI 201.002

(each question is worth 2 pts)

 

  1. Explain why the following code fragment is illegal.
  2. 
       const float PayRate = 6.50;
       OldSalary = PayRate * 40;
       PayRate = 7.25;
       NewSalary = PayRate * 40;
    

  3. For each of the following expressions, give the final values of the objects Cost and Price.
  4. 
       int Cost = 5, Price = 10;
    
    1. Cost = Price++;
    2. Cost = ++Price;
    3. Cost += Price++;
    4. Price /= Cost;

  5. Give a single assignment statement that is equivalent to:
  6. 
    j += 1;
    i = j;
    

  7. Does the following code always correctly assign the average of the three floats x, y, and z to average?
  8. 
    float average = (x + y + z / 3.0);
    

  9. For the following if statement, which values of <logical expression> cause the text string "true part" to appear on the monitor.
  10. 
       if (<logical expression>)
       	cout << "true part" << endl;
    
    1. 0
    2. 1
    3. –2
    4. 3

  11. Which of the following are legal object definitions (also called declarations) and initializations:
    1. int my_int(5);
    2. int my_int = 6;
    3. SimpleWindow W("my winder", 4, 5);
    4. RectangleShape R(W, 2.0, 4.0, Blue, 3.0, 4);

  12. Evaluate the expressions below given the following object definitions:
  13. 
       bool P = false, Q = true, R = true;
       int  i = 1, j = 0;
    
    1. P && Q || !P && !Q;
    2. !!P;
    3. 0 == 1 || 0 < j;
    4. j = i;

  14. Assume that i and j are two integer variables that have been previously define, write an expression that is true when i is even and j is odd, or i is odd and j is even.


  15. Consider the following if statement:
  16. 
       if ((i == 3) || (j == 4)) {
           cout << "yes" << endl;
       }
       else {
           cout << "no" << endl;
       }
    
    Are there values that cause the display of both "yes" and "no"? Why?

  17. Write an if–else statement that compares the values of two integer objects i and j, and writes the message "they are equal" to the monitor if i and j hold the same value, otherwise it writes the message "they are not equal" to the monitor.