Chapter 5 Exercise Set 1: CPE PracticeΒΆ

  1. What is the output of the following program?

    #include <iostream>
    using namespace std;
    
    int main() {
      bool ints = sizeof(long) >= sizeof(int) && sizeof(int) >= sizeof(short);
      bool floats = sizeof(float) == 0.5 * sizeof(double);
      bool chars = sizeof(char) == 1;
      int v = ints && floats && chars;
      cout << v << endl;
    
      return 0;
    }
    
    1. True

    2. False

    3. 1

    4. 0

  2. What is the output of the following program?

    #include <iostream>
    using namespace std;
    
    int f() {
        return 1;
    }
    
    int f(int n) {
        return n + 1;
    }
    
    int f(int n, int m) {
        return n + m + 1;
    }
    
    int main() {
        cout << f() + f(0) + f(3, 4) << endl;
        return 0;
    }
    
  3. What is the output of the following program?

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

    #include <iostream>
    using namespace std;
    
    int f(int a) {
        int b = 0;
        int c = 5;
        int total = 0;
        while (--c)
            total += b;
        return total - a;
    }
    
    int f(int a, int b) {
        int c = 5;
        int total = 0;
        while (--c)
            total += b;
        return total - a;
    }
    
    int f(int a, int b, int c) {
        int total = 0;
        while (--c)
            total += b;
        return total - a;
    }
    
    int main() {
        cout << f(3, 1, 2) << f(5, 2) << f(0) << endl;
        return 0;
    }
    
  5. What is the output of the following program?

    #include <iostream>
    using namespace std;
    
    int f(char x, float y) {
        return 2 * x + y;
    }
    
    int f(float x, char y) {
        return x - y;  
    }
    
    int f(int a, int b, int c) {
        int total = 0;
        while (--c)
            total += b;
        return total - a;
    }
    
    int main() {
        char c1 = 3;
        char c2 = 5;
        cout << f(1, 4, 2) << f(c2, 2.0) << f(4.0, c1) << endl;
        return 0;
    }
    
  6. What is the output of the following program?

    #include <iostream>
    using namespace std;
    
    int f(char x) {
        return x * x;
    }
    
    int f(double g) {
        return int(g);
    }
    
    int f(int a, int b, int c) {
        int total = 0;
        while (--c)
            total += b;
        return total - a;
    }
    
    int f(double a, int b, char c) {
        return int(a + b + c);
    }
    
    int main() {
        char ch = 3;
        cout << f(2, 3, 4) << f(3.0, 1, ch) << f(1.0) << f(ch) << endl;
        return 0;
    }
    
  7. What is the output of the following program?

    #include <iostream>
    using namespace std;
    
    int main() {
        int i = 12;
        float f = 1000.0;
        while (i > 0) {
            i -= 3;
            f /= 10;
        }
        cout << f << endl;
        return 0;
    }
    
  8. What is the output of the following program?

    #include <iostream>
    using namespace std;
    
    int main() {
        int i = 12;
        float f = -1.0;
        while (i < 0) {
            f = f + 5.0 * f / -5; 
            --i;
        }
        cout << i << endl;
        return 0;
    }
    
  9. What is the output of the following program?

    #include <iostream>
    using namespace std;
    
    int f() {
        return 1;
    }
    
    int f(int n) {
        return n % 2 == 0 ? 1 : 0;
    }
    
    int f(int n, int m) {
        return n > m ? n - m : m - n;
    }
    
    int main() {
        cout << f() + f(11) + f(3, 4) << endl;
        return 0;
    }
    
  10. What is the output of the following program? (Note: Watch our for the >>=, which is not a comparison operator, but a compound assignment operator.)

    #include <iostream>
    using namespace std;
    
    int doit(int n, int t = 2) {
        int x = n;
        while (t >>= 1)
            x++;
        return x;
    }
    
    int main() {
        cout << doit(2, 4) << doit(6) << endl;
        return 0;
    }
    
  11. What is the output of the following program?

    #include <iostream>
    using namespace std;
    
    int main() {
        int a, b = 2 | 5;
        for (a = 0; a < 3; a += 2)
            b++;
        cout << b << endl;
        return 0;
    }
    
  12. What is the output of the following program?

    #include <iostream>
    using namespace std;
    
    int main() {
        int n = 0, m = 7 ^ 5;
        while (n == 0) {
            if (m > 1)
                n = m;
            ++m;
        }
        cout << n << m << endl;
        return 0;
    }
    
  13. What is the output of the following program?

    #include <iostream>
    using namespace std;
    
    int main() {
        for (float val = -10.0; val < 100.0; val = -val * 2) {
            if (val < 0 && -val >= 40)
                break;
            cout << '*'; 
        }
        cout << endl;
    
        return 0;
    }
    
  14. What is the output of the following program?

    #include <iostream>
    using namespace std;
    
    int main() {
        float v = 500.0;
        do {
            v /= 5;
            cout << '*';
        } while (v > 1.0);
        cout << endl;
    
        return 0;
    }
    
  15. What is the output of the following program?

    #include <iostream>
    using namespace std;
    
    int main() {
        int u = 2, v = 1;
        do {
            u--;
            v++;
        } while (u >= 0);
        cout << u << '&' << v << endl;
    
        return 0;
    }
    
  16. What is the output of the following program?

    #include <iostream>
    using namespace std;
    
    int main() {
        char ch = 'd';
        for (float f = -4.0; f < 100; f *= -2)
            cout << ++ch;
        cout << endl;
    
        return 0;
    }
    
  17. What is the output of the following program?

    #include <iostream>
    using namespace std;
    
    int main() {
        int a = 3, b = 1;
    
        while (a > 0) {
            a--;
            b++;
        }
    
        cout << a << ' ' << b << endl; 
    
        return 0;
    }
    
  18. What is the output of the following program?

    #include <iostream>
    using namespace std;
    
    int main() {
        for (float f = -4.0; f < 500; f *= -2)
            cout << '*';
        cout << endl;
    
        return 0;
    }
    
  19. What is the output of the following program?

    #include <iostream>
    using namespace std;
    
    char f(char n) {
        int count = 0;
        for (n = 4 * n; n > 0; n >>= 2)
            count++;
        return count;
    }
    
    void f(int n) {
        for (char count = f(char(n)); count > 0; count--)
            cout << '*';
    }
    
    int main() {
        char x = 3;
        f(3);
        cout << endl;
        return 0;
    }