Some Practice Problems For The C++ Exam and Solutions For The Problems
Some Practice Problems For The C++ Exam and Solutions For The Problems
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 <iostream.h>
main()
{
int n = 4, k = 2;
1
cout << 'n' << endl;
return 0;
}
2
2. What is the output of the program below?
#include <iostream.h>
main()
{
int n = 3;
while (n >= 0)
{
cout << n * n << endl;
--n;
}
while (n < 4)
cout << ++n << endl;
while (n >= 0)
cout << (n /= 2) << endl;
return 0;
}
#include <iostream.h>
main()
{
int n;
return 0;
}
3
4. What is the output of the following program?
#include <iostream.h>
main()
{
enum color_type {red, orange, yellow, green, blue, violet};
shirt = red;
pants = blue;
cout << shirt << " " << pants << endl;
return 0;
}
int i = 5, j = 6, k = 7, n = 3;
cout << i + j * k - k % n << endl;
cout << i / n << endl;
char ch;
char title[] = "Titanic";
ch = title[1];
title[3] = ch;
4
8. Suppose that the following code fragment is executed.
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?
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?
int i = 0;
do
{
cin >> message[i];
++i;
}
while (i < LENGTH - 1 && message[i] != '\n');