Chapter 8 Exercise Set 1: CPE PracticeΒΆ

  1. What is the output of the following program?

    #include <iostream>
    using namespace std;
    
    struct A
    {
        int a;
        float b;
    };
    
    struct B
    {
        int b;
        float a;
    };
    
    struct C
    {
        A a;
        B b;
    };
    
    int main() {
        C c1 = {1, 2, 3, 4}, c2 = {5, 6, 7, 8};
        cout << c1.b.a + c2.a.b << endl;
    
        return 0;
    }
    
  2. What is the output of the following program?

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

    #include <iostream>
    using namespace std;
    
    int f(int n) {
        ++n;
        return n++;
    }
    
    int f2(int &n) {
        ++n;
        return n++;
    }
    
    int main() {
        int x = 2, y, z;
        y = f(x);
        z = f2(y);
        cout << x + y + z << endl;
        return 0;
    }
    
  4. What is the output of the following program?

    #include <iostream>
    using namespace std;
    
    struct A
    {
        char a;
        char b;
    };
    
    struct B
    {
        char a;
        double b;
    };
    
    struct C
    {
        A a;
        B b;
    };
    
    int main() {
        A a = {'a', 'A'};
        B b = {'B', 3.14};
        C c = {{'c', 'C'}, {'d', 4.9}};
        cout << (a.b < c.b.a) << ' ' << c.a.a + b.b << ' ' << char(a.b + 5);
        cout << endl;
        return 0;
    }