kiransrinivas.files.wordpress.com · web view#include #include usingnamespacestd; classbase...

26
#include <iostream> #include<string> usingnamespacestd; classBase { public: virtualstring print() const { return"This is Base class"; } }; classDerived : publicBase { public: virtualstring print() const { return"This is Derived class"; } }; voiddescribe(Base p) { cout << p.print() << endl; } intmain() { Base b; Derived d; describe(b); describe(d); return0; } Run on IDE (A) This is Derived class This is Base class (B) This is Base class This is Derived class

Upload: vokien

Post on 25-May-2018

224 views

Category:

Documents


0 download

TRANSCRIPT

#include <iostream>#include<string>usingnamespacestd;

classBase{public:    virtualstring print() const    {        return"This is Base class";    }};

classDerived : publicBase{public:    virtualstring print() const    {        return"This is Derived class";    }};

voiddescribe(Base p){    cout << p.print() << endl;}

intmain(){    Base b;    Derived d;    describe(b);    describe(d);    return0;}Run on IDE(A)

This is Derived class

This is Base class

(B)

This is Base class

This is Derived class

(C)

This is Base class

This is Base class

(D) Compiler Error

#include<iostream>usingnamespacestd;

classBase{public:    intx, y;public:    Base(inti, intj){ x = i; y = j; }};

classDerived : publicBase{public:    Derived(inti, intj):x(i), y(j) {}    voidprint() {cout << x <<" "<< y; }};

intmain(void){    Derived q(10, 10);    q.print();    return0;}Run on IDE(A) 10 10(B) Compiler Error(C) 0 0

#include<iostream>usingnamespacestd;

classBase{protected:    inta;public:    Base() {a = 0;}};

classDerived1:  publicBase{public:    intc;};

classDerived2:  publicBase{public:    intc;};

classDerivedDerived: publicDerived1, publicDerived2{public:    voidshow()  {   cout << a;  }};

intmain(void){    DerivedDerived d;    d.show();    return0;}Run on IDE(A)Compiler Error in Line “cout << a;"(B)0(C) Compiler Error in Line “class DerivedDerived: public Derived1, public Derived2”

#include<iostream>usingnamespacestd; classBase1{public:    charc;}; classBase2{public:    intc;}; classDerived: publicBase1, publicBase2{public:    voidshow()  { cout << c; }}; intmain(void){    Derived d;    d.show();    return0;}

(A) Compiler Error in “cout << c;"(B) Garbage Value(C) Compiler Error in “class Derived: public Base1, public Base2”

#include<iostream>usingnamespacestd;

classBase{public:    virtualvoidshow() { cout<<" In Base \n"; }};

classDerived: publicBase{public:    voidshow() { cout<<"In Derived \n"; }};

intmain(void){    Base *bp = newDerived;    bp->Base::show();  // Note the use of scope resolution here    return0;}Run on ID(AinBase(B)InDerived(C)CompilerError(D)RuntimeError

#include<iostream>usingnamespacestd;

intmain(){  intx = 10;  int& ref= x;  ref= 20;  cout << "x = "<< x << endl ;  x = 30;  cout << "ref = "<< ref<< endl;  return0;}Run on IDE(A)

x = 20

ref = 30

(B)

x = 20

ref = 20

(C)

x = 10

ref = 30

(D)

x = 30

ref = 30

What is the output of this program?

1. #include <iostream>2. usingnamespace std;3. class Base4. {5. public:6. virtualvoid print()const=0;7. };8. class DerivedOne :public Base9. {10. public:11. void print()const12. {13. cout<<"DerivedOne\n";14. }15. };16. class DerivedTwo :public Base17. {18. public:19. void print()const20. {21. cout<<"DerivedTwo\n";

22. }23. };24. class Multiple :public DerivedOne, public DerivedTwo25. {26. public:27. void print()const28. {29. DerivedTwo ::print();30. }31. };32. int main()33. {34. int i;35. Multiple both;36. DerivedOne one;37. DerivedTwo two;38. Base *array[3];39. array[0]=&both;40. array[1]=&one;41. array[2]=&two;42. array[ i ]-> print();43. return0;44. }

a)DerivedOne

b)DerivedTwo

c)Error

d) None of the mentioned

1. #include <iostream>2. usingnamespace std;3. struct a4. {5. int count;6. };7. struct b8. {9. int* value;10. };11. struct c :public a, public b12. {13. };14. int main()

15. {16. c* p =new c;17. p->value =0;18. cout<<"Inherited";19. return0;20. }

a)Inherited

b)Error

c)Runtimeerror

d)Noneofthementioned

Which of the followings are true about constructors?

1. A class can have more than one constructor.2. They can be inherited.3. Their address can be referred.4. Constructors cannot be declared in protected section of the class.5. Constructors cannot return values. 

How many types of constructor are there in C++?

A. 1

B. 2

C. 3

D. 4

Questions:1....what is the output of following program......

#include <iostream>

using namespace std;

struct Person

{

char name[50];

int age;

float salary;

};

int main()

{

Person p1;

cout << "Enter Full name: ";

cin.get(p1.name, 50);

cout << "Enter age: ";

cin >> p1.age;

cout << "Enter salary: ";

cin >> p1.salary;

cout << "\nDisplaying Information." << endl;

cout << "Name: " << p1.name << endl;

cout <<"Age: " << p1.age << endl;

cout << "Salary: " << p1.salary;

return 0;

}

questions 2: what is the output of this program...using functions and returns statement...

#include <iostream>

using namespace std;

struct Person {

char name[50];

int age;

float salary;

};

Person getData(Person);

void displayData(Person);

int main()

{

Person p;

p = getData(p);

displayData(p);

return 0;

}

Person getData(Person p) {

cout << "Enter Full name: ";

cin.get(p.name, 50);

cout << "Enter age: ";

cin >> p.age;

cout << "Enter salary: ";

cin >> p.salary;

return p;

}

void displayData(Person p)

{

cout << "\nDisplaying Information." << endl;

cout << "Name: " << p.name << endl;

cout <<"Age: " << p.age << endl;

cout << "Salary: " << p.salary;

}

queitons 3: what will be the ans...

#include <iostream>

using namespace std;

class A

{

public:

void display()

{

cout<<"Base class content.";

}

};

class B : public A

{

};

class C : public B

{

};

int main()

{

C obj;

obj.display();

return 0;

}

question 4:

#include <iostream>

struct V {

virtual void f() {}; // must be polymorphic to use runtime-checked dynamic_cast

};

struct A : virtual V {};

struct B : virtual V {

B(V* v, A* a) {

// casts during construction (see the call in the constructor of D below)

dynamic_cast<B*>(v); // well-defined: v of type V*, V base of B, results in B*

dynamic_cast<B*>(a); // undefined behavior: a has type A*, A not a base of B

}

};

struct D : A, B {

D() : B((A*)this, this) { }

};

struct Base {

virtual ~Base() {}

};

struct Derived: Base {

virtual void name() {}

};

int main()

{

D d; // the most derived object

A& a = d; // upcast, dynamic_cast may be used, but unnecessary

D& new_d = dynamic_cast<D&>(a); // downcast

B& new_b = dynamic_cast<B&>(a); // sidecast

Base* b1 = new Base;

if(Derived* d = dynamic_cast<Derived*>(b1))

{

std::cout << "downcast from b1 to d successful\n";

d->name(); // safe to call

}

Base* b2 = new Derived;

if(Derived* d = dynamic_cast<Derived*>(b2))

{

std::cout << "downcast from b2 to d successful\n";

d->name(); // safe to call

}

delete b1;

delete b2;

}

1.Which is more effective while calling the functions?

a) call by value

b) call by reference

c) call by pointer

d) none of the mentioned

2) constructor

When are the Global objects destroyed?

A. When the control comes out of the block in which they are being used.

B. When the program terminates.

C. When the control comes out of the function in which they are being used.

D. As soon as local objects die.

3)

Which of the following statement is correct?

A. Destructor destroys only integer data members of the object.

B. Destructor destroys only float data members of the object.

C. Destructor destroys only pointer data members of the object.

D. Destructor destroys the complete object.

4)

What is the output of this program?

#include <iostream>

using namespace std;

void func(int x)

{

cout << x ;

}

int main()

{

void (*n)(int);

n = &func;

(*n)( 2 );

n( 2 );

return 0;

}

a) 2

b) 20

c) 21

d) 22

ans-Answer:d

Explanation:As we are calling the function two times with the same value, So it is printing as 22.

5)

What will happen when the function call operator is overloaded?

a) It will not modify the functions.

b) It will modify the functions.

c) It will modify the object.

d) It will modify the operator to be interpreted.

Explanation:It will modifies how the operator is to be interpreted when applied to objects of a given type.

6)

If the programmer does not explicitly provide a destructor, then which of the following creates an empty destructor?

A. Preprocessor

B. Compiler

C. Linker

D. main() function

ans-B

7)

#include<iostream>

using namespace std;

class Point

{

private:

int x, y;

public:

// Parameterized Constructor

Point(int x1, int y1)

{

x = x1;

y = y1;

}

int getX()

{

return x;

}

int getY()

{

return y;

}

};

int main()

{

// Constructor called

Point p1(10, 15);

// Access values assigned by constructor

cout << "p1.x = " << p1.getX() << ", p1.y = " << p1.getY();

return 0;

}

ans-p1.x = 10, p1.y = 15

8)

Which of the following statement is correct whenever an object goes out of scope?

A. The default constructor of the object is called.

B. The parameterized destructor is called.

C. The default destructor of the object is called.

D. None of the above.

1.

What happens when we try to compile the class definition in following code snippet?

class Birds {};

class Peacock : protected Birds {};

A. It will not compile because class body of Birds is not defined.

B. It will not compile because class body of Peacock is not defined.

C. It will not compile because a class cannot be protectedly inherited from other class.

D.. It will compile succesfully.

2.

#include<iostream>

using namespace std;

class Base

{

public:

virtual void show() { cout<<" In Base \n"; }

};

class Derived: public Base

{

public:

void show() { cout<<"In Derived \n"; }

};

int main(void)

{

Base *bp = new Derived;

bp->show();

Base &br = *bp;

br.show();

return 0;

}

A

In Base

In Base

B

In Base

In Derived

C

In Derived

In Derived

D

In Derived

In Base

3.

Which of the following statements is correct when a class is inherited publicly?

A. Public members of the base class become protected members of derived class.

B. Public members of the base class become private members of derived class.

C. Private members of the base class become protected members of derived class.

D.. Public members of the base class become public members of derived class.

4.

Which of the following access specifies is used in a class definition by default?

A. Protected

B. Public

C.. Private

D. Friend

5.

What does the class definitions in following code represent?

class Bike

{

Engine objEng;

};

class Engine

{

float CC;

};

A. kind of relationship

B.. has a relationship

C. Inheritance

D. Both A and B

6.

Which of the following statements is correct about the program given below?

class Bix

{

public:

static void MyFunction();

};

int main()

{

void(*ptr)() = &Bix::MyFunction;

return 0;

}

A. The program reports an error as pointer to member function cannot be defined outside the definition of class.

B. The program reports an error as pointer to static member function cannot be defined.

C. The program reports an error as pointer to member function cannot be defined without object.

D.. The program reports linker error.

7.

Destructor has the same name as the constructor and it is preceded by ______ .

A. !

B. ?

C.. ~

D. $

8.

Which of the following is not a type of constructor?

A. Copy constructor

B. Friend constructor

C. Default constructor

D. Parameterized constructor