COSC 1315

Programming Fundamentals

 Practice Text #184

Counter Loops, Nested Loops, and Sentinel Loops

Revised: February 4, 2007
By Richard G. Baldwin

File Pfsg00184.htm
Practice Text Index


Welcome

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.

Questions



184-1.  True or False.  The name counter loop results from the fact that a loop of this type runs an incremental counter and continues looping until the value of the counter reaches a pre-specified limit.

Answer and Explanation

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.

Answer and Explanation

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:

Answer and Explanation

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:

Answer and Explanation

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.

Answer and Explanation

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.

Answer and Explanation

184-7.  True or False.  A for loop is an exit-condition loop.

Answer and Explanation

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.

Answer and Explanation

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.

Answer and Explanation

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
 
Listing 184-10b.
0 0 0 0 0 0
0 1 2 3 4 5
0 2 4 6 8 10
0 3 6 9 12 15

Answer and Explanation

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

Answer and Explanation

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
 
Listing 184-12b.
0 0 0 0 0
0 1 2 3 4
0 2 4 6 8

Answer and Explanation

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.

Answer and Explanation

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
 
Listing 184-14b.
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
 
Answer and Explanation

184-15.  True or False.  A do-while loop is an exit-condition loop.

Answer and Explanation

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);

Answer and Explanation

184-17.  True or False.  The body of a do-while loop will always execute at least once.

Answer and Explanation

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
 
Listing 184-18b.
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

Answer and Explanation



Copyright 2007, Richard G. Baldwin.  Reproduction in whole or in part in any form or medium without express written permission from Richard Baldwin is prohibited.

About the author

Richard Baldwin is a college professor (at Austin Community College in Austin, TX) and private consultant whose primary focus is a combination of Java and XML. In addition to the many platform-independent benefits of Java applications, he believes that a combination of Java and XML will become the primary driving force in the delivery of structured information on the Web.

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.

Baldwin@DickBaldwin.com


Answers and Explanations

Answer 18

False

Back to Question 18

Explanation 18

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.

Answer 17

True

Back to Question 17

Explanation 17

Answer 16

True

Back to Question 16

Explanation 16

Answer 15

True

Back to Question 15

Explanation 15

Answer 14

False

Back to Question 14

Explanation 14

The average value displayed by the program should have a fractional part.  The actual value displayed is 92.6667.

Answer 13

True

Back to Question 13

Explanation 13

Answer 12

True

Back to Question 12

Explanation 12

Answer 11

True

Back to Question 11

Explanation 11

Answer 10

False

Back to Question 10

Explanation 10

The output shown has one to many rows and one to many columns.

Answer 9

True

Back to Question 9

Explanation 9

Answer 8

False

Back to Question 8

Explanation 8

The code in the body of the loop is executed before the update clause is evaluated.

Answer 7

False

Back to Question 7

Explanation 7

A for loop is an entry-condition loop.

Answer 6

False

Back to Question 6

Explanation 6

You must not omit the semicolon.

Answer 5

True

Back to Question 5

Explanation 5

Answer 4

False

Back to Question 4

Explanation 4

The clauses are separated by semicolons, not colons.

Answer 3

False

Back to Question 3

Explanation 3

There are only three clauses.  There is no catch-all clause.

Answer 2

True

Back to Question 2

Explanation 2

Answer 1

True

Back to Question 1

Explanation 1



Copyright 2007, Richard G. Baldwin.  Reproduction in whole or in part in any form or medium without express written permission from Richard Baldwin is prohibited.

About the author

Richard Baldwin is a college professor (at Austin Community College in Austin, TX) and private consultant whose primary focus is a combination of Java and XML. In addition to the many platform-independent benefits of Java applications, he believes that a combination of Java and XML will become the primary driving force in the delivery of structured information on the Web.

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.

Baldwin@DickBaldwin.com

-end-