Object-oriented programming essentials (classes, objects, inheritance) CPPE2 M1 Test

  1. What is the output of the following snippet?

    #include <iostream>
    using namespace std;
    
    class A {
    public:
        A(A *v) { A::v = v; }
        A() { A::v 1.0; }
        float v;
        float set(float v) {
            A::v = v;
            return v;
        }
        float get(float v) {
            return A::v;
       }
    };
    
    int main()
    {
        A a, *b = new A(a);
        cout << a->get(b->set(a->v));
        return 0;
    }
    • Compilation fails
    • 2
    • 4
    • 1
  2. What is the output of the following snippet?

    #include <iostream>
    using namespace std;
    
    class A {
    public:
        A(A &a) { v = a.get(0.0);}
        A(float v) { A::v =v; }
        float v;
        float set(float v) {
           A::v += v;
           return v;
        }
        float get(float v) {
           return A::v + v;
        }
    };
    
    int main()
    {
        A a(0.), b = a;  
        cout << a.get(b.set(1.5));
        return 0;
    }
    • Compilation fails
    • 4.5
    • 2.5
    • 1.5
  3. What is the output of the following snippet?

    #include <iostream>
    using namespace std;
    
    class A {
    public:
        A() { v =2.5; }
        float v;
        float set(float v) {
           A::v += 1.0;
           return v;
        }
        float get(float v) {
           v += A::v;
           return v;
         }
     };
    
    int main()
    {
        A a;
        a.A(); 
        cout << a.get(a.set(1.5));
        return 0;
    }
    • 5
    • 1
    • Compilation fails
    • 3
  4. What is the output of the following snippet?

    #include <iostream>
    using namespace std;
    
    class A {
    public:
        float v;
        A() { v = 1.0; }
        A(A &a) { A::v = a.v; count << "1";}
        ~A() { cout << "0"; }
        float set(float v) {
           A::v = v;
           return v;
        }
        float get(float v) {
           A::v =v;
           return A::v;
        }
    }
    
    int main()
    {
         A a, *b = new A(a), *c = new A(*b);
         c->get(b->get(a.set(1.0)));
         delete b;
         delete c;    
         return 0;
    }
    • Compilation fails
    • 11000
    • 110
    • 1100
  5. What is the output of the following snippet?

    #include <iostream>
    using namespace std;
    
    class A {
       int data[3];
    public:
       int cnt;
       void put(int v) { data[cnt++] = v; }
    };
    int main()
    {
        A a;
        a.cnt = 0;
        a.put(1);
        a.put(1);  
        cout << a.cnt;
        return 0;
    }
    • Compilation fails
    • 0
    • 1 
    • 2
  6. What is the output of the following snippet?

    #include <iostream>
    using namespace std;
    
    class A {
    public:
        float v;
        A() { v = 1.0; }
        A(A &a) { A::v = a.v; cout << "1"; }
        ~A() { cout << "0"; }
       float set(float v) {
           A::v = v;
           return v;
       }
       float get(float v) {
           return A::v;
       }
    };
    
    int main()
    {
        A a, b = a;    
        return 0;
    }
    • 11100
    • 1100
    • 100
    • Compilation fails
  7. What is the output of the following snippet?

    #include <iostream>
    using namespace std;
    
    class A {
    public:
        int cnt;
        void put(int v);
    };
    
    void A::put(int v) { cout << ++cnt; }
    
    int main()
    {
        A a[2];
        a[0].cnt = 0;
        a[0].cnt = 1;
        a[a[0].cnt].put(a[1].cnt);
        return 0;
    }
    • 2
    • Compilation fails
    • 1
    • 0
  8. What is the output of the following snippet?

    #include <iostream>
    #include <string>
    using namespace std;
    
    class A {
    public: string a(string b) {
        return b.substr(0,2);
    }};
    
    class B {
    public: string a(string b) {
        return b.substr(2,2);
    }};
    
    int main()
    {
        A a;
        B b;  
        cout << a.a(b.a("ABCD"));
        return 0;
    }
    • CD
    • ABCD
    • AB
    • Compilation fails
  9. What is the output of the following snippet?

    #include <iostream>
    using namespace std;
    
    class A {
    public:
        A() { v = 2.5; }
        A(float v) { A::v = v + 1.0; }
        float v;
        float set(float v) {
            A::v += 1.0;
            return v;
        }
        float get(float v) {
            v += A::v;
            return v;
        }
    };
    
    int main()
    {
        A a,b(1.0);
        cout << a.get(b.set(1.5));
        return 0;
    }
    • 1
    • Compilation fails
    • 4
    • 2
  10. What is the output of the following snippet?

    #include <iostream>
    using namespace std;
    
    class A {
    public:
        A() { v = 2.5; }
        float v;
        float set(float v) {
            A::v += 1.0;
            return v;
        }
        float get(float v) {
            v += A::v;
            return v;
        }
    };
    int main()
    {
        A a;  
        cout << a.get(a.set(1.5));
        return 0;
    }
    • 5
    • 1
    • 3
    • Compilation fails
  11. What is the output of the following snippet?

    #include <iostream>
    using namespace std;
    
    class A {
    public:
        float v;
        A(float x) : v(x) {}
    };
    
    class B {
    public:
        A a;
        float b;
        B(float x) : a(x + 1) { b = a.v; }
    };
    
    int main()
    {
        B b(2.0);  
        cout << b.b;
        return 0;
    }
    • 3
    • 1
    • Compilation fails
    • 2
  12. What is the output of the following snippet?

    #include <iostream>
    using namespace std;
    
    class A {
    public:
        A(float v) { A::v = v; }
        float v;
        float set(float v) {
            A::v = v;
            return v;
        }
        float get(float v) {
            return A::v;
        } 
    };
    
    int main()
    {
        A *a = new A(1.0), *b = new A(*a);  
        cout << a->get(b->set(a->v));
        return 0;
    }
    • 1
    • Compilation fails
    • 4
    • 2
  13. What is the output of the following snippet?

    #include <iostream>
    using namespace std;
    
    class A {
    public:
        A(A &a) { v = a.get(0.0); }
        A(float v) { A::v + v; }
        float v;
        float set(float v) {
            A::v += v;
            return v;
        }
        float get(float v) {
            return A::v + v;
        }
    };
    
    int main()
    {
        A *a = new A(1.0), *b = new A(*a);
        cout << a->get(b->set(a-v));
        return 0;
    }
    • 1
    • Compilation fails
    • 4
    • 2
  14. What is the output of the following snippet?

    #include <iostream>
    using namespace std;
    
    class A {
        int cnt;
        void put(int v) { cout << cnt++; }
    };
    int main()
    {
         A a;
         a.cnt = 0;
         a.put(1);
         a.put(1);
         return 0;
    }
    • 0
    • 2
    • 1
    • Compilation fails
  15. What is the output of the following snippet?

    #include <iostream>
    using namespace std;
    
    class A {
    public:
        float v;
        float set(float v) {
           v += 1.0;
           this -> v = v;
           return v;
        }
        float get(float d) {
           v += 1.0;
           return v;
        }
    };
    
    int main()
    {
        A a;  
        cout << a.get(a.set(a.set(0.5)));
        return 0;
    }
    • 3.5
    • 1.5
    • Compilation fails
    • 2.5
  16. What is the output of the following snippet?

    #include <iostream>
    using namespace std;
    
    class A {
    public:
       A(float v) { A::v = v; }
       float v;
       float set(float v) {
          A::v += v;
          return v;
       }
       float get(float v) {
          return A::v + v;
       }
    };
    
    int main()
    {
        A a,b(1.0);  
        cout << a.get(b.set(1.5));
        return 0;
    }
    • 4
    • Compilation fails
    • 2
    • 1
  17. What is the output of the following snippet?

    #include <iostream>
    using namespace std;
    
    class A {
    public:
       float v;
       float set(float v) {
          A::v += 1.0;
          return v;
       }
       float set(void) {
          A::v = v + 1.0;
          return 0.0;
       }
       float get(float v) {
          v += A::v;
          return v;
       }
    };
    
    int main()
    {
        A a;  
        cout << a.get(a.set(a.set(a.set())));
        return 0;
    }
    • 2
    • Compilation fails
    • 3
    • 1
  18. What is the output of the following snippet?

    #include <iostream>
    using namespace std;
    
    class A {
    public:
        float v;
        float set(float v) {
            A::v += 1.0;
            A::v = v + 1.0;
            return v;
        }
        float get(float v) {
            v += A::v;
            return v;
       }
    };
    
    int main()
    {
        A a;
        cout << a.get(a.set(a.set(0.5)));
        return 0;
    }
    • Compilation fails
    • 1.5
    • 2.5
    • 2
  19. What is the output of the following snippet?

    #include <iostream>
    using namespace std;
    
    class A {
    public:
       float v;
       A() : v(1.0) {}
       A(A &a) :v(2.0) {}
       A(float f) : v(3.0) {}
       float get() {
          return A::v;
       }
    };
    
    int main()
    {
        A a, b(a.get()), c(b);  
        cout << a.v + b.v + c.v;
        return 0;
    }
    • Compilation fails
    • 6
    • 3
    • 9
  20. What is the output of the following snippet?

    #include <iostream>
    using namespace std;
    
    class A{
    public:
        int data[3];
    private:
        int cnt;
    public:
        void put(int v) { data[cnt++] = v; }
        int take() {int c = cnt; cnt = 0; return c;}
    };
    int main()
    {
        A a;
        a.take();
        a.put(a.take());
        a.put(1);
        cout << a.data[0];
        return 0;
    }
    • 0
    • 1
    • Compilation fails
    • 2
Subscribe
Notify of
guest
0 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments