Chapter 4 Exercise Set 1: CPE PracticeΒΆ

  1. What is the output of the following program?

    #include <iostream>
    using namespace std;
    
    int main() {
        int i = 4;
        float f = 5;
        bool b1 = i < f, b2 = f < i, b3 = f / i < i / f;
        if (b3)
            if (b2)
                i += 1;
            else
                i += 2;
        else if (b1)
            i += 3;
        else
            i += 4;
        cout << i << endl;
    
        return 0;
    }
    
  2. What is the final value of the c variable?

    int a = 4, b = 3, c = 2;
    if (a > 0) {
        b -= 4;
        if (b > 0) {
            if (c > 0)
                c++;
            if (c <= 3)
                c--;
        }
    if (b < 0)
        a--;
    }
    c = a + b + c;
    
  3. What is the value of the n variable?

    int n = 1 % 2 + 4 % 2
    
  4. What is the output of the following program?

    #include <iostream>
    using namespace std;
    
    int main() {
        int x = 3, y = x++, z = ++x;
        cout << (z - x < y - x ? 5 : 6) <<  endl;
        return 0;
    }
    
  5. What is the output of the following program?

    #include <iostream>
    using namespace std;
    
    int main() {
        int n = 2, m = n >> 1, p = m >> n, q = 1 << p, r = q << q;
        cout << m << p << q << r << endl;
        return 0;
    }
    
  6. What is the output of the following program?

    #include <iostream>
    using namespace std;
    
    int main() {
        int n = 2, m = n >> 1, p = n >> m, q = 1 << p, r = q >> q;
        cout << m << p << q << r << endl;
        return 0;
    }
    
  7. What is the output of the following program?

    #include <iostream>
    using namespace std;
    
    int main() {
        int x = 0x02, y = 011, z = x ^ y, u = z | x, v = u & 0;
        cout << x << y << z  << u << v << endl;
        return 0;
    }
    
  8. What is the output of the following program?

    #include <iostream>
    using namespace std;
    
    int main() {
        int n = (3 % 4 + 6) << 2;
        cout << n << endl;
        return 0;
    }
    
  9. What is the value of the n variable?

    int n = (3 << 2) + (4 >> 1);
    
  10. What is the output of the following program?

    #include <iostream>
    using namespace std;
    
    int main() {
        char a = 3 << 1, b = a | 3, c = a ^ b;
        cout << int(a) << int(b) << int(c) << endl;
        return 0;
    }
    
  11. Given that the ASCII value of 'a' is 97, what is the output of the following program?

    #include <iostream>
    using namespace std;
    
    int main() {
        unsigned char n = 'a', m = ~n;
        cout << int(m) << endl;
        return 0;
    }
    
  12. What is the output of the following program?

    #include <iostream>
    using namespace std;
    
    int main() {
        unsigned char n = 6, m = 9;
        cout << (n & m ? "yes" : "no") << endl;
        return 0;
    }
    
  13. What is the output of the following program?

    #include <iostream>
    using namespace std;
    
    int main() {
        bool b1 = true, b2 = false, b3 = !b2;
        cout << (b1 || b2 ? "mustard" : "ketchup");
        cout << " and ";
        if (b3)
            if (b1 && !(b2 && b3))
                cout << "ice cream";
            else
                cout << "spinach";
        cout << endl;
        return 0;
    }
    
  14. What is the output of the following program?

    #include <iostream>
    using namespace std;
    
    int main() {
        int n = 1, m = n << 2;
        switch(m) {
            case 1:
                n += 1;
                break;
            case 2:
                n += 2;
                break;
            case 4:
                n += 3;
                break;
            default:
                n += 4;
        }
        cout << n << endl; 
        return 0;
    }
    
  15. What is the output of the following program?

    #include <iostream>
    using namespace std;
    
    int main() {
        bool yes = !false;
        bool no = !yes;
        if (!no)
            cout << "true";
        else
            cout << "false";
        cout << endl;
    
        return 0;
    }
    
  16. What is the output of the following program?

    #include <iostream>
    using namespace std;
    
    int main() {
        int a = 0, b = a++, c = --a;
    
        if (a > 0)
            b++;
        else
            c++;
        if (c == 0)
            c--;
        else
            c++;
    
        cout << a + b + c << endl; 
    
        return 0;
    }
    
  17. What is the output of the following program?

    #include <iostream>
    using namespace std;
    
    int main() {
        int n = 2;
        switch (n << n) {
            case 8: n++;
            case 4: n++;
            case 2: break;
            case 1: n--;
        }
        cout << n << endl; 
        return 0;
    }