Revised: February 4, 2007
By Richard G. Baldwin
File Pfsg00180.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 #180 titled Relational and Logical Operators.
Listing 180-1.
void doSomething(){ char temp = 0; cout << "Enter an upper-case letter: "; cin >> temp; while((temp < 'A') || (temp > 'Z')){ //Outside allowable character range cout << "Bad input" << endl; cout << "Enter an upper-case letter: "; cin >> temp; }//end while loop //End end loop structure //Good input, display msg and terminate. cout << "Thanks for the " << temp << endl; }//end doSomething function |
180-2. True or False. The character codes for the 26 upper-case characters A through Z are the numeric values ranging from 60 through 95 inclusive.
180-3. True or False. The conditional clause in Listing 180-3 tests to see if the character entered by the user and stored in the variable temp falls inside the range from 65 through 90 inclusive.
Listing 180-3.
void doSomething(){ char temp = 0; cout << "Enter an upper-case letter: "; cin >> temp; while((temp < 'A') || (temp > 'Z')){ cout << "Bad input" << endl; cout << "Enter an upper-case letter: "; cin >> temp; }//end while loop cout << "Thanks for the " << temp << endl; }//end doSomething function |
180-4. True or False. The conditional clause in 180-4 tests to see if the value of the character entered by the user and stored in the variable temp is less than 65 (A) OR is greater than 90 (Z).
Listing 180-4.
void doSomething(){ char temp = 0; cout << "Enter an upper-case letter: "; cin >> temp; while((temp < 'A') || (temp > 'Z')){ cout << "Bad input" << endl; cout << "Enter an upper-case letter: "; cin >> temp; }//end while loop cout << "Thanks for the " << temp << endl; }//end doSomething function |
180-5. True or False. The conditional clause in Listing 180-5 returns false causing the body of the loop to be skipped if the character code is outside the specified range from 65 through 90 inclusive.
Listing 180-5.
void doSomething(){ char temp = 0; cout << "Enter an upper-case letter: "; cin >> temp; while((temp < 'A') || (temp > 'Z')){ cout << "Bad input" << endl; cout << "Enter an upper-case letter: "; cin >> temp; }//end while loop cout << "Thanks for the " << temp << endl; }//end doSomething function |
180-6. True or False. The body of the loop in Listing 180-6 displays an error message and requests another input character from the user. When the user enters another character, the test is performed again and the process repeats.
Listing 180-6.
void doSomething(){ char temp = 0; cout << "Enter an upper-case letter: "; cin >> temp; while((temp < 'A') || (temp > 'Z')){ cout << "Bad input" << endl; cout << "Enter an upper-case letter: "; cin >> temp; }//end while loop cout << "Thanks for the " << temp << endl; }//end doSomething function |
180-7. True or False. For the function shown in Listing 180-7, the C++ compiler and/or the runtime system automatically translates 'A' into 60 and automatically translates 'Z' into 90.
Listing 180-7.
void doSomething(){ char temp = 0; cout << "Enter an upper-case letter: "; cin >> temp; while((temp < 'A') || (temp > 'Z')){ cout << "Bad input" << endl; cout << "Enter an upper-case letter: "; cin >> temp; }//end while loop cout << "Thanks for the " << temp << endl; }//end doSomething function |
180-8. True or False. For the function shown in Listing 180-8, if the value of the character stored in temp does not fall outside the range of 65 through 90 inclusive, the conditional clause returns false. In this case, the while loop terminates, bypassing the body of the loop, and control transfers to the last statement shown in Listing 180-8.
Listing 180-8.
void doSomething(){ char temp = 0; cout << "Enter an upper-case letter: "; cin >> temp; while((temp < 'A') || (temp > 'Z')){ cout << "Bad input" << endl; cout << "Enter an upper-case letter: "; cin >> temp; }//end while loop cout << "Thanks for the " << temp << endl; }//end doSomething function |
180-9. True or False. For the while loop shown in Listing 180-9, the conditional clause tests to determine if the value stored in the variable named temp falls outside the range from 65 through 90 inclusive.
Listing 180-9.
while(! ((temp >= 'A') && (temp <= 'Z')) ){ cout << "Bad input" << endl; cout << "Enter an upper-case letter: "; cin >> temp; }//end while loop |
180-10. True or False. For the while loop shown in Listing 180-10, we can express the behavior of the conditional clause in words by saying that the conditional clause tests to see if the value of temp is not outside the range from 65 through 90 inclusive.
Listing 180-10.
while(! ((temp >= 'A') && (temp <= 'Z')) ){ cout << "Bad input" << endl; cout << "Enter an upper-case letter: "; cin >> temp; }//end while loop |
180-11. True or False. For the while loop shown in Listing 180-11, the code shown in boldface inside the conditional clause returns true if the value of temp is within the range from 65 through 90 inclusive. Then the application of the negation operator (!) changes that value to false, causing the entire conditional clause to return false.
Listing 180-11.
while(! ((temp >= 'A') && (temp <= 'Z')) ){ cout << "Bad input" << endl; cout << "Enter an upper-case letter: "; cin >> temp; }//end while loop |
180-12. True or False. For the while loop shown in Listing 180-12, the code shown in boldface inside the conditional clause returns false if the value of temp is outside the range from 65 through 90 inclusive. Then the application of the negation operator (!) changes that value to true, causing the entire conditional clause to return true.
Listing 180-12.
while(! ((temp >= 'A') && (temp <= 'Z')) ){ cout << "Bad input" << endl; cout << "Enter an upper-case letter: "; cin >> temp; }//end while loop |
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.
The conditional clause tests to see if the value of temp is not inside the range from 65 through 90 inclusive.
It automatically translates 'A' into 65.
It returns true causing the body of the loop to be executed.
The test is for outside the range from 65 through 90 inclusive.
The correct range is 65 through 90.
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-