Revised: February 4, 2007
By Richard G. Baldwin
File Pfsg00184.htm
Practice Text Index
The practice tests in this series were written specifically for the benefit of my students in COSC 1315, Fundamentals of Programming. They consists of questions, answers, and explanations. The questions are based on the material covered in my series of online lecture notes for the course. Each practice test is keyed to a specific lecture. This practice test is keyed to lecture #184 titled Counter Loops, Nested Loops, and Sentinel Loops.
184-2. True or False. The name nested loop results from the fact that it consists of two or more loop structures nested inside one another.
184-3. True or False. The header for a for loop contains four clauses surrounded by parentheses and separated by colons. The four clauses appear in the following order from left to right:
184-4. True or False. The header for a for loop contains three clauses surrounded by parentheses and separated by colons. The three clauses appear in the following order from left to right:
184-5. True or False. The optional initialization clause in the header of a for loop is executed once and only once before any other element of the for loop is executed.
184-6. True or False. When you omit one of the optional clauses from the header of a for loop, you must also omit the associated semicolon.
184-7. True or False. A for loop is an exit-condition loop.
184-8. True or False. If the expression in the conditional clause of a for loop evaluates to true, the update clause is evaluated, the code in the body of the loop is executed once, and then the test in the conditional clause is performed again.
184-9. True or False. The code in the body of a for loop continues to be executed for as long as the conditional clause evaluates to true. When the conditional clause evaluates to false, the body of the for loop is skipped and control passes out of the loop.
184-10. True or False. The doSomething function shown in Listing 184-10a produces the screen output shown in Listing 184-10b.
Listing 184-10a.
void doSomething(){ int rowLim = 3; int colLim = 5; for(int rCnt = 0;rCnt < rowLim;rCnt = rCnt + 1){ for(int cCnt = 0;cCnt < colLim;cCnt = cCnt + 1){ cout << rCnt * cCnt << " "; }//end inner loop cout << endl; }//end outer loop }//end doSomething function |
0 0 0 0 0 0 0 1 2 3 4 5 0 2 4 6 8 10 0 3 6 9 12 15 |
184-11. True or False. The inner for loop shown in Listing 184-11 will iterate five times during each iteration of the outer for loop in Listing 184-11.
Listing 184-11.
void doSomething(){ int rowLim = 3; int colLim = 5; for(int rCnt = 0;rCnt < rowLim;rCnt = rCnt + 1){ for(int cCnt = 0;cCnt < colLim;cCnt = cCnt + 1){ cout << rCnt * cCnt << " "; }//end inner loop cout << endl; }//end outer loop }//end doSomething function |
184-12. True or False. The doSomething function shown in Listing 184-12a produces the screen output shown in Listing 184-12b.
Listing 184-12a.
void doSomething(){ int rowLim = 3; int colLim = 5; for(int rCnt = 0;rCnt < rowLim;rCnt = rCnt + 1){ for(int cCnt = 0;cCnt < colLim;cCnt = cCnt + 1){ cout << rCnt * cCnt << " "; }//end inner loop cout << endl; }//end outer loop }//end doSomething function |
0 0 0 0 0 0 1 2 3 4 0 2 4 6 8 |
184-13. True or False. For a sentinel loop, the sentinel value must be chosen carefully to ensure that it won't be confused with a valid data value.
184-14. True or False. For the doSomething function shown in Listing 184-14a, and the user input shown in shown in Listing 184-14b, the program output for the average matches the last line of text shown in Listing 184-14b.
Listing 184-14a.
void doSomething(){ int count = 0; double sum = 0; cout << "Enter a grade or -1 to quit." << endl; //Do a priming read double temp = 0; cin >> temp; while(temp != -1.0){ count = count + 1; sum = sum + temp; cout << "Enter a grade or -1 to quit." << endl; cin >> temp; }//end while loop if(count != 0){ cout << "Average = " << sum/count << endl; }//end if }//end doSomething function |
Enter a grade or -1 to quit. 86 Enter a grade or -1 to quit. 93 Enter a grade or -1 to quit. 99 Enter a grade or -1 to quit. -1 Average = 92 |
184-15. True or False. A do-while loop is an exit-condition loop.
184-16. True or False. The general syntax of a do-while loop is shown in Listing 184-16.
Listing 184-16.
do{ //block of code }while(test); |
184-17. True or False. The body of a do-while loop will always execute at least once.
184-18. True or False. For the doSomething function shown in Listing 184-18a, and the user input shown in shown in Listing 184-18b, the program output for the average matches the last line of text shown in Listing 184-18b.
Listing 184-18a.
void doSomething(){ int count = -1; double sum = 0; double temp = 0; do{ count = count + 1; cout << "Enter a grade or -1 to quit." << endl; cin >> temp; }while(temp != -1.0); if(count > 0){ cout << "Average = " << sum/count << endl; }//end if }//end doSomething function |
Enter a grade or -1 to quit. 86 Enter a grade or -1 to quit. 93 Enter a grade or -1 to quit. 99 Enter a grade or -1 to quit. -1 Average = 92.6667 |
Richard has participated in numerous consulting projects involving Java, XML, or a combination of the two. He frequently provides onsite Java and/or XML training at the high-tech companies located in and around Austin, Texas. He is the author of Baldwin's Java Programming Tutorials, which has gained a worldwide following among experienced and aspiring Java programmers. He has also published articles on Java Programming in Java Pro magazine.
Richard holds an MSEE degree from Southern Methodist University and has many years of experience in the application of computer technology to real-world problems.
This program has a logical error. In particular, the statement that is required to accumulate the sum of the grades is missing, so the actual average value reported by the program is 0.
The average value displayed by the program should have a fractional part. The actual value displayed is 92.6667.
The output shown has one to many rows and one to many columns.
The code in the body of the loop is executed before the update clause is evaluated.
A for loop is an entry-condition loop.
You must not omit the semicolon.
The clauses are separated by semicolons, not colons.
There are only three clauses. There is no catch-all clause.
Richard has participated in numerous consulting projects involving Java, XML, or a combination of the two. He frequently provides onsite Java and/or XML training at the high-tech companies located in and around Austin, Texas. He is the author of Baldwin's Java Programming Tutorials, which has gained a worldwide following among experienced and aspiring Java programmers. He has also published articles on Java Programming in Java Pro magazine.
Richard holds an MSEE degree from Southern Methodist University and has many years of experience in the application of computer technology to real-world problems.
-end-