Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

Engineering Problem Solving with C++, 3e Chapter 4 Test Bank

1. Flowcharts and pseudocode can be used to describe an algorithm


A. True
B. False
2. A C++ while loop tests the controlling condition before the first iteration of the loop.
A. True
B. False
3. A C++ do while loop tests the controlling condition before the first iteration of the loop.
A. True
B. False
4. Show the program trace of the following program using the input stream 5 2 -1 10
#include <iostream>
using namespace std;
int main()
{ int A, B, N;
A = 3;
B = 6;
cout << "Enter a sequence of values " ;
do
{ cin >> N;
A = A + N + B;
B ++;
} while (N < 10);
B *= 2;
cout << "(N,A,B) = " << "(" << N << ", " << A << ", " << B << ")" <<endl;
return 0;
}
(N,A,B) = (10, 49, 20)

©2017 Pearson Education, Inc. Hoboken, NJ. All Rights Reserved.


Engineering Problem Solving with C++, 3e Chapter 4 Test Bank

5. Show the output for the following program:


#include<iostream>
using namespace std;
int main()
{ int I, J;
for (I=1; I<4; ++I)
{ for (J=4; J>1; --J)
{ cout << J << ", " << I;
cout << endl;
}
}
return 0;
}
4, 1,
3, 1,
2, 1,
4, 2,
3, 2,
2, 2,
4, 3,
3, 3,
2, 3,
6. Show the output of the following program using the input data: 8 -3 20
#include<iostream>
using namespace std;
int main()
{ int N, R, S;
cout << "Enter a list of integers terminated by 20.";
S=1;
R=0;
do
{
cin >> N;
R++;
S = S + N;
} while (N < 20);
cout << "S = " << S << “ R = “ << R;
return 0;
}
S = 26 R = 3

©2017 Pearson Education, Inc. Hoboken, NJ. All Rights Reserved.


Engineering Problem Solving with C++, 3e Chapter 4 Test Bank

7. Write a short C++ program to read positive integers, until the value -1 is read and print the
sum of the values.
#include <iostream>
using namespace std;
int main()
{
int num, sum;
cout << "Enter a list of integers to sum terminated by -1\n";
cin >> num;
sum = 0;
while (num != -1)
{
sum += num;
cin >> num;
}
cout << "The sum is " << sum << endl;
return 0;
}
8. Which control statement which is best used when you know how many times to repeat the
execution of a group of statements?
A. the do-while statement
B. the for statement
C. the switch statement
D. the while statement

©2017 Pearson Education, Inc. Hoboken, NJ. All Rights Reserved.


Engineering Problem Solving with C++, 3e Chapter 4 Test Bank

9. The control statement which is best used when you need to select from among many integer
choices is the . . .
A. the for statement
B. the while statement
C. the do/while statement
D. the switch statement
10. The control statement which is best used when you want to repeat a group of statements
based on a condition and the statements in the loop body must be executed at least once is the
...
A. the for statement
B. the while statement
C. the do/while statement
D. the switch statement
11. When more than one statement appears in a loop body . . .
A. the loop body must be enclosed in parentheses, ( )
B. the loop body must be enclosed in curly braces, { }
C. the compiler requires that the statements in the loop body be indented.
D. there is nothing required to specify that there is more than one statement to the compiler

Use the following program to answer the next two questions


#include<iostream>
using namespace std;
int main()
{ int I, J;
for (I=2; I>=0; --I)
{ for (J=1; J<4; ++J)
cout << J << ", " << I << ", ";
cout << endl;
}
return 0;
}
12. How many times is the statement cout << J << ", " << I << ", "; executed?
A. 0
B. 2
C. 3
D. 6
E. None of the above are correct.
13. How many times is the statement cout << endl: executed?
A. 0
B. 2
C. 3
D. 6
E. None of the above answers are correct.

©2017 Pearson Education, Inc. Hoboken, NJ. All Rights Reserved.


Engineering Problem Solving with C++, 3e Chapter 4 Test Bank

14. The break statement is used to terminate the execution of a switch statement
A. True
B. False
15. A do-while loop repeats the loop body zero or more times.
A. True
B. False
16. One reason for an infinite loop in a while loop is that the loop body has no statement which
changes the value of a variable in the conditional boolean expression part of the while loop.
A. True
B. False
17. The while statement will execute the loop body if the condition evaluates to false or zero.
A. True
B. False

18. The continue statement causes the program to skip the rest of the loop body and to determine
if the loop body is to be executed again.
A. True
B. False

19. A while loop repeats the loop body zero or more times.
A. True
B. False

20. A for loop repeats the loop body zero or more times.
A. True
B. False

21. When multiple initialization and/or modification statements are required in a for statement,
which operator is used to separate the statements:
A. The comma
B. The semicolon
C. The colon
D. None of these – it is not possible to have more than one initialization statement.

22. The comma operator has the lowest precedence of any C++ operator.
A. True
B. False

23. Sentinel-controlled input loops require a priori knowledge of how many data are in the file.
A. True
B. False

©2017 Pearson Education, Inc. Hoboken, NJ. All Rights Reserved.


Engineering Problem Solving with C++, 3e Chapter 4 Test Bank

24. Counter-controlled input loops require a priori knowledge of how many data are in the file.
A. True
B. False

25. eof-controlled input loops require a priori knowledge of how many data are in the file.
A. True
B. False

©2017 Pearson Education, Inc. Hoboken, NJ. All Rights Reserved.

You might also like