Self-test 1:
C++ Class Basics

  1. If a member of a class is ____, you cannot access it outside the class.

    1. static
    2. public
    3. private
    4. constant
    Solution
    private

  2. In C++, class is a reserved word, and it defines only a(n) ____; no memory is allocated.

    Solution
    data type

  3. If a class object is passed by _____, the contents of the member variables of the actual parameter are copied into the corresponding member variables of the formal parameter.

    1. value
    2. reference
    3. either value or reference
    4. None of the above
    Solution

    value


  4. The word ____ at the end of a member function heading specifies that the function cannot modify the member variables of that object.

    1. protected
    2. private
    3. public
    4. const
    Solution

    const


  5. If a member function is used only to implement other member functions of a class and the user does not need to access the function, you should declare it ____

    Solution

    private


  6. Which of the following is true regarding parameter passing with class objects?

    1. A class object may be passed by reference only.
    2. A class object may be passed by value only.
    3. A class object may be passed by reference or by value.
    4. A class object may not be passed as a parameter.
    Solution

    A class object may be passed by reference or by value.


  7. A constructor:

    1. Obtains memory for a new variable
    2. Enables the initialization of an object as it is created
    3. Constructs a new data type
    4. None of the above
    Solution

    Enables the initialization of an object as it is created


  8. The members of a class, by default, are:

    1. public
    2. protected
    3. private
    4. mandatory to specify

    Solution

    private


  9. What is the output of the following code

    #include <iostream>
    using namespace std;
    
    inline int cubic(int s)
    {
        return s*s*s;
    }
    
    int main()
    {
        cout << "The cubic of 3 is: " << cubic(3) << "\n";
        return 0;
    }
    
    Solution

    The cubic of 3 is: 27


  10. What is the output of the following program when 45 and 15 are entered (in the order)?

    #include <iostream>
    using namespace std;
    
    class Operation
    {
        int a, b, aa, ss, mm, vv;
      public:
        void get();
        void su();
        void di();
        void pr();
        void dv();
    };
    
    inline void Operation::get()
    {
        cout << "Enter first value:";
        cin >> a;
        cout << "Enter second value:";
        cin >> b;
    }
    
    inline void Operation::su()
    {
        aa = a+b;
        cout << ": " << a+b << "\n";
    }
    
    inline void Operation::di()
    {
        ss = a-b;
        cout << ": " << a-b << "\n";
    }
    
    inline void Operation::pr()
    {
        mm = a*b;
        cout << ": " << a*b << "\n";
    }
    
    inline void Operation::dv()
    {
        vv = a/b;
        cout << ": " << a/b << "\n";
    }
    
    int main()
    {
        cout << "Program using inline function\n";
        Operation s;
        s.get();
        s.su();
        s.di();
        s.pr();
        s.dv();
        return 0;
    }
    
    
    
    Solution
    : 60
    : 30
    : 675
    : 3 

  11. What is the output of the following program?

    #include <iostream>
    using namespace std;
    
    class Test
    {
      private:
        int x;
      public:
        Test(int x = 0) { this->x = x; }
        void change(const Test  *t) { *this = *t; }
        void print() const { cout << "x = " << x << endl; }
    };
    
    int main()
    {
        Test obj(5);
        Test* ptr = new Test (10);
        obj.change(ptr);
        obj.print();
        return 0;
    }
    
    Solution
    x = 10

  12. In the question above, the body of the constructor of Test reads: this->x = x; What does the first and second x refer to?

    1. the first x is the class member variable
    2. the second x is the class member variable
    3. the first x is the function input argument
    4. the second x is the function input argument
    Solution
    The first x is the class member variable, and the second is the function input argument

  13. In the following program, is the line: obj1.setX(10).setY(20); correct? Why?

    #include<iostream>
    using namespace std;
    
    class Test
    {
      private:
        int x;
        int y;
      public:
        Test(int a = 0, int b = 0) { x = a; y = b; }
        Test& setX(int a) { x = a; return *this; }
        Test& setY(int b) { y = b; return *this; }
        void print() const { cout << "x = " << x << " y = " << y << endl; }
    };
    
    int main()
    {
        Test obj1(5, 5);
        obj1.setX(10).setY(20);
        obj1.print();
        return 0;
    }
    
    Solution
    Yes; When a reference to a non-local object is returned, the returned reference can be used to chain function calls on a single object. Here all calls modify the same object as the same object is returned by reference.

  14. What is the output of the following C++ program?

    #include <iostream>
    
    class A
    {
      public:
        A(int n = 0) : m(n) { std::cout << 'd'; }
        A(const A& a) : m(a.m) { std::cout << 'c'; }
    
      private:
        int m;
    };
    
    void f(const A& a1, const A& a2 = A()) { }
    
    int main()
    {
        A a(2), b;
        const A c(a), &d = c, e = b;
        b = d;
        A* p = new A(c), *q = &a;
        static_cast < void >(q);
        delete p;
        f(3);
        std::cout << std::endl;
    
        return 0;
    }
    
    Solution
    ddcccdd