Questions From Test #2 and correct
answers
Start
About This Quiz
This quiz assesses understanding of C++ programming concepts such as loops, value parameters, and
variable roles. It's designed to prepare students for the CPS 150 final exam, testing their ability to
analyze and predict code behavior.
CPS 150 Final Exam Study Questions From Test #2 - Quiz
Quiz Preview
1/50 Questions
Complete the program fragment using while statement that will display the numbers 1 to 19 ( in steps of
3, i.e. 1, 4, 7, ..., 19) with one number per output line.int number = _____ ;while (number <= 19) { cout
<< number << endl ; number = number + 3 ;} // end while
2.
Complete the program fragment using while statement that will display the numbers 1 to 19 ( in steps of
3, i.e. 1, 4, 7, ..., 19) with one number per output line.int number = 1 ;while (___________) { cout <<
number << endl ; number = number + 3 ;} // end while
Explanation
The while loop condition should be "number
Rate this question:
3.
Complete the program fragment using while statement that will display the numbers 1 to 19 ( in steps of
3, i.e. 1, 4, 7, ..., 19) with one number per output line.int number = 1 ;while (number <= 19) { cout <<
________ << endl ; number = number + 3 ;} // end while
Explanation
The variable "number" is being outputted on each line using the "cout" statement.
,Rate this question:
4.
Complete the program fragment using while statement that will display the numbers 1 to 19 ( in steps of
3, i.e. 1, 4, 7, ..., 19) with one number per output line.int number = 1 ;while (number <= 19) { cout <<
number << _______ ; number = number + 3 ;} // end while
Explanation
The correct answer is "endl" because it is used to insert a new line character after each number is
displayed, ensuring that each number is printed on a separate line.
Rate this question:
5.
Complete the program fragment using while statement that will display the numbers 1 to 19 ( in steps of
3, i.e. 1, 4, 7, ..., 19) with one number per output line.int number = 1 ;while (number <= 19) { cout <<
number << endl ; _________________;} // end while
Explanation
The correct answer is "number = number + 3" or "number = (number + 3)". These statements increment
the value of the variable "number" by 3 in each iteration of the while loop. This ensures that the loop
continues until the value of "number" reaches or exceeds 19.
Rate this question:
6.
Complete the program fragment using a for statement that will display the numbers 1 to 19 ( in steps of
3, i.e. 1, 4, 7, ..., 19) with one number per output line.for (int number = 1 ;____________; number =
number + 3) cout << number << endl ;
Explanation
The missing condition in the for statement should be "number
Rate this question:
7.
Complete the program fragment using a for statement that will display the numbers 1 to 19 ( in steps of
3, i.e. 1, 4, 7, ..., 19) with one number per output line.for (int number = 1 ; number <= 19 ;
_______________) cout << number << endl ;
, Explanation
The correct answer is "number = number + 3" or "number = (number + 3)". These statements increment
the value of the variable "number" by 3 in each iteration of the for loop. This ensures that the program
will display the numbers 1 to 19 in steps of 3, as required by the question.
Rate this question:
8.
Complete the program fragment using a for statement that will display the numbers 1 to 19 ( in steps of
3, i.e. 1, 4, 7, ..., 19) with one number per output line.for (int number = 1 ; number <= 19 ; number =
number + 3) cout <<__________<< endl ;
Explanation
The given correct answer is "number". In the for loop, the variable "number" is being incremented by 3
in each iteration, starting from 1 and ending at 19. So, when we print the value of "number" in each
iteration, it will display the numbers 1, 4, 7, ..., 19.
Rate this question:
9.
Complete the program fragment using a for statement that will display the numbers 1 to 19 ( in steps of
3, i.e. 1, 4, 7, ..., 19) with one number per output line.for (int number = 1 ; number <= 19 ; number =
number + 3) cout << number <<______ ;
Explanation
The "endl" statement is used to insert a new line after each number is displayed. This ensures that each
number is printed on a separate line, as required by the question.
Rate this question:
10.
Complete the following partially written program which is supposed to read 10 integer values using a
loop, and find the sum total of those numbers.void main () { int k, number ; int sum = 0 ; ______;
while (k <= 10) { cout << "Enter a number: " ; cin >> number ; sum = sum + number ; k
= k + 1 ; } // end while cout << "Result = " << sum << endl ;} // end main
Explanation
The correct answer is "k = 1" because the program is using a while loop to iterate 10 times and read 10
integer values. The variable "k" is used as a counter to keep track of the number of iterations. By setting
"k = 1" initially, the loop will run 10 times as long as "k" is less than or equal to 10.