Exercise

1.What is the output of this program?
Note:Includes all required header files

using namespace std;
class Base
{
public:
Base(){}
~Base(){}
protected:
private:
};
class Derived:public Base
{
public:
Derived(){}
Derived(){}
private:
protected:
};
int main()
{
cout << "Executed" << endl;
}

A. Executed
B. Error
C. Runtime error
D. None of the mentioned

View Answer


2.What is the output of this program?
Note:Includes all required header files

using namespace std;
int main()
{
string s = "a long string";
s.insert(s.size() / 2, " * ");
cout << s << endl;
return 0;
}

A. long* string
B. a long st*ring
C. Depends on compiler
D. None of the mentione

View Answer


3.What is the output of this program?
Note:Includes all required header files

using namespace std;
struct a
{
int p;
};
struct b
{
int* x;
};
struct c : public a, public b
{
};
int main()
{
c* p = new c;
p->x = 0;
cout << "Inherited";
return 0;
}

A. Inherited
B. Error
C. Runtime error
D. None of the mentioned

View Answer


4.What is the output of this program?
Note:Includes all required header files

using namespace std;
class Base1 {
public:
Base1()
{ cout << " Base1" << endl; }
};
class Base2 {
public:
Base2()
{ cout << "Base2" << endl; }
};
class Derived: public Base1, public Base2 {
public:
Derived()
{ cout << "Derived" << endl; }
};
int main()
{
Derived d;
return 0;
}

A. Compiler Dependent
B. Base1
    Base2
    Derived
C. Base2′s constructor called
    Base1
    Derived
D. Compiler Error

View Answer


5.Which of the following is true about the following program
Note:Includes all required header files

using namespace std;
class Base1 {
public:
~Base1() { cout << " Base1" << endl; }
};
class Base2 {
public:
~Base2() { cout << " Base2" << endl; }
};
class Derived: public Base1, public Base2 {
public:
~Derived() { cout << " Derived" << endl; }
};
int main()
{
Derived d;
return 0;
}

A. Base1
    Base2
    Derived
B. Derived
    Base2
    Base1
C. Derived
D. Compiler Dependent

View Answer


6.Assume that an integer takes 2 bytes and there is no alignment in following classes, predict the output.
Note:Includes all required header files

using namespace std;
class base {
int arr[15];
};
class b1: public base { };
class b2: public base { };
class derived: public b1, public b2 {};
int main(void)
{
cout << sizeof(derived);
return 0;
}

A. 30
B. 60
C. 0
D. 2

View Answer


7.What will be the output of this program?
Note:Includes all required header files

using namespace std;
class Base {};
class Derived: public Base {};
int main()
{
Base *p = new Derived;
Derived *q = new Base;

A. Compiler Error in line "Base *p = new Derived;"
B. No Compiler Error
C. Compiler Error in line " Derived *q = new Base;"
D. Runtime Error

View Answer


8. What will be the output of the following program?
Note:Includes all required header files

using namespace std;
class Base
{
public:
int find() { cout << "Base::find() called"; }
int find(int i) { cout << "Base::find(int i) called"; }
};
class Derived: public Base
{
public:
int find() { cout << "Derived::find() called"; }
};

int main()
{
Derived d;
d.find(5);
return 0;
}

A. Base::find(int i) called
B. Derived::find() called
C. Base::find() called
D. Compiler Error

View Answer


9. What will be the output of the following program?
Note:Includes all required header files

class find {
public:
void print() { cout <<" In find"; }
};
class course : public find {
public:
void print() { cout <<" In course"; }
};
class tech: public course { };
int main(void)

{
tech t;
t.print();
return 0;
}

A. In find
B. In course
C. Compiler Error: Ambiguous call to print()
D. None of the above

View Answer



Discussion

* You must be logged in to add comment.