Question 1. What is the exact output of the program below? Indicate a blank space in the output by writing the symbol. Indicate a blank line in the output by writing blank line.
#include
main()
{
int n = 4, k = 2;
cout << ++n << endl;
cout << n << endl;
cout << n++ << endl;
cout << n << endl;
cout << -n << endl;
cout << n << endl;
cout << --n << endl;
cout << n << endl;
cout << n-- << endl;
cout << n << endl;
cout << n + k << endl;
cout << n << endl;
cout << k << endl;
cout << n << k << endl;
cout << n << endl;
cout << " " << n << endl;
cout << " n" << endl;
cout << "\n" << endl;
cout << n * n << endl;
cout << 'n' << endl;
return 0;
}
Questin 2. What is the output when the following code fragment is executed?
int found = 0, count = 5;
if (!found || --count == 0)
cout << "danger" << endl;
cout << "count = " << count <<
endl;
Question 3. Suppose that the following code fragment is executed.
const int LENGTH = 21;
char message[LENGTH];
cout << "Enter a sentence on the line below." << endl;
cin >> message;
cout << message << endl;
Suppose that in response to the prompt, the interactive user types the following line and presses Enter:
Please go away.
What will the output of the code fragment look like?
Question 4. What is the output of the program below?
#include
main()
{
int n = 3;
while (n >= 0)
{
cout << n * n << endl;
--n;
}
cout << n << endl;
while (n < 4)
cout << ++n << endl;
cout << n << endl;
while (n >= 0)
cout << (n /= 2) << endl;
return 0;
}
Question 5. What is the output when the following code fragment is executed?
int i =5, j =6, k =7, n =3;
cout << i+j*k-k%n << endl;
cout << i / n << endl;
Question 6. What is the output of the program below?
#include
main()
{
int n;
cout << (n = 4) << endl;
cout << (n == 4) << endl;
cout << (n > 3) << endl;
cout << (n < 4) << endl;
cout << (n = 0) << endl;
cout << (n == 0) << endl;
cout << (n > 0) << endl;
cout << (n && 4) << endl;
cout << (n || 4) << endl;
cout << (!n) << endl;
return 0;
}
Question 8. What is the output when the following code fragment is executed?
char ch;
char title[] = "Titanic";
ch = title[1];
title[3] = ch;
cout << title << endl;
cout << ch << endl;
Question 9. Suppose that the following code fragment is executed.
const int LENGTH = 21;
char message[LENGTH];
cout << "Enter a sentence on the
line below." << endl;
cin.getline(message,LENGTH,'\n');
cout << message << endl;
a. Suppose that in response to the prompt, the interactive user types the following line and presses Enter:
Please go away.
What will the output of the code fragment look like?
b. Suppose that in response to the prompt, the interactive user types the following line and presses Enter:
Please stop bothering me.
What will the output of the code fragment look like?
Question 10. Suppose that the following code fragment is executed.
const int LENGTH = 21;
char message[LENGTH];
cout << "Enter a sentence on the line below."
<< endl;
int i = 0;
do
{
cin >> message[i];
++i;
}
while (imessage[i] = '\0'; // NUL char.
cout << message << endl;
a. Suppose that in response to the prompt, the interactive user types the following line and presses Enter:
What will the output of the code fragment look like?
b. Suppose that the statement
cin >> message[i];
is replaced by the statement
cin.get(message[i]);
Now what will the output of the code fragment look like if, in response to the prompt, the interactive user types the following line and
presses Enter?
Please go away.
Question 11. The nested conditional statement shown below has been written by an inexperienced C/C++ programmer.
The behavior of the statement is not correctly represented by the formatting.
if (n < 10)
if (n > 0)
cout << "The number is positive.
<< endl;
else
cout << "The number is _________."
<< endl;
a. What is the output of the statement if the variable n has the value 7 ? If n has the value 15 ?
If n has the value -3 ?
b. Correct the syntax of the statement so that the logic of the corrected statement corresponds to the formatting of the original statement. Also, replace the blank with an appropriate word or phrase.
c. Correct the formatting of the (original) statement so that the new format reflects the logical behavior of the original statement. Also, replace the blank with an appropriate word or phrase.
12. The loop shown below has been written by an inexperienced C/C++ programmer. The behavior of the loop is not correctly represented by the formatting.
int n = 10;
while (n > 0)
n /= 2;
cout << n * n << endl;
a. What is the output of the loop as it is written?
b. Correct the syntax of the loop so that the logic of the corrected loop corresponds to the formatting of the original loop. What is the output of the corrected loop?
c. Correct the formatting of the (original) loop so that the new format reflects the logical behavior of the original loop.
13. Rewrite the following code fragment so that it uses a "do...while..." loop to accomplish the same task.
int n;
cout <<"Enter a non-negative integer: ";
cin >> n;
while (n < 0) {
cout <<"The integer you entered is
negative." << endl;
cout<<"Enter a non-negative integer: ";
cin >> n; }