Inheritance CPPE2 M2 Test

  1. What is the output of the following snippet?

    #include <iostream>
    using namespace std;
    class A {
    public:
        A() : val(0) {}
        int val;
        int inc() { ++val; return val--; }
    };
    
    void Do(A *a) {
        a-> val = a->inc() ;
    }
    
    int main(void) 
    {
       A a;
       Do(&a);
       cout << a.inc();
       return 0;
    }
    • 0
    • 1
    • Compilation fails
    • 2
  2. What is the output of the following snippet?

    #include <iostream>
    using namespace std;
    class A {
    friend class B;
    private:
        int field;
    public:
        int set(int x) { return field = ++x; }
        int get() { return ++field; }
    };
    
    class B {
    public:
        void kill(A &a) { a.field = 0; }
    };
    
    int main(void) 
    {
       A a; B b;
       a.set(1);
       b.kill(a);
       cout << a.get();
       return 0;
    }
    • Compilation fails
    • 1
    • 2
    • 0
  3. What is the output of the following snippet?

    #include <iostream>
    using namespace std;
    class X {
    private:
        int v;
    };
    
    class Y : public X {
       Y() : v(0) {}
    }
    
    int main() 
    {
      Y y;
      cout << y.v;
      return 0;
    }
    • Compilation fails
    • 1
    • 0
    • -1
  4. What is the output of the following snippet?

    #include <iostream>
    using namespace std;
    class X {
    public:
        virtual void shout() { cout << "X"; }
    };
    
    class Y : public X {
    public:
        void shout() { cout << "Y"; }
    };
    
    class Z : public Y {
    public:
       void shout() { cout << "Z"; }
    };
    
    int main() 
    {
       Z *z = new Z();
       static_cast<Y *>(z) -> shout();
       return 0;
    }
    • X
    • Y
    • Z
    • Compilation fails
  5. What is the output of the following snippet?

    #include <iostream>
    using namespace std;
    
    class X {
    public:
        void shout() { cout << "X"; }
    };
    class Y : public X {
    public:
        virtual void shout() { cout << "Y"; }
    };
    
    class Z : public Y {
    public:
        void shout() { cout << "Z"; }
    };
    
    int main() 
    {
       Y *y =  new Z();
       dynamic_cast<X *>(y) -> shout();
       return 0;
    }
    • Compilation fails
    • X
    • Y
    • Z
  6. What is the output of the following snippet?

    #include <iostream>
    using namespace std;
    class A {
    friend void f();
    private:
        int field;
    public:
        int set(int x) { return field = ++x; }
        int get() { return ++field; }
    };
    
    void f(A &a) {a.field /=2;}
    
    int main() 
    {
       A a;
       a.set(2);
       f(a);
       cout << a.get();
       return 0;
    }
    • 2
    • 0
    • Compilation fails
    • 1
  7. What is the output of the following snippet?

    #include <iostream>
    using namespace std;
    class X {
    protected:
       int v;
    };
    
    class Y : protected X {
       Y() : v(0) {}
    }
    int main() 
    {
       Y *y = new Y();
       cout << y->v;
       delete y;
       return 0;
    }
    • -1
    • 1
    • 0
    • Compilation fails
  8. What is the output of the following snippet?

    #include <iostream>
    using namespace std;
    class A {
    public:
        A() : val(0) {}
        int val;
        void inc() { ++val; }
    };
    
    void Do(A a) {
        a.inc();
    }
    
    int main(void) 
    {
       A a;
       Do(a);
       a.inc();
       cout << a.val;
       return 0;
    }
    • 0
    • 2
    • Compilation fails
    • 1
  9. What is the output of the following snippet?

    #include <iostream>
    using namespace std;
    class X {
    public:
        virtual void shout() { cout << "X"; }
    };
    
    class Y : public X {
    public:
       void shout() { cout << "Y"; }
    };
    
    class Z : public Y {
    public:
       void shout() { cout << "Z"; }
    };
    
    int main(void) 
    {
       Y *y = new Z();
       dynamic_cast<X *>(y) -> shout();
       return 0;
    }
    • Z
    • Compilation fails
    • Y
    • X
  10. What is the output of the following snippet?

    #include <iostream>
    using namespace std;
    class A {
        int *val;
    public:
        A() { val = new int; *val = 0; }
        int get() { return ++(*val); }
    };
    
    int main(void) 
    {
       A a,b = a;
       cout << a.get() << b.get();
       return 0;
    }
    • 22
    • 20
    • 12
    • Compilation fails
  11. What is the output of the following snippet?

    #include <iostream>
    using namespace std;
    class X {
    public:
        void shout() { cout << "X"; }
    };
    
    class Y : public X {
    };
    
    class Z : public Y {
    public:
        void shout() { cout << "Z"; }
    };
    
    int main() 
    {
       Z *z = new Z();
       static_cast<Y *>(z) -> shout();
       return 0;
    }
    • Y
    • X
    • Compilation fails
    • Z
  12. What is the output of the following snippet?

    #include <iostream>
    using namespace std;
    class A {
    public:
        A() : val(0) {}
        int val;
        virtual void run() { cout << val; }
    };
    
    class B : public A {
    };
    
    class C : public B {
    public:
       void run() { cout << val + 2; }
    };
    
    void Do(A *a) {
        B *b;
        C *c;
        if(b = dynamic_cast<B *>(a))
           b->run();
        if(c = dynamic_cast<C *>(a))
           c->run();
        a->run();
    }
    
    int main() 
    {
       A *a = new C();;
       Do(a);
       return 0;
    }
    • 210
    • 222
    • Compilation fails
    • 212
  13. What is the output of the following snippet?

    #include <iostream>
    using namespace std;
    class A {
    public:
        int v;
        A(int x) : v(x + 1) {}
        int get() const { return v; }
    };
    
    int main() 
    {
       A a(2);
       A b(a);
       cout << a.get() << b.get();
       return 0;
    }
    • 23
    • 13
    • 33
    • Compilation fails
  14. What is the output of the following snippet?

    #include <iostream>
    using namespace std;
    
    class X { };
    class Y : public X { };
    class Z : public X { };
    
    int main() 
    {
       Z *z = new Z();
       X *x = new X();
       x = z;
       cout << (x == z);
       return 0;
    }
    • 0
    • -1
    • Compilation fails
    • 1
  15. What is the output of the following snippet?

    #include <iostream>
    using namespace std;
    class X {
    public:
        void shout() { cout << "X"; }
    };
    
    class Y : public X {
    public:
        void shout() { cout << "Y"; }
    };
    
    int main() 
    {
       X *x = new Y();
       static_cast<Y *>(x) -> shout();
       return 0;
    }
    • Compilation fails
    • X
    • Y
    • It prints nothing
  16. What is the output of the following snippet?

    #include <iostream>
    using namespace std;
    
    class X { };
    class Y : public X { };
    class Z : public X { };
    int main() 
    {
       Z *z = new Z();
       Y *y = new Y();
       z = y;
       cout << (z == y);
       return 0;
    }
    • 0
    • -1
    • 1
    • Compilation fails
  17. What is the output of the following snippet?

    #include <iostream>
    using namespace std;
    class X {
    public:
       int v;
       void put(int x) { v =x; }
       int get(void) { return v; }
    };
    
    class Y : public X {
    public:
       Y() { put(0); }
       void write(int x) { put(x + 1); }
       int read(void) { return get() - 1; }
    };
    
    int main() 
    {
       Y *y = new Y();
       y ->write(1);
       cout << y->read();
       delete y;
       return 0;
    }
    • 1
    • -1
    • Compilation fails
    • 0
  18. What is the output of the following snippet?

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

    #include <iostream>
    using namespace std;
    class A {
        int *val;
    public:
       A() { val = new int; *val = 0; }
       A(A &a) { val = new int; *val = a.get(); }
       int get() { return ++(*val); }
    };
    
    int main() 
    {
       A a,b = a;
       cout << a.get() << b.get();
       return 0;
    }
    • 20
    • Compilation fails
    • 22
    • 21
  20. What is the output of the following snippet?

    #include <iostream>
    using namespace std;
    class A {
    public:
        A() : val(0) {}
        int val;
        void run() { cout << val; }
    };
    
    class B : public A {
    public:
        virtual void run() { cout << val + 2; }
    };
    
    class C: public B {
    };
    
    void Do(A *a) {
       B *b;
       C *c;
       if(b = static_cast<B *>(a))
          b->run();
       if(c = dynamic_cast<C *>(b))
          c->run();
       a->run();
    }
    int main(void) 
    {
       A *a =new C();;
       Do(a);
       return 0;
    }
    • Compilation fails
    • 220
    • 222
    • 221
Subscribe
Notify of
guest
0 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments