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

Question 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() {
    Z *z = new Z();
    static_cast<Y *>(z) -> shout();
    return 0;
}
 
Answer: Z
 
Question 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;
}
 
Answer: X
 
Question 3: 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;
}
 
Answer: 1
 
Question 4: 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;
}
 
Answer: Compilation fails
 
Question 5: 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;
}
 
Answer: 1
 
Question 6: 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;
}
 
Answer: 222
 
Question 7: 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;
}
 
Answer: Compilation fails
 
Question 8: 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;
}
Answer: 22
 
Question 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() {
    Y *y = new Z();
    dynamic_cast<X *>(y) -> shout();
    return 0;
}
 
Answer: Z
 
Question 10: 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;
}
 
Answer: Compilation fails
 
Question 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 { 
public:
    void shout() { cout << "Y"; }
};
int main() {
    X *x = new Y();
    static_cast<Y *>(x) -> shout();
    return 0;
}
 
Answer: Y
 
Question 12: 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;
}
 
Answer: X
 
Question 13: 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;
}
 
Answer: 2
 
Question 14: 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;
}
 
Answer: 33
 
Question 15: 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;
}
 
Answer: Compilation fails
 
Question 16: 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;
}
 
Answer: 21
 
Question 17: 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;
}
 
Answer: Compilation fails
 
Question 18: 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;
}
 
Answer: -1
 
Question 19: 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;
}
 
Answer: 1
 
Question 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() {
    A *a = new C();;
    Do(a);
    return 0;
}
 
Answer:
220

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.