Thursday , April 25 2024
Breaking News

CPA – Programming Essentials in C++ Chapter 2 Module 2 Exam Answers

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() {

    A a; B b;

    a.set(1);

    b.kill(a);

    cout << a.get();

    return 0;

}

  • 0
  • Compilation fails
  • 2
  • 1

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() {

    Y *y = new Z();

    dynamic_cast<X *>(y) -> shout();

    return 0;

}

  • X
  • Compilation fails
  • Y
  • Z

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;

}

  • 222
  • Compilation fails
  • 210
  • 212

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() {

    A a,b = a;

    cout << a.get() << b.get();

    return 0;

}

  • 21
  • 20
  • Compilation fails
  • 12

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() {

    A a;

    Do(a);

    a.inc();

    cout << a.val;

    return 0;

}

  • 1
  • Compilation fails
  • 0
  • 2

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;

}

  • 1
  • Compilation fails
  • 0
  • 2

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;

}

  • Y
  • Compilation fails
  • Z
  • X

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() {

    A a;

    Do(&a);

    cout << a.inc();

    return 0;

}

  • 2
  • 0
  • 1
  • Compilation fails

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;

}

  • Compilation fails
  • 1
  • -1
  • 0

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
  • Compilation fails
  • X
  • Z

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;

}

  • 0
  • -1
  • Compilation fails
  • 1

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;

}

  • 0
  • 1
  • Compilation fails
  • -1

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() {

    A *a = new C();;

    Do(a);

    return 0;

}

  • 220
  • Compilation fails
  • 222
  • 221

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;

}

  • 1
  • 0
  • Compilation fails
  • -1

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;

}

  • Compilation fails
  • 20
  • 21
  • 22

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;

}

  • Compilation fails
  • 13
  • 33
  • 23

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

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;

}

  • -1
  • 0
  • 1
  • Compilation fails

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
  • Compilation fails
  • 1
  • 3

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
  • Compilation fails
  • Z
  • Y

About Clear My Certification

Check Also

CPA – Programming Essentials in C++ Final Exam answers :

CPA – Programming Essentials in C++ Final Exam answers : What is the output of …

Leave a Reply

Your email address will not be published. Required fields are marked *