Question 6

What happens when you attempt to compile and run the following code?
#include <iostream>
#include <string>
using namespace std;
class A {
protected:
int y;
public:
int x;
int z;
A() { x=2; y=2; z=3; }
A(int a, int b) : x(a), y(b) { z = x ? y;}
void Print() {
cout << z;
}
};
int main () {
A a(2,5);
a.Print();
return 0;
}
  • Question 7

    Which code, inserted at line 5, generates the output "ABC"?
    #include <iostream>
    using namespace std;
    class A {
    public:
    //insert code here
    };
    class B:public A {
    public:
    void Print(){ cout<< "B"; }
    };
    class C:public B {
    public:
    void Print(){ cout<< "C"; }
    };
    int main()
    {
    A ob1;
    B ob2;
    C ob3;
    A *obj;
    obj = &ob1;
    obj?>Print();
    obj = &ob2;
    obj?>Print();
    obj = &ob3;
    obj?>Print();
    }
  • Question 8

    What happens when you attempt to compile and run the following code?
    #include <iostream>
    #include <string>
    using namespace std;
    class A {
    public:
    int x;
    };
    class B : public A {
    public:
    B() { x=1;}
    B(int x) {this?>x = x;}
    };
    int main () {
    B c1;
    B c2(10);
    cout << c1.x;
    cout << c2.x;
    return 0;
    }
  • Question 9

    What happens when you attempt to compile and run the following code?
    #include <iostream>
    #include <string>
    using namespace std;
    class A {
    int x;
    protected:
    int y;
    public:
    int z;
    A() { x=1; y=2; z=3; }
    };
    class B : public A {
    string z;
    public:
    void set() { y = 4; z = "John"; }
    void Print() { cout << y << A::z; }
    };
    int main () {
    B b;
    b.set();
    b.Print();
    return 0;
    }
  • Question 10

    What happens when you attempt to compile and run the following code?
    #include <iostream>
    using namespace std;
    void fun(int*);
    int main()
    {
    int *x;
    int i=2;
    x=&i;
    fun(x);
    cout<<i;
    return 0;
    }
    void fun(int *i)
    {
    *i = *i * *i;
    }