## 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:
• Sequence
• Selection or decision
• Loop or iteration
• Top down

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.

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.

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

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.

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.

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

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

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.

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.

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

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.

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

• One or more actions are performed in sequence
• The condition is tested again.

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

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.

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

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

• A.  Sequence structure
• B.  Selection structure
• C.  Loop structure
• D.  Round-and-round structure
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

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

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

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

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

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

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

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

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

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

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

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

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

• If the incoming parameter value is even, the function returns true.
• Otherwise, the function returns false.
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

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.

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.

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.

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?

• A.  The character A
• B.  The character B
• C.  The character C
• D.  The words Invalid input
• E.  None of the above.
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

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.

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.

A.  The character A

False

True

True

True

False

The function executes a selection structure.

False

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

True

True

True

False

The loop control variable is named quit.

False

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

True

True

True

False

The words sequence and selection are reversed.

A.  Sequence structure

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

False

This is a sequence structure.

True

False

True and false are reversed in this question.

True

True

True

False

The test is simply for true or false.

False

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

True

True

True

False

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

True

True

False

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.