Chapter 5 Exercises

  1. Given the following code, what is displayed?
    int x = 3, y = 5;
    
    cout << "x < y: "      << (x < y) << endl;
    cout << "x + 2 < y: "  << (x + 2 < y) << endl;
    cout << "x != y: "     << (x != y) << endl;
    cout << "x + 3 >= y: " << (x + 3 >= y) << endl;
    cout << "y == x: "     << (y == x) << endl;
    cout << "y == x+2: "   << (y == x+2) << endl;
    cout << "y = x + 3: "  << (y = x + 3) << endl;
    
  2. Given the following code, what is displayed?
    string prevState, homeState;
    prevState = "Maryland";
    homeState = "Tennessee";
    
    cout << "prevState == homeState: " << (prevState == homeState) << endl;
    cout << "prevState >  homeState: " << (prevState >  homeState) << endl;
    cout << "homeState == \"Tennessee\": " << (homeState == "Tennessee") << endl;
    cout << "homeState <  \"tennessee\": " << (homeState < "tennessee") << endl;
    
  3. Given the following code, what is displayed?
    int   age = 20;  
    float temperature = 102.0;
    bool  isSenior = false, hasFever = false;
    
    isSenior = (age >= 55); 
    hasFever = (temperature > 98.6);    
    
    cout << "isSenior && hasFever: " << (isSenior && hasFever) << endl;
    cout << "isSenior || hasFever: " << (isSenior || hasFever) << endl;
    cout << "! isSenior: " << (! isSenior) << endl;
    cout << "! hasFever: " << (! hasFever) << endl;
    
  4. Write an expression for each of the following:
    1. taxRate is over 25% and income is less than $20,000
    2. temperature is less than or equal to 75 degrees or humidity is less than 70%
    3. age is over 21 and less than 60
    4. age is 21 or 22
  5. Given the following code, what is displayed?
    int airQuality = 35;
    cout << "50 < airQuality < 80: " << (50 < airQuality < 80) << endl;
    
  6. Given the variable number (which is an int) write code to display a one word description of "Positive", "Negative" or "Zero" based on number's value.