COSC 1315

Programming Fundamentals

 Practice Text #180

Relational and Logical Operators

Revised: February 4, 2007
By Richard G. Baldwin

File Pfsg00180.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 #180 titled Relational and Logical Operators.

Questions



180-1.  True or False.  The doSomething function shown in Listing 180-1 contains a priming read from the keyboard.

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

Answer and Explanation

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.

Answer and Explanation

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

Answer and Explanation

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

Answer and Explanation

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

Answer and Explanation

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

Answer and Explanation

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

Answer and Explanation

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

Answer and Explanation

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

Answer and Explanation

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

Answer and Explanation

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

Answer and Explanation

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

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 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 conditional clause tests to see if the value of temp is not inside the range from 65 through 90 inclusive.

Answer 9

True

Back to Question 9

Explanation 9

Answer 8

True

Back to Question 8

Explanation 8

Answer 7

False

Back to Question 7

Explanation 7

It automatically translates 'A' into 65.

Answer 6

True

Back to Question 6

Explanation 6

Answer 5

False

Back to Question 5

Explanation 5

It returns true causing the body of the loop to be executed.

Answer 4

True

Back to Question 4

Explanation 4

Answer 3

False

Back to Question 3

Explanation 3

The test is for outside the range from 65 through 90 inclusive.

Answer 2

False

Back to Question 2

Explanation 2

The correct range is 65 through 90.

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-