CPA Chapter 6 Assessment Answers 100%

CPA Chapter 6 Assessment Answers 100%

  1. What happens when you attempt to compile and run the following code?

    #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;
    }

    •  It prints -1
    • It prints 0
    • It prints 1
    • Compilation fails
  2. What happens when you attempt to compile and run the following code?

    #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;
    }

    • It prints -1
    • It prints 0
    • Compilation fails
    • It prints 1
  3. What happens when you attempt to compile and run the following code?

    #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;
    }

    • It prints -1
    • Compilation fails
    • It prints 1
    • It prints 0
  4. What happens when you attempt to compile and run the following code?

    #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;
    }

    • It prints 0
    • It prints 1
    • It prints -1
    • Compilation fails
  5. What happens when you attempt to compile and run the following code?

    #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;
    }

    • It prints 1
    • Compilation fails
    • It prints -1
    • It prints 0
  6. What happens when you attempt to compile and run the following code?

    #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;
    }

    • It prints Y
    • It prints nothing
    • Compilation fails
    • It prints X
  7. What happens when you attempt to compile and run the following code?

    #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;
    }

    • Compilation fails
    • It prints Z
    • It prints Y
    • It prints X
  8. What happens when you attempt to compile and run the following code?

    #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;
    }

    • It prints Y
    • It prints X
    • It prints Z
    • Compilation fails
  9. What happens when you attempt to compile and run the following code?

    #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() {
    Y *y = new Z();
    dynamic_cast<X *>(y) -> shout();
    return 0;
    }

    • Compilation fails
    • It prints Y
    • It prints Z
    • It prints X
  10. What happens when you attempt to compile and run the following code?

    #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
    • It prints Z
    • It prints Y
    • It prints X
  11. What happens when you attempt to compile and run the following code?

    #include <iostream>
    using namespace std;

    class A {
    public:
    A() : val(0) {}
    int val;
    void inc() { ++val; }
    };

    void Do(A a) {
    a.inc();
    }

    int main() {
    A a;
    Do(a);
    a.inc();
    cout << a.val;
    return 0;
    }

    • It prints 1
    • It prints 2
    • Compilation fails
    • It prints 0
  12. What happens when you attempt to compile and run the following code?

    #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() {
    A a;
    Do(&a);
    cout << a.inc();
    return 0;
    }

    • It prints 1
    • Compilation fails
    • It prints 2
    • It prints 0
  13. What happens when you attempt to compile and run the following code?

    #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;
    }

    • It prints 210
    • It prints 212
    • It prints 222
    • Compilation fails
  14. What happens when you attempt to compile and run the following code?

    #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() {
    A *a = new C();;
    Do(a);
    return 0;
    }

    • Compilation fails
    • It prints 222
    • It prints 220
    • It prints 221
  15. What happens when you attempt to compile and run the following code?

    #include <iostream>
    using namespace std;

    class A {
    int *val;
    public:
    A() { val = new int; *val = 0; }
    int get() { ++(*val); return *val; }
    };

    int main() {
    A a,b = a;
    cout << a.get()
    cout << b.get();
    return 0;
    }

    • It prints 21
    • Compilation fails
    • It prints 20
    • It prints 22
    • It prints 12
  16. What happens when you attempt to compile and run the following code?

    #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;
    }

    • It prints 21
    • Compilation fails
    • It prints 20
    • It prints 22
  17. What happens when you attempt to compile and run the following code?

    #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;
    }

    • It prints 2
    • Compilation fails
    • It prints 3
    • It prints 1
  18. What happens when you attempt to compile and run the following code?

    #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;
    }

    • Compilation fails
    • It prints 33
    • It prints 23
    • It prints 13
  19. What happens when you attempt to compile and run the following code?

    #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() {
    A a; B b;
    a.set(1);
    b.kill(a);
    cout << a.get();
    return 0;
    }

    • Compilation fails
    • It prints 2
    • It prints 0
    • It prints 1
  20. What happens when you attempt to compile and run the following code?

    #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;
    }

    • It prints 1
    • It prints 2
    • Compilation fails
    • It prints 0