COSC 1315

Programming Fundamentals

 Practice Text #170

Sequence, Selection, and Loop Structures

Revised: February 3, 2007
By Richard G. Baldwin

File Pfsg00170.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 #170 titled Sequence, Selection, and Loop Structures.

Questions



170-1.  True or False.  The concept of structured programming says that any programming logic problem can be solved using an appropriate combination of only four programming structures, none of which are complicated.  The four structures are known generally as:

Answer and Explanation

170-2.  True or False.  In structured programming, there cannot be multiple entry points or multiple exit points.  There must be only one way into the section of code and one way out of the section of code.

Answer and Explanation

170-3.  True or False.  In structured programming, structures may be nested inside of other structures provided that every structure meets the basic rules for a structure.  Therefore, by nesting simple structures inside of simple structures, large and complex overall structures can be constructed.

Answer and Explanation

170-4.  True or False.  The pseudocode shown in Listing 170-4 describes the sequence structure.

Listing 170-4.
Enter
  Perform one or more actions in sequence
  GoTo some other location in the program
Exit

Answer and Explanation

170-5.  True or False.  The general requirement for the sequence structure is that one or more actions may be performed in sequence after entry and before exit.

Answer and Explanation

170-6.  True or False.  In a sequence structure, one or more of the action elements may themselves be sequence, selection, or loop structures.  If each of the structures that make up the sequence has only one entry point and one exit point, each such structure can be viewed as a single action element in a sequence of actions.

Answer and Explanation

170-7.  True or False.  The pseudocode shown in Listing 170-7 describes a selection structure.

Listing 170-7.
Enter
  Test a condition for true or false
  On true 
    Take one or more actions in sequence
  On false
    Take none, one, or more actions in sequence
Exit

Answer and Explanation

170-8.  True or False.  As a special exception to the rule, there may be multiple entry and exit points in a selection structure.

Answer and Explanation

170-9.  True or False.  The first thing that happens following entry into a selection structure is that some condition is tested for greater than, less than, or equal to, with the subsequent path being one of three alternatives determined by the result of the test.

Answer and Explanation

170-10.  True or False.  In a selection structure, if the tested condition is true, one or more actions are taken in sequence and control exits the structure.  If the tested condition is false, none, one or more actions are taken in sequence and control exits the structure.

Answer and Explanation

170-11.  True or False.  The loop or iteration structure can be described as shown by the pseudocode in Listing 170-11.

Listing 170-11.
Enter
  Test a condition for true or false
  Exit on false
  On true
    Perform one or more actions in sequence
    Go back and test the condition again

Answer and Explanation

170-12.  True or False.  In an entry-condition loop structure, the first thing that happens following entry is that a condition is tested for true or false.  If the test returns false, control simply exits the structure without taking any action at all.

Answer and Explanation

170-13.  True or False.  In an entry-condition loop structure, if the initial test returns true:

During each iteration, if the test returns true, control exits the structure.  If the test returns false, the entire cycle is repeated.

Answer and Explanation

170-14.  True or False.  Generally speaking, unless something is done in one of the actions in a loop structure to cause the test to eventually return false, control will never exit the loop.  In this case, the program will be caught in what is commonly referred to as an infinite loop.

Answer and Explanation

170-15.  True or False.  The code shown in Listing 170-15 is an example of the use of a selection structure.

Listing 170-15.
  void doSomething(){
    cout << "Hello Viet Nam\n";
    cout << "Hello Iraq\n";
    cout << "Hello America\n";
    cout << "Hello World\n";
  }//end doSomething function

Answer and Explanation

170-16.  The function shown in Listing 170-16 illustrates which one of the following structures?

Listing 170-16.
  void doSomething(){
    cout << "Hello Viet Nam\n";
    cout << "Hello Iraq\n";
    doSelection();
    cout << "Hello America\n";
    cout << "Hello World\n";
  }//end doSomething function

Answer and Explanation

170-17.  True or False.  The doSelection function shown in Listing 170-17 executes a selection structure followed by a sequence structure.

Listing 170-17.
  void doSelection(){
    int temp = 0;
    cout << "Enter an integer: ";
    cin >> temp;
    if(temp % 2 == 0){
      cout << "Your number was even" << endl;
    }else{
      cout << "Your number was odd" << endl;
    }//end else
  }//end doSelection

Answer and Explanation

170-18.  True or False.  The expression shown in Listing 170-18 will return true if A is equal to B, and will return false otherwise.

Listing 170-18.
A == B

Answer and Explanation

170-19.  True or False.  The expression shown in Listing 170-19 will return true if temp is even, and will return false if temp is odd.

Listing 170-19.
temp % 2 == 0

Answer and Explanation

170-20.  True or False.  The function named doLoop shown in Listing 170-20 consists of a sequence structure followed by a loop structure.

Listing 170-20.
  void doLoop(){
    bool quit = false;
    int temp = 0;
    while(quit != true){
      cout << "Enter an even integer to quit\n" 
           << "or an odd integer to loop: ";
      cin >> temp;
      quit = doSelection(temp);
    }
    cout << "I'm outta here!" << endl;
  }//end doLoop

Answer and Explanation

170-21.  True or False.  The function named doLoop shown in Listing 170-21 declares and initializes two instance variables named quit and temp.

Listing 170-21.
  void doLoop(){
    bool quit = false;
    int temp = 0;
    while(quit != true){
      cout << "Enter an even integer to quit\n" 
           << "or an odd integer to loop: ";
      cin >> temp;
      quit = doSelection(temp);
    }
    cout << "I'm outta here!" << endl;
  }//end doLoop

Answer and Explanation

170-22.  True or False.  In the function named doLoop shown in Listing 170-22, the local variable named temp is a loop control variable.

Listing 170-22.
  void doLoop(){
    bool quit = false;
    int temp = 0;
    while(quit != true){
      cout << "Enter an even integer to quit\n" 
           << "or an odd integer to loop: ";
      cin >> temp;
      quit = doSelection(temp);
    }
    cout << "I'm outta here!" << endl;
  }//end doLoop

Answer and Explanation

170-23.  True or False.  In the function named doLoop shown in Listing 170-23 the loop control variable named quit is initialized to false, and the function continues to loop for as long as the variable named quit continues to have a value of false.

Listing 170-23.
  void doLoop(){
    bool quit = false;
    int temp = 0;
    while(quit != true){
      cout << "Enter an even integer to quit\n" 
           << "or an odd integer to loop: ";
      cin >> temp;
      quit = doSelection(temp);
    }
    cout << "I'm outta here!" << endl;
  }//end doLoop

Answer and Explanation

170-24.  True or False.  In the function named doLoop shown in Listing 170-24, the value of the loop control variable named quit is controlled by the return value from a function named doSelection.

Listing 170-24.
  void doLoop(){
    bool quit = false;
    int temp = 0;
    while(quit != true){
      cout << "Enter an even integer to quit\n" 
           << "or an odd integer to loop: ";
      cin >> temp;
      quit = doSelection(temp);
    }
    cout << "I'm outta here!" << endl;
  }//end doLoop

Answer and Explanation

170-25.  True or False.  The expression shown in Listing 170-25 evaluates to true if A is not equal to B, and evaluates to false if B is equal to B.

Listing 170-25.
A != B

Answer and Explanation

170-26.  True or False.  The function named doLoop shown in Listing 170-26 continues to loop until the function named doSelection returns a value of false, at which point the function terminates.

Listing 170-26.
  void doLoop(){
    bool quit = false;
    int temp = 0;
    while(quit != true){
      cout << "Enter an even integer to quit\n" 
           << "or an odd integer to loop: ";
      cin >> temp;
      quit = doSelection(temp);
    }
    cout << "I'm outta here!" << endl;
  }//end doLoop

Answer and Explanation

170-27.  True or False.  The function shown in Listing 170-27 implements a loop structure.

Listing 170-27.
  bool aFunction(int data){
    bool result;
    if(data % 2 == 0){
      result = true;
    }else{
      result = false;
    }//end else
    //End selection structure
    return result;
  }//end aFunction

Answer and Explanation

170-28.  True or False.  For the function shown in Listing 170-28

Listing 170-28.
  bool aFunction(int data){
    bool result;
    if(data % 2 == 0){
      result = true;
    }else{
      result = false;
    }//end else
    //End selection structure
    return result;
  }//end aFunction

Answer and Explanation

170-29.  True or False.  A while loop is known as an entry-condition loop.  This is because the conditional test is performed before the code in the body of the loop is executed.

Answer and Explanation

170-30.  True or False.  For an entry-condition loop, if the test returns true, the code in the body of the loop is executed and the test is performed again.  When the test returns false, even if it is first time that the test is performed, the code in the body of the loop is skipped and control exits the loop structure.  Thus, if the test returns false the first time that it is performed, the code in the body of the loop won't be executed.

Answer and Explanation

170-31.  True or False.  C++ has an exit-condition loop, known as a do-while loop.  The major significance of the exit-condition loop is that the code in the body of the loop will not be executed if the test returns false the first time that it is performed.

Answer and Explanation

170-32.  For the program named Structures04a shown in Listing 170-32, what will the program output be if the user enters the value 3 when requested by the program to enter an integer?

Listing 170-32.
/*File:  Structures04a.cpp
This C++ program illustrates the if else if
structure.
************************************************/

#include 
using namespace std;

class Structures04a{ 
  public:
  static void classMain(){
    Structures04a* ptrToObject = 
                              new Structures04a();
    ptrToObject -> doSomething();
  }//End classMain function
  //-------------------------------------------//

  void doSomething(){

    int temp = 0;
    cout << "Enter an integer, 1-3 inclusive: ";
    cin >> temp;

    if(temp == 1){
      cout << "C" << endl;
    }else if(temp == 2){
      cout << "B" << endl;
    }else if(temp == 3){
      cout << "A" << endl;
    }else{
      cout << "Invalid input" << endl;
    }//end else
    //End selection structure

  }//end doSomething function
};//End Structures04a class
//---------------------------------------------//

int main(){
  Structures04a::classMain();
  return 0;
}//end main

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 32

A.  The character A

Back to Question 32

Explanation 32

Answer 31

False

Back to Question 31

Explanation 31

Answer 30

True

Back to Question 30

Explanation 30

Answer 29

True

Back to Question 29

Explanation 29

Answer 28

True

Back to Question 28

Explanation 28

Answer 27

False

Back to Question 27

Explanation 27

The function executes a selection structure.

Answer 26

False

Back to Question 26

Explanation 26

The doSelection function must return a value of true to cause the loop to terminate.

Answer 25

True

Back to Question 25

Explanation 25

Answer 24

True

Back to Question 24

Explanation 24

Answer 23

True

Back to Question 23

Explanation 23

Answer 22

False

Back to Question 22

Explanation 22

The loop control variable is named quit.

Answer 21

False

Back to Question 21

Explanation 21

They are local variables, not instance variables.  Instance variables cannot be declared inside a function.

Answer 20

True

Back to Question 20

Explanation 20

Answer 19

True

Back to Question 19

Explanation 19

Answer 18

True

Back to Question 18

Explanation 18

Answer 17

False

Back to Question 17

Explanation 17

The words sequence and selection are reversed.

Answer 16

A.  Sequence structure

Back to Question 16

Explanation 16

The call to the function named doSelection is simply one of the action elements in the sequence structure.

Answer 15

False

Back to Question 15

Explanation 15

This is a sequence structure.

Answer 14

True

Back to Question 14

Explanation 14

Answer 13

False

Back to Question 13

Explanation 13

True and false are reversed in this question.

Answer 12

True

Back to Question 12

Explanation 12

Answer 11

True

Back to Question 11

Explanation 11

Answer 10

True

Back to Question 10

Explanation 10

Answer 9

False

Back to Question 9

Explanation 9

The test is simply for true or false.

Answer 8

False

Back to Question 8

Explanation 8

There is no exception to the rule regarding one entry point and one exit point.

Answer 7

True

Back to Question 7

Explanation 7

Answer 6

True

Back to Question 6

Explanation 6

Answer 5

True

Back to Question 5

Explanation 5

Answer 4

False

Back to Question 4

Explanation 4

Structured programming does not allow the use of a goto instruction.

Answer 3

True

Back to Question 3

Explanation 3

Answer 2

True

Back to Question 2

Explanation 2

Answer 1

False

Back to Question 1

Explanation 1

Top down is not one of the structures.



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-