Operators and enumerated types CPPE2 M4 Test

  1. What is the output of the following snippet?

    #include <iostream>
    using namespace std;
    
    class N {
    public:
        float x;
        N() { x = 0.0; }
        N(float a) {x = a; }
        N(N &n) { x = n.x; }
        N &operator<<(N &y) { return *new N(x * 10); }
    };
    
    int main()
    {
        N a(2.0),b(4.0);
        N c = a << 1;
        cout << c.x;
        return 0;
    }
    • 0
    • Compilation fails
    • 2
    • 1
  2. What is the output of the following snippet?

    #include <iostream>
    #include <string>
    using namespace std;
    
    class N {
    public:
        float x;
        N() { x = 0.0; }
        N(float a) {x = a; }
        N(N &n) { x = n.x; }
        string operator==(N &n) { if(this != &n) return "true"; else return "false"; }
    };
    
    int main()
    {
        N a(1.1), *b = &a;
        cout << (a == *b);
        return 0;
    }  
    • Compilation fails
    • It prints an empty string
    • false
    • true
  3. What is the output of the following snippet?

    #include <iostream>
    using namespace std;
    class Int {
    public:
        int v;
        Int(int a) { v = a; }
        Int &operator--() {
           ++v;
           return *this;
        }
        Int &operator--(int v) {
           v+=2;
           return *this;
        }
    };
    
    ostream &operator <<(ostream &o, Int &a) {
           return o << a.v++;
    }
    int main()
    {
        Int i = 0;
        cout << --i << i--;
        return 0;
    }
    • 12
    • 21
    • 10
    • 23
  4. What is the output of the following snippet?

    #include <iostream>
    using namespace std;
    class Int {
    public:
        int v;
        Int(int a) { v = a; }
    };
    
    int main()
    {
        Int i = 1;
        cout << i;
        return 0;
    }
    • 2
    • 0
    • 1
    • Compilation fails
  5. What is the output of the following snippet?

    #include <iostream>
    using namespace std;
    
    enum T { A =2, B = -1, C };
    
    T operator+(T t, int i) {
        switch(t) {
            case A: return T(0);
            case B: return static_cast<T>(2);
        default:return (T)1;
        }
    }
    
    int main()
    {
        T i = A + 2;
        cout << i;
        return 0;
    }
    • 1
    • 0
    • Compilation fails
    • 2
  6. What is the output of the following snippet?

    #include <iostream>
    using namespace std;
    
    class Int {
    public:
        int v;
        Int(int a) { v = a; }
    };
    
    ostream &operator <<(ostream &o, Int &a) {
        return o << --a.v;
    }
    
    int main()
    {
        Int i = 1;
        cout << i;
        return 0;
    }
    • Compilation fails
    • 2
    • 1
    • 0
  7. What is the output of the following snippet?

    #include <iostream>
    using namespace std;
    
    class N {
    public:
        float x;
        N() { x = 0.0; }
        N(float a) {x = a; }
        N(N &n) { x = n.x; }
        N &operator%(N &y) { return *new N((int)x % (int)y.x); }
    };
    
    int main()
    {
        N a(2.0),b(4.0);
        N c = a % b;
        cout << c.x;
        return 0;
    }
    • Compilation fails
    • 1
    • 2
    • 0
  8. What is the output of the following snippet?

    #include <iostream>
    using namespace std;
    
    string operator>(float l, float r) { 
       if(int(l) > int(r)) 
         return "true";
       else return "false"; 
    }
    int main()
    {
        float l =2.0, r =2.9999;
        cout << (l > r);
        return 0;
    }
    • false
    • It prints an empty string
    • true
    • Compilation fails
  9. What is the output of the following snippet?

    #include <iostream>
    using namespace std;
    
    class Int {
    public:
        int v;
        Int(int a) { v = a; }
        Int &operator--() {
           ++v;
           return *this;
        }
    };
    
    ostream &operator <<(ostream &o, Int &a) {
        return o << --a.v;
    }
    int main()
    {
        Int i = 2;
        cout << i;
        return 0;
    }
    • 0
    • 1
    • Compilation fails
    • 2
  10. What is the output of the following snippet?

    #include <iostream>
    using namespace std;
    class Int {
    public:
        int v;
        Int(int a) { v = a; }
        Int &operator[](int x) {
           v+=x;
        }
    };
    
    ostream &operator <<(ostream &o, Int &a) {
        return o << a.v;
    }
    
    int main()
    {
        Int i = 2;
        cout << i.v << i[2];
        return 0;
    }
    • Compilation fails
    • 33
    • 24
    • 44
  11. What is the output of the following snippet?

    #include <iostream>
    using namespace std;
    
    enum T { A, B, C };
    
    class Int {
    public:
        T v;
        Int(T a) { v = a; }
    };
    
    ostream &operator <<(ostream &o, Int &a) {
        return o << a.v;
    }
    int main()
    {
        Int i = B;
        cout << i;
        return 0;
    }
    • 1
    • 0
    • Compilation fails
    • B
  12. What is the output of the following snippet?

    #include <iostream>
    using namespace std;
    
    class N {
    public:
        string n;
        X(string s) : n(s) {}
        void operator() (X x) {
           cout << x.n;
        }
    };
    
    int main()
    {
          X x("a", y("b");
          x(y);
        y(x);
        return 0;
    }
    • ba
    • ab
    • Compilation fails
    • It prints an empty string
  13. What is the output of the following snippet?

    #include <iostream>
    using namespace std;
    
    class N {
    public:
        float x;
        N() { x = 0.0; }
        N(float a) {x = a; }
        N(N &n) { x = n.x; }
    };
    
    N &operator=(N &y,float f) { return *new N(f); }
    
    int main()
    {
        N a;
        a = 2.0;
        cout << a.x;
        return 0;
    }
    • 2
    • 0
    • 1
    • Compilation fails
  14. What is the output of the following snippet?

    #include <iostream>
    using namespace std;
    
    enum T { A = 2, B = -1, C };
    
    class Int {
    public:
        T v;
        Int(T a) {  v = a; }
        Int & operator++() { v = C; return &this; }
    };
    
    ostream &operator <<(ostream &o, Int &a) { 
        ++a;
    return o << a.v;
    }
    
    int main()
    {
        Int i = B;
        cout << i;
        return 0;
    }
    • 1
    • Compilation fails
    • C
    • 0
  15. What is the output of the following snippet?

    #include <iostream>
    using namespace std;
    class Int {
    public:
        int v;
        Int(int a) { v = a; }
        Int &operator--() {
           ++v;
           ++v;
           return *this;
        }
    };
    
    ostream &operator <<(ostream &o, Int &a) {
        return o << a.v++;
    }
    
    int main()
    {
        Int i = 0;
        cout << --i;
        return 0;
    }
    • 0
    • 2
    • Compilation fails
    • 1
  16. What is the output of the following snippet?

    #include <iostream>
    using namespace std;
    
    class Int {
    public:
        int v;
        Int(int a) { v = a; }
        Int &operator++(int x) {
           v+=2;
           return *this;
        }
    };
    
    ostream &operator <<(ostream &o, Int &a) {
        return o << a.v;
    }
    
    int main()
    {
        Int i = 0;
        cout << i++ << i.v;
        return 0;
    }
    • 22
    • Compilation fails
    • 21
    • 20
  17. What is the output of the following snippet?

    #include <iostream>
    using namespace std;
    
    class N {
    public:
        float x;
        N() { x = 0.0; }
        N(float a) {x = a; }
        N(N &n) { x = n.x; }
        N &operator=(float f) { x = f - 1; return *this; }
    };
    
    int main()
    {
        N a;
        a = 2.0;
        cout << a.x;
        return 0;
    }
    • Compilation fails
    • 2
    • 1
    • 0
  18. What is the output of the following snippet?

    #include <iostream>
    using namespace std;
    class Int {
    public:
        int v;
        Int(int a) { v = a }
    };
    
    ostream &operator <<(Int &a) {
        return cout << a.v;
    }
    
    int main()
    {
        Int i = 1;
        cout << i;
        return 0;
    }
    • 0
    • 2
    • Compilation fails
    • 1
  19. What is the output of the following snippet?

    #include <iostream>
    using namespace std;
    
    enum T { A = 2, B = -1, C };
    
    class Int {
    public:
        T v;
        Int(T a) { v = a; }
        Int & operator++() { v += 2; return *this; }
    };
    
    ostream &operator <<(ostream &o, Int &a) {
        ++a;
        return o << a.v;
    }
    
    int main()
    {
       Int i = B;
        cout << i;
        return 0;
    }
    • 0
    • 2
    • Compilation fails
    • 1
  20. What is the output of the following snippet?

    #include <iostream>
    #include <string>
    using namespace std;
    
    class N {
    public:
        float x;
        N() { x = 0.0; }
        N(float a) {x = a; }
        N(N &n) { x = n.x; }
        string operator==(float f) { 
            if(int(x) == int(f)) 
               return "true"; 
            else return "false"; 
        }
    };
    
    int main()
    {
        N a(1.1);
        cout << ( a == 1.9);
        return 0;
    }
    • Compilation fails
    • It prints an empty string
    • true
    • false
Subscribe
Notify of
guest
0 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments