Chapter 7 Exercise Set 1: CPE PracticeΒΆ

  1. What is the output of the following program?

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

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

    2. ***

    3. ****

    4. *****

  3. What is the output of the following program?

    #include <iostream>
    using namespace std;
    
    int somesum(int n = 1, int m = 2) {
        return n + m;
    }
    
    int main() {
        cout << somesum() + somesum(0) + somesum(2, 3) << endl;
        return 0;
    }