- Explain why the following code fragment is illegal.
const float PayRate = 6.50;
OldSalary = PayRate * 40;
PayRate = 7.25;
NewSalary = PayRate * 40;
- For each of the following expressions, give the final values of the objects Cost and Price.
int Cost = 5, Price = 10;
- Cost = Price++;
- Cost = ++Price;
- Cost += Price++;
- Price /= Cost;
- Give a single assignment statement that is equivalent to:
j += 1;
i = j;
- Does the following code always correctly assign the average of the three floats x, y, and z to average?
float average = (x + y + z / 3.0);
- For the following if statement, which values of <logical expression> cause the text string "true part" to appear on the monitor.
if (<logical expression>)
cout << "true part" << endl;
- 0
- 1
- –2
- 3
- Which of the following are legal object definitions (also called declarations) and initializations:
- int my_int(5);
- int my_int = 6;
- SimpleWindow W("my winder", 4, 5);
- RectangleShape R(W, 2.0, 4.0, Blue, 3.0, 4);
- Evaluate the expressions below given the following object definitions:
bool P = false, Q = true, R = true;
int i = 1, j = 0;
- P && Q || !P && !Q;
- !!P;
- 0 == 1 || 0 < j;
- j = i;
- 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.
- Consider the following if statement:
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?
- 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.