Engineering Problem Solving With C++, 3e Chapter 4 Test Bank
Engineering Problem Solving With C++, 3e Chapter 4 Test Bank
Engineering Problem Solving With C++, 3e Chapter 4 Test Bank
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;
}
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;
}
7. Write a short C++ program to read positive integers, until the value -1 is read and print the
sum of the values.
8. Which control statement 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
9. The control statement which is best used when you need to select from among many integer
choices is the . . .
A. for statement
B. while statement
C. do/while statement
D. 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
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
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