- What is the output of this code?
1 #include <iostream>
2 using namespace std;
3
4 int main()
5 {
6 int i;
7
8 i = 0;
9 while( i < 5 ) {
10 cout << "A";
11 i++;
12 }
13 return 0;
14 }
AAAA
AAAAA
A
A
A
A
A
A
A
A
A
next prev start
- What is the output of this code? (Note the two slight
changes from the previous problem.)
1 #include <iostream>
2 using namespace std;
3
4 int main()
5 {
6 int i;
7
8 i = 1;
9 while( i < 5 ) {
10 cout << "A" << endl;;
11 i++;
12 }
13 return 0;
14 }
AAAA
AAAAA
A
A
A
A
A
A
A
A
A
next prev start
-
There is no syntax error below (it compiles), but it does not
do what it claims. What is the error?
1 #include <iostream>
2
3 using namespace std;
4
5 int main()
6 {
7 int sum;
8 int i;
9 int n;
10
11 cout << "This program will compute the sum 1 + 2 + ... + n" << endl;
12 cout << "\nEnter a value for n: ";
13 cin >> n;
14
15 sum = 0;
16 while( i <= n ) {
17 sum += i;
18 i++;
19 }
20
21 cout << "1 + 2 + ... + " << n << " = " << sum << endl;
22
23 return 0;
24 }
- sum should be initialized to 1.
- i should be initialized to 1.
- sum += i; should be sum = sum + i;
- i++ should be ++i
next prev start
-
The following code also compiles without errors, but contains a
serious error. What is it?
#include <iostream>
using namespace std;
int main()
{
int capacity;
cout << "This program will determine the largest value of n"
<< "\nsuch that 1 + 2 + 3 + ... + n < capacity" << endl;
cout << "\nEnter a value for capacity > 1: ";
cin >> capacity;
if (capacity <= 1) {
cout << "The capacity must be > 1" << endl;
exit(0);
}
int sum = 0;
int n = 1
while( sum + n < capacity ) {
sum += n;
}
cout << "The largest value of n such that 1 + 2 + 3 + ... + n < "
<< capacity << " is " << n - 1<< endl;
return 0;
}
- The loop condition sum + n < capacity should be
sum < capacity
- The initial value of n should be 0.
- The statement in the loop body, sum += n;
should be sum += n++;
- The statement in the loop body, sum += n;
should be sum += ++n;
next prev start
-
What is the output of the code below as written and what would the
output be if lines 10 and 11 were interchanged?
1 #include <iostream>
2
3 using namespace std;
4
5 int main()
6 {
7 int sum = 0;
8 int k = 1;
9 while (k < 4) {
10 sum += k;
11 k++;
12 }
13 cout << sum << endl;
14
15 return 0;
16 }
- 4 and 4
- 6 and 6
- 4 and 7
- 6 and 9
next prev start
-
Which for
loop below is equivalent to this code using a while
loop:
int x;
x = 1;
while( x < 10) {
if (x % 3 == 0) {
cout << x << " is divisible by 3." << endl;
}
x++;
}
-
1 for(int x = 1; x < 10; x++) {
2 if (x % 3 == 0) {
3 cout << x << " is divisible by 3." << endl;
4 }
5 x++;
6 }
-
1 for(int x = 1; x < 10; x++) {
2 x++;
3 if (x % 3 == 0) {
4 cout << x << " is divisible by 3." << endl;
5 }
6 }
-
1 for(int x = 1; x < 10; x++) {
2 if (x % 3 == 0) {
3 cout << x << " is divisible by 3." << endl;
4 }
5 }
-
1 for( int x; x < 10; x++) {
2 if (x % 3 == 0) {
3 cout << x << " is divisible by 3." << endl;
4 }
5 }
next prev start
- Which while loop below is equivalent to this
for loop:
int n;
for(n = 10; n > 0; n--) {
p *= n;
}
-
int n;
n = 10;
while( n > 0 ) {
n--;
p *= n;
}
-
int n = 10;
while( n > 0 ) {
n--;
p *= n;
}
-
int n;
n = 10;
while( n > 0 ) {
p *= n;
n--;
}
-
int n = 10;
while( n > 0 ) {
p *= --n;
}
next prev start
-
What is the output of the following program?
#include <iostream>
int main()
{
for(int i = 0; i < 5; i++) {
cout << "A";
}
return 0;
}
AAAA
AAAAA
A
A
A
A
A
A
A
A
A
next prev start
-
What is the output of this program? (Note the
2 slight changes from the program in the previous problem.)
#include <iostream>
int main()
{
for(int i = 1; i < 5; i++) {
cout << "A" << endl;;
}
return 0;
}
AAAA
AAAAA
A
A
A
A
A
A
A
A
A
next prev start
-
Which of the following will print exactly 10 copies of
X?
-
#include <iostream>
int main()
{
for(int i = 0; i <= 10; i++) {
cout << "X";
}
cout << endl;
return 0;
}
-
#include <iostream>
int main()
{
for(int i = 1; i < 10; i++) {
cout << "X";
}
cout << endl;
return 0;
}
-
#include <iostream>
int main()
{
for(int i = 0; i < 10; i++) {
cout << "X";
}
cout << endl;
return 0;
}
-
#include <iostream>
int main()
{
for(int i = 0; i < 11; i++) {
cout << "X";
}
cout << endl;
return 0;
}
next prev start
- What is output if input is 'Tristram Shandy' (without the
quotes)?
1 #include <iostream>
2 #include <string>
3 using namespace std;
4
5 int main()
6 {
7 string name;
8 cout << "Enter a name (no spaces): ";
9 cin >> name;
10 int n = name.length();
11
12 for(int i = 0; i < n + 2; i++) {
13 cout << "*";
14 }
15
16 cout << endl;
17 cout << "*" << name << "*" << endl;
18
19 for(int i = 0; i < n + 2; i++) {
20 cout << "*";
21 }
22 cout << endl;
23
24
25 return 0;
26 }
-
*****************
*Tristram Shandy*
*****************
-
**********
*Tristram*
**********
-
***************
*Tristram Shandy*
***************
-
********
*Tristram*
********
next prev start
- What does this code do?
int sum = 0;
for(int i = 1; i <= 10; i += 2) {
sum += i;
}
cout << sum << endl;
- Prints the sum of the 1 + 2 + ... + 10
- Prints the sum of the 1 + 2 + ... + 5
- Prints the sum of the 2 + 4 + 6 + ... + 10
- Prints the sum of the 1 + 3 + 5 ... + 9
next prev start
- What is output?
int a = 1;
for( int k = 0; k < 4; k++) {
a *= 2;
}
cout << a << endl;
- 4
- 8
- 16
- 32
next prev start