UNIT II- Operator Overloading and Inheritance
One mark questions:
1) ________ are those that have been redefined within a C++ class.
a) Method Overloading b) Operator Overloading c) Both a & b d) None
2) The internal call to an object ss++ will be made as _______
a) ss. operator++( ) b) ss. operator++(0) c) ss. operator++(1) d) None
3) _______ is a special pointer that points to the current object.
a) This b) new c) Both d) None
4) To prevent the member wise copy, it is necessary to provide the ______.
a) Copy Constructor b) Constructor c) Operator Constructor d) None
5) ______ provides explicit conversion between datatypes.
a) Conversion b) Casting c) Both d) None
6) Operator int( ) is said to be ___________.
a) Conversion Function b) Casting Function c) Both d) None
7) To convert from basic to Object we use ______ in the class.
a) Conversion Function b) Constructor c) Both d) None
8) To convert from Object to basic we use ______ in the class.
a) Conversion Function b) Constructor c) Both d) None
9) Functions with same name but differing in their signatures is said to be ____.
a) Operator Overloading b) Function Overloading c) Both d) None
10) The process of creating new classes from already existing classes is said to be ____.
a) Inheritance b) Polymorphism c) Exceptions d) None
11) The access specifiers used while inheriting is used for ________.
determining availability of base class members to derived class
determining availability of derived class members to base class
determining availability of base class members to subclass class
determining availability of super class members to derived class
12. A member function in the base class can be used by the objects of the derived class ,this is
said to be __________.
a. Accessibility b. Reusability c. Both a & b d. None
13. ___ member can be accessed by the classes derived from the base class.
a. private b. public c. protected d. None
14. ______ inheritance does not change the specifiers of base class members.
a. private b. public c. protected d. None
15. ______ inheritance changes the specifiers of base class members to private of derived class.
a. private b. public c. protected d. None
16. ______ inheritance changes the specifiers of base class members to protected of derived class.
a. private b. public c. protected d. None
17. We cannot create an instance of _________ classes
a. virtual b. abstract c. protected d. None
18. A class derived from more than one class is said to be _______.
a. Multilevel b. Hierarchical c. multiple d. Both b & c
19.Inheritance is ________ relationship.
a. kind- of b. has a c. Both d. None
20.containership is ________ relationship.
a. kind- of b. has a c. Both d. None
Two mark Questions
1. Which of the following operators cannot be overloaded:
i) ++ ii) :: iii) ?: iv) . v) + vi) >>
a. ii, iii, iv b. ii, iii, iv, vi c. All d. None
2. When converting class to class we need _____ in destination class and ______ in source class
a. conversion function, constructor b. constructor, conversion function
c. nothing, conversion function d. constructor, nothing
3. Which of the following are the precautions while overloading:
i)use similar syntax
ii) Avoid ambiguity
iii) Not all operators can be overloaded
a. I only b. ii , iii c. All d. None
4. Which of the following is advantages of Inheritance:
i) Code reusability
ii) The protection is decreased
iii) We can add data and functions to class without affecting the base class.
a. I, iii b. ii, iii c. All d. None
5. The accessibility can be done by :
i) calling the base class functions in derived class
ii) substituting base class constructors
iii) substituting base class member functions
iv) Hiding the base class members
a. I,ii,iii b. ii, iii c. All d. None
6. Which of the following statements are true for method overriding:
i)Functions must have same name
ii) Functions may be in same class
iii) Functions must have same parameters
iv) Functions must be in base and derived class
a. I, iii, iv b. I, iii c. I,iv d. All
7. Which of the following statements are true for method overloading:
i)Functions must have same name
ii) Functions may be in same class
iii) Functions must have same parameters
iv) Functions must be in base and derived class
a. I, iii, iv b. I, ii c. I,iv d. All
8. Which of the following are true to prevent objects of the derived class calling the base class
function.
i) Make a dummy function in derived class of the same name and structure and give an error
message whenever called
ii) Make the derived class private
a. only I b. only ii c. Both I,ii d. None
9. Which of the following are true about abstract classes:
i) We can create an instance of this class.
ii) We cannot create an instance of this class
iii) They must contain atleast one virtual function
iv) They must contain atleast one pure virtual function
v) They are used only for deriving other classes
vi) The derived class must override all the pure virtual functions
a. ii, iv v, vi b. ii, iii, iv, v, vi c. All d. I, iii,iv,v
Four mark Questions
1. Explain the O/P
class A
{
int a;
public:
A(int h=0) { a= h;}
Void show( ) { cout<<”\n a=”< Void operator++( ){ ++a; cout<<”\n ++ no arg called”;}
Void operator++( int g){ ++a; cout<<”\n ++ one arg called”;}
};
void main( )
{
A ss(100);
++ss;
getch();
}
a. ++ no arg called b. ++ one arg called c. Both a & b d. None
2. Explain the O/P
class A
{
int a;
public:
A(int h=0) { a= h;}
Void show( ) { cout<<”\n a=”< Void operator++( ){ ++a; cout<<”\n ++ no arg called”;}
Void operator++( int g){ ++a; cout<<”\n ++ one arg called”;}
};
void main( )
{
A ss(100);
ss++;
getch();
}
a. ++ no arg called b. ++ one arg called c. Both a & b d. None
3. When is the copy constructor called in the following prog:-
class string
{
char ss;
public:
string(char *s=NULL){ strcpy(ss,s);}
string(string &s){ ss=strdup(s.ss); cout<<”\n copy called”;}
void disp( ) { cout<<”\n ss=”<
{
ss=strdup(s.ss); cout<<”\n assignment called”;
return (*this);
}
};
void main( )
{
1. string ss1(“ACME”),ss2;
2. ss2=ss1;
3. string ss3(ss2);
4. string ss4=ss1;
}
a. at line nos., 3,4 b. at line nos., 2,3,4 c. at line no., 3 only d. line no. 2
4. When is the assignment operator method called in the following prog:-
class string
{
char *ss;
public:
string(char *s=NULL){ strcpy(ss,s);}
string(string &s){ ss=strdup(s.ss); cout<<”\n copy called”;}
void disp( ) { cout<<”\n ss=”<
{
ss=strdup(s.ss); cout<<”\n assignment called”;
return (*this);
}
};
void main( )
{
1. string ss1(“ACME”),ss2;
2. ss2=ss1;
3. string ss3(ss2);
4. string ss4=ss1;
}
a. at line nos., 3,4 b. at line nos., 2,3,4 c. at line no., 2,4 d. line no. 2
5. What is the output:-
const int MAX= 10;
class stack
{
protected: int st[MAX},top;
public:
stack() { top= -1; }
void push(int var)( st[++top]=var; }
int pop( ) { return st[top--]; }
};
class stack2:public stack
{
public:
void push(int var)(
if(top+1
else
{cout<<”\n Error: stack is full”; exit(1);}
}
int pop( ) {
if(top>=0) return stack::pop( );
else
{cout<<”\n Error : stack empty”; exit(1); }
}
};
void main( )
{
stack2 ss1;
ss1.push(10);
ss1.push(11);
ss1.push(12);
cout<<”\n”<
a. Error b. 10 11 12 error:stack is empty c. None d. 10 11 12
6. What is the o/p:
class A
{
public:
void show(){
cout<<”\n class A”;}
};
class B
{ public: void show(){
cout<<”\n class A”;}
};
class C: public A, public B { };
void main( )
{
C ss;
ss.show();
}
a. class A b. class B c. Error d. None
7. What is the o/p:
class A
{
public:
void show(){
cout<<”\n class A”;}
};
class B
{ public: void show(){
cout<<”\n class A”;}
};
class C: public A, public B { };
void main( )
{
C ss;
ss.A::show();
ss.B::show();
}
a. class A b. class B c. Error d. Both a & b
8. What is the O/p:
class string
{
char *ss;
public:
string(char *s=NULL){ strcpy(ss,s);}
~string( ){ delete ss;}
int operator==(string &s)
{
int a;
if(strcmp(ss,s.ss)==0)
return 1;
else
return 0;
}
void disp(){ cout<
void main()
{
string s1(“ACME”),s2(“ACME”);
clrscr();
s1.disp();
s2.disp();
if(s1==s2)
cout<<”\n both are equal”;
else
cout<<”\n both are not equal”;
}
a. both are equal b. both are not equal c. Error d. None
9. What is the o/p:
class A
{
int a;
public: A(int h=0){ a=h;}
void show(){ cout<<”\n a=”< };
class B: public A
{
int b;
public: B(int h=0){ b=h;}
void show(){ cout<<”\n b=”<};
void main()
{
B ss(20);
ss.show();
}
a) a= 20 b) b=20 c) Both a & b d. None
10.What is the O/p
class A
{
int a;
public: A(){ a=0;}
A(int h){ a=h;}
void show_a(){ cout<<”\n a=”< };
class B: public A
{
int b;
public: B(){ b=0;}
B(int h){ b=h;}
void show_b(){ cout<<”\n b=”< };
void main()
{
B ss,ss2(10);
ss.show_a();
ss.show_b();
ss2.show_a();
ss2.show_b();
}
a) a=0 b=0 a= 0 b= 10 b) a=0 b=0 a=10 b= 10 c) Error d. None
11.In the below program Whish statement must be used instead of ? do that the
O/P is a=10 b=10
class A
{
int a;
public: A(){ a=0;}
A(int h){ a=h;}
void show_a(){ cout<<”\n a=”< };
class B: public A
{
int b;
public: B(){ b=0;}
? { b=h;}
void show_b(){ cout<<”\n b=”< };
void main()
{
B ss2(10);
ss2.show_a();
ss2.show_b();
}
i. B(int h) ii. B(int h):A(h+2) iii. B(int h):A(a) iv B(int h):A(h) v)B(int h):A(1)
a. Only I b. ii, iv, v c. ii, iii, iv, v d. iv, ii
12. What is the order of constructors called in the below program:
class A
{
public:
A() { cout<<”\n constructor A called”; }
~A() { cout<<”\n Destructor A called”; }
};
class B: public A
{
public:
B() { cout<<”\n constructor B called”; }
~B() { cout<<”\n Destructor B called”; }
};
class C: public A
{
public:
C() { cout<<”\n constructor C called”; }
~C() { cout<<”\n Destructor C called”; }
};
class D: public B, public C
{
public:
D() { cout<<”\n constructor D called”; }
~D() { cout<<”\n Destructor D called”; }
};
void main()
{ D ss; }
i) constructor C called
ii) constructor A called
iii) constructor D called
iv) constructor B called
a. ii, iv, I, iii b. ii, iv, ii, I, iii c. iii, I, iv, ii d. iii, I, ii, iv, ii
13. What is the order of destructors called in the below program:
class A
{
public:
A() { cout<<”\n constructor A called”; }
~A() { cout<<”\n Destructor A called”; }
};
class B: public A
{
public:
B() { cout<<”\n constructor B called”; }
~B() { cout<<”\n Destructor B called”; }
};
class C: public A
{
public:
C() { cout<<”\n constructor C called”; }
~C() { cout<<”\n Destructor C called”; }
};
class D: public B, public C
{
public:
D() { cout<<”\n constructor D called”; }
~D() { cout<<”\n Destructor D called”; }
};
void main()
{ D ss; }
i) Destructor C called
ii) Destructor A called
iii) Destructor D called
iv) Destructor B called
a. ii, iv, I, iii b. ii, iv, ii, I, iii c. iii, I, iv, ii d. iii, I, ii, iv, ii
Answers
One mark
1. b 11.a
2. b 12.a
3. a 13.c .
4.a 14.b
5.b 15.a
6.a 16.c
7.b 17.b
8.a 18.c
9.b 19.a
10.a 20.b
two marks
1. a
2. b
3. c
4.a
5.b
6.a
7.b
8.c
9.a
four marks
1. a 11.b
2. b 12.b
3. a 13.d
4.d
5.b
6.c
7.d
8.a
9.b
10.a