Chapter 7 Exercise Set 1: CPE Practice

Namespaces

According to the C++ Reference “Namespaces provide a method for preventing name conflicts in large projects.”

Since this undergraduate C++ book will not involve the kinds of large projects that require the use of namespaces, they will not be discussed in the main text. They are on the CPE certification, however, and so are introduced here.

According to Wikipedia, “a namespace ensures that all of a given set of objects have unique names so that they can be easily identified.”

The following example shows the creation of a separate namespace for each high school and high school program in Arlington Public Schools. The string variable principal is defined in each, but it has a different value in each namespace.

#include <iostream>
#include <string>
using namespace std;

namespace ghc {
    string principal = "Margaret Chung";
}
namespace hb {
    string principal = "Casey Robinson";
}
namespace wakefield {
    string principal = "Peter Balas";
}
namespace w_l {
    string principal = "Tony Hall";
}
namespace yorktown {
    string principal = "Kevin Clark";
}

int main() {
    cout << ghc::principal << " is the principal of the Grace Hopper Center.\n";
    cout << hb::principal << " is the principal of the H-B Woodlawn Program.\n";
    cout << wakefield::principal << " is the principal of Wakefield High.\n";
    cout << w_l::principal << " is the principal of W-L High.\n";
    cout << yorktown::principal << " is the principal of Yorktown High.\n";
    return 0;
}

The scope resolution operator (::) is used to specify the context – school in this case – of the variable.

  1. What is the output of the following program?

    #include <iostream>
    #include <string>
    using namespace std;
    
    int main() {
        int n = 2;
        string s = "2";
        cout << (s == n) <<  endl;
        return 0;
    }
    
    1. 1

    2. The program causes a runtime error

    3. 0

    4. Compilation fails

  2. What is the output of the following program?

    #include <iostream>
    #include <string>
    using namespace std;
    
    int main() {
        string s = "ABC";
        s.append(s.substr(2)).push_back(s[s.length() - 3]);
        cout << s << endl;
        return 0;
    }
    
  3. What is the output of the following program?

    #include <iostream>
    #include <string>
    using namespace std;
    
    string multi_copy(string s = "_", int n = 1) {
        string clone_str;
        while (n--)
            clone_str += s;
        return clone_str;
    }
    
    int main() {
        string pattern = "!@";
        cout << multi_copy(pattern) << multi_copy () << multi_copy("#", 3) << endl;
        return 0;
    }
    
  4. What is the output of the following program?

    #include <iostream>
    #include <string>
    using namespace std;
    
    int main() {
        string s1 = "ACC";
        string s2 = "GCTAA";
        string s;
        s1.swap(s2);
        s2.swap(s);
        s.swap(s2);
        cout << s1 << endl;
        return 0;
    }
    
  5. What is the output of the following program?

    #include <iostream>
    #include <string>
    using namespace std;
    
    int main() {
        string s = "School";
        int i = s.compare("Grade") > 0;
        int j = s.compare("High") < 0;
        cout << i << j << endl;
    
        return 0;
    }
    
  6. What is the output of the following program?

    #include <iostream>
    #include <string>
    using namespace std;
    
    int main() {
        string s1 = "Abc", s2 = "A";
        int i = s1.compare(s2) < 0;
        int j = s1.length() > s2.length();
        cout << i << j << endl;
        return 0;
    }
    
  7. What is the output of the following program?

    #include <iostream>
    #include <string>
    using namespace std;
    
    int main() {
        string s1, s2;
        s1 = "XYZ";
        s2 = s1.substr(1, 1) + s1.substr(2) + s1.substr();
        s1 = s2.substr(1, 1) + s2.substr(2) + s2.substr();
        cout << s1 << ' ' << s2 << endl;
        return 0;
    }
    
  8. What is the output of the following program?

    #include <iostream>
    #include <string>
    using namespace std;
    
    int main() {
        string s = "W";
        s.append(s).append(s).append(s);
        cout << s.length() << endl;
        return 0;
    }
    
  9. What is the output of the following program?

    #include <iostream>
    #include <string>
    using namespace std;
    
    namespace alpha {
        int i = 4;
    }
    namespace beta {
        int i = alpha::i + 1;
    }
    int main() {
        beta::i += alpha::i;
        {
            using namespace beta;
            cout << i << endl;
        }
        return 0;
    }
    
  10. What is the output of the following program?

    #include <iostream>
    #include <string>
    using namespace std;
    
    namespace alpha {
        int i = 4;
    }
    
    namespace beta {
        int i = alpha::i + 1;
    }
    
    int main() {
        beta::i += alpha::i;
        {
            using namespace beta;
            cout << i << endl;
        }
    
        return 0;
    }
    
  11. What is the output of the following program?

    #include <iostream>
    using namespace std;
    
    namespace ACC {
        int program_count = 4;
    }
    namespace ACC {
        int programs2024 = program_count + 2;
    }
    int main() {
        ACC::program_count /= 2;
        {
            using namespace ACC;
            programs2024++;
        }
        cout << ACC::program_count << ' ' << ACC::programs2024 << endl;
        return 0;
    }
    
  12. What is the output of the following program?

    #include <iostream>
    #include <string>
    using namespace std;
    
    int main() {
        string s = "CS@ACC";
        for (int i = 1; i < s.length(); i += 2)
            s[i - 1] = s[i] + 'a' - 'A';
        cout << s << endl;
        return 0;
    }
    
  13. What is the output of the following program?

    #include <iostream>
    using namespace std;
    
    namespace A {
        int n = 1;
    }
    
    namespace B {
        int n = 2;
    }
    
    int main() {
        { using namespace A;
            B::n = n + 1;
        }
        { using namespace B;
            A::n = n + 1;
        }
        cout << A::n << ' ' << B::n << endl;
        return 0;
    }
    
  14. What is the output of the following program?

    #include <iostream>
    #include <string>
    using namespace std;
    
    int main() {
        string s1 = "acc";
        string s2 = "ACC";
        if (s1 > s2)
            cout << "YES" << endl;
        else
            cout << "NO" << endl;
        return 0;
    }
    
  15. What is the output of the following program?

    #include <iostream>
    #include <string>
    using namespace std;
    
    int main() {
        string s1 = "A";
        string s2 = "B";
        string s3 = "C";
        s1.swap(s2);
        s2.swap(s3);
        s3.swap(s2);
        cout << s1 << s2 << s3 << endl;
        return 0;
    }
    
  16. What is will happen when you compile and run the following program?

    #include <iostream>
    using namespace std;
    
    int main() {
        string s = "x";
        cout << s + "y" + "z" << endl;
        return 0;
    }
    

    How about this the following one?

    #include <iostream>
    using namespace std;
    
    int main() {
        string s = "z";
        cout << "x" + "y" + s << endl;
        return 0;
    }
    
  17. What is the output of the following program?

    #include <iostream>
    #include <string>
    using namespace std;
    
    int main() {
        string s = "PQ";
        s.append(s).push_back(s[s.length() - 1]);
        cout << s << endl;
        return 0;
    }
    
  18. What is the output of the following program?

    #include <iostream>
    #include <string>
    using namespace std;
    
    int main() {
        string s = "ABCDEFGHIJ";
        cout << s.substr(2, 5).substr(2).substr() << endl;
        return 0;
    }
    
  19. What is the output of the following program?

    #include <iostream>
    #include <string>
    using namespace std;
    
    int main() {
        string s1 = "ACC";
        string s2 = "ACC!";
        cout << (s1.compare(s2) < 0) << endl;
        return 0;
    }
    
  20. 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 main() {
        cout << f(3, 1, 2) << f(5, 2) << f(0) << endl;
        return 0;
    }
    
  21. What is the output of the following program?

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

    #include <iostream>
    using namespace std;
    
    int f(char x) {
        return x * x;
    }
    
    int f(float g) {
        return int(g);
    }
    
    int f(int a, int b = 0, int c = 5) {
        int total = 0;
        while (--c)
            total += b;
        return total - a;
    }
    
    int main() {
        char ch = 3;
        cout << f(2, 3, 4) << f(0, 2) << f(1) << f(ch) << endl;
        return 0;
    }