C++ Vita Soln
C++ Vita Soln
Contents
Enhancements .......................................................................................................................................................... 2
Oops ........................................................................................................................................................................ 56
Operator Overloading............................................................................................................................................. 67
Conversion .............................................................................................................................................................. 70
Inheritance.............................................................................................................................................................. 70
Late Binding ............................................................................................................................................................ 84
File Handling ........................................................................................................................................................... 98
Templates ............................................................................................................................................................... 99
Exception .............................................................................................................................................................. 100
1
Vidyanidhi Info Tech Academy
Vidyanidhi Info Tech Academy
PG DAC C++ Question Bank
Enhancements
1) What is the output?
const int a=124;
void main()
{
const int* sample();
int * const p=sample();
cout<<*p;
}
const int* sample()
{ return (&a);
}
a) Warning b) compilation error c) output “124” d) garbage value
2
Vidyanidhi Info Tech Academy
Vidyanidhi Info Tech Academy
PG DAC C++ Question Bank
4) What is the output?
void main()
{
int* getAr();
int *ptr;
ptr=getAr();
cout<<ptr[2]<<endl;
getch();
}
int* getAr()
{
int arr[4]={10,20,30,40};
return arr;
}
a) 20 b) 30 c) it will not compile d) warning
5) In case of command line arguments main accepts following two arguments.
a) int argc,char *argv b) char argv,int argc
c) int argc,char *argv[] d) char *argv,int *argc
3
Vidyanidhi Info Tech Academy
Vidyanidhi Info Tech Academy
PG DAC C++ Question Bank
10) What will happen to following code?
struct emp
{
char name[20];
};
void main()
{
emp e1={"abc"};
emp e2=e1;
cout<<e2.name<<endl;
getch();
}
a) warning b) compiler error “can not initialize e2 with e1”
c) output “abc” d) garbage
13) *p++ ;
a) increments value b) increments address c) Error d) None
4
Vidyanidhi Info Tech Academy
Vidyanidhi Info Tech Academy
PG DAC C++ Question Bank
a=11
printf(“%d%d”,a,b);
a) Results in compile time error b) Results in run time error
c) 1 1 1 1 d) None of the above.
a) 12 b) 25 c) 11 d) None of these
Explanation:
***(arr+1)+5+4
Solve *(arr+1) , this is equivalent to arr[1] i.e. base address of second dd array.
Add one more *, u will get address of first one d array represented by second dd array.
Add one more *, u will get an element of first one d array represented by second dd array i.e. 2
Now
2+5+4
i.e. 11.
27) in case of command line arguments main accepts following two arguments.
a) int argc,char *argv b) char argv,int argc
c) int argc,char *argv[] d) char *argv,int *argc
28) using which macro, we can display the argument from variable number of argument function ?.
a) va_arg b) va_list c) va_show d) va_start
36) If ptr is a pointer to array of objects, then delete ptr and delete [] ptr both are same
a) False b) True
37) Which one of the following is demonstrated by the sample code above?
a) A default function parameter b) A virtual member function
c) A template function d) A member function definition
45) We can not make constant pointer pointing to constant int variable.
a) True b) False
47) Using which macro, we can initialize the list of data in case of variable number of
argument function ?
a) va_arg b) va_list c) va_show d) va_start
54) #include<iostream.h>
void main()
{
char * const t=”hello”;
t=”world”;
}
a)Runtime Error b) Compilation Error c) Neither Compilation or Runtime Error
55) #include<iostream.h>
int& disp()
{
int num=10;
return num;
}
void main()
{
disp()=30;
}
a) Compilation Error b) No Error,No Warning c) Warning
56) #include<iostream.h>
void main()
{
int i=5;
int &j=i;
int p=10;
j=p;
p=20;
cout<<endl<<i<<endl<<j;
}
a) 20,20 b) 10,5 c) 5,10 d) 10,10
12
Vidyanidhi Info Tech Academy
Vidyanidhi Info Tech Academy
PG DAC C++ Question Bank
57) #include<iostream.h>
void main()
{
char *p="Hello";
char *q=p;
q="Good Bye";
cout<<p<<”\t”<<q;
}
a) Hello Good Bye b) Good Bye Good Bye c) Error: Lvalue Reqd.
58) #include<iostream.h>
const int a=124;
void main()
{
const int* sample();
int *p;
p=sample();
}
const int* sample()
{ return (&a);
}
a) Warning b) Neithe Warning nor Error c) Compilation Error
59) #include<iostream.h>
void main()
{
char t[]="String functions are simple";
int len=strlen(t);
cout<<len;
}
a) Compilation Error b) Warning c) successful output
60) #include<iostream.h>
void main()
{
int a=30;
f();
}
void f()
{
int b=30;
}
a) Successful output b) Warning c) Compilation Error
63) C++ compiler internally changes names of all functions at the declaration, definition and call. This process
is known as __________ or __________.
64) True or False. Default arguments can be given in the beginning or in between also.
a) True b) False
68) Which of the following is false about struct and class in C++?
a) he members of a struct are public by default, while in class, they are private by default
b) Struct and class are otherwise functionally equivalent
c) A class supports all the access specifiers like private, protected and public
d) A struct cannot have protected access specifier
16
Vidyanidhi Info Tech Academy
Vidyanidhi Info Tech Academy
PG DAC C++ Question Bank
86) Given
#include<iostream.h>
void disp()
{
int *ptr=new int;
}
void main()
{
disp();
}
In the above code after disp() method is over, the situation becomes
a) Dangling Poiner b) Memory Leak c) None of these
87) Given
#include<iostream.h>
void main()
{
int *ptr=new int;
delete ptr;
//Some other C++ Statements....
}
20
Vidyanidhi Info Tech Academy
Vidyanidhi Info Tech Academy
PG DAC C++ Question Bank
In the above code after “ delete ptr “ statement, the situation becomes
a) Dangling Pointer b) Memory Leak c) None of these
21
Vidyanidhi Info Tech Academy
Vidyanidhi Info Tech Academy
PG DAC C++ Question Bank
cout << f() << endl;
return 0;
}
a) Yes b) no
91) Will the following code compile and link? Give reasons.
#include <iostream>
using namespace std;
int main ()
{
int i = 0;
int &ri(i);
return 0;
}
a) yes b) no
92) Will the following code compile and link? Give reasons.
int main()
{
int i = 0;
int &ri = 0;
return 0;
}
a) Yes b) no
94) When the following two file, a.cpp and b.cpp are compiled, we get linking error.
Why?
Compilation and linking command
cl.exe a.cpp b.cpp
File a.cpp
=======
int f();
int main()
22
Vidyanidhi Info Tech Academy
Vidyanidhi Info Tech Academy
PG DAC C++ Question Bank
{
f();
return 0;
}
File b.cpp
=======
extern "C" int f();
int f()
{
return 0;
}
a) There is no main function inside “b.cpp”
b) Function “f()” is declared but not defined inside “a.cpp”
c) Function “f()” is declared with “extern” inside “b.cpp”
d) None of the above
106) #include<iostream.h>
void main()
{
25
Vidyanidhi Info Tech Academy
Vidyanidhi Info Tech Academy
PG DAC C++ Question Bank
char * const t=”hello”;
t=”world”;
}
a) Runtime Error b) Compilation Error c) Neither Compilation or Runtime Error
107) #include<iostream.h>
int& disp()
{
int num=10;
return num;
}
void main()
{
disp()=30;
}
a) Compilation Error b) No Error,No Warning c) Warning
108) #include<iostream.h>
void main()
{
char *p="Hello";
char *q=p;
q="Good Bye";
cout<<p<<”\t”<<q;
}
a) Hello Good Bye b) Good Bye Good Bye c) Error: Lvalue Reqd.
109) #include<iostream.h>
const int a=124;
void main()
{
const int* sample();
int *p;
p=sample();
}
const int* sample()
{ return (&a);
}
a) Warning b) Neithe Warning nor Error c) Compilation Error
110) #include<iostream>
void main()
{
char t[]="String functions are simple";
int len=strlen(t);
cout<<len;
}
26
Vidyanidhi Info Tech Academy
Vidyanidhi Info Tech Academy
PG DAC C++ Question Bank
a) Compilation Error b) Warning c) successful output
111) #include<iostream.h>
void main()
{
int a=30;
f();
}
void f()
{
int b=30;
}
a) Successful output b) Warning c) Compilation Error
114) C++ compiler internally changes names of all functions at the declaration, definition and call. This
process is known as _______ or _______________
115) True or False. Default arguments can be given in the beginning or in between also.
a) True b) False
27
Vidyanidhi Info Tech Academy
Vidyanidhi Info Tech Academy
PG DAC C++ Question Bank
119) Which of the following is false about struct and class in C++?
a) The members of a struct are public by default, while in class, they are private by default
b) Struct and class are otherwise functionally equivalent
c) A class supports all the access specifiers like private, protected and public
d) A struct cannot have protected access specifier
28
Vidyanidhi Info Tech Academy
Vidyanidhi Info Tech Academy
PG DAC C++ Question Bank
cout<<"Hello";
cout<<"Bye"<<a;
}
a) HiBye10 b) HelloBye10 c) Compilation Error d) HiBye5 e) Bye10
29
Vidyanidhi Info Tech Academy
Vidyanidhi Info Tech Academy
PG DAC C++ Question Bank
30
Vidyanidhi Info Tech Academy
Vidyanidhi Info Tech Academy
PG DAC C++ Question Bank
136) Given
#include<iostream.h>
void disp()
{
int *ptr=new int;
}
void main()
{
disp();
}
In the above code after disp() method is over, the situation becomes
a) Dangling Poiner b) Memory Leak c) None of these
137) Given
#include<iostream.h>
void main()
{
int *ptr=new int;
delete ptr;
//Some other C++ Statements....
}
In the above code after “ delete ptr “ statement, the situation becomes
a) Dangling Pointer b) Memory Leak c) None of these
return 0;
}
a) ri b) i c) Both ri and i d)none
if ( x > y );
cout << ”x is greater than y" < < endl;
return 0;
}
a) x is greater than y b) no output c) compiler error d) none of these
142) What is the output of the following code? Explain the reason.
#include <iostream>
using namespace std;
int main()
{
int i = 10;
int j = 20;
if( pi = pj) {
cout << "Address of pi and pj are same" < < endl;
}
else {
cout << "Address of pi and pj are different" < < endl;
}
return 0;
}
a)address of pi and pj are same c) compiler erro
b)address of pi and pj are different d) none of these
ri = 200;
ri = i;
i = ri;
cout << i << endl;
return 0;
}
a) 100 b) 200 c) 300 d)Compiler error
144) Write code in main function, which will output the value of the global
variable i on the console.
#include <iostream>
using namespace std;
int i = 100;
35
Vidyanidhi Info Tech Academy
Vidyanidhi Info Tech Academy
PG DAC C++ Question Bank
int main()
{
int i = 500;
// Write your code below this comment
return 0;
}
a) cout<<i; b) cout<<::i; c) cout<<&i; d) You can’t print global variable in main
36
Vidyanidhi Info Tech Academy
Vidyanidhi Info Tech Academy
PG DAC C++ Question Bank
148) What is the output of the following code:
#include <iostream>
using namespace std;
int main()
{
int a[3] = {10, 20, -30};
int *p = &a[1];
P--;
cout << *p << endl;
P--;
cout << p[3] << endl;
return 0;
}
a) 10 garbage value b) 10 -30 c) 10 20 d) Runtime error
152) Will the following code compile and link? I‘ not, give reasons for the error.
int main ()
{
int i = (int)10;
return 0;
}
a) Yes b) No
return 0;
}
a) Yes b) No
return 0;
}
a) Yes b) no
157) In the following code, which variable will be created in stack memory?
int i;
int main ()
{
int j;
return 0;
}
a) I b) j c) both I and j d) none
&i;
return 0;
39
Vidyanidhi Info Tech Academy
Vidyanidhi Info Tech Academy
PG DAC C++ Question Bank
}
a) Yes b) no
159) Will the following code compile and link?
#define Begin {
#define End }
int main ()
Begin
return 0;
End
a) Yes b) no
160) What kind of error we will get in the following code? Compilation Error or Linking
Error?
void f();
int main ()
{
f();
return 0; -
}
a)compile time error b)link error c)runtime error d)successful execution
163)In the following code, function f returns a value which is an integer. In the function
main, we are calling function f, but the return value we are not using or storing in
any variable.
Is this acceptable?
int f ()
40
Vidyanidhi Info Tech Academy
Vidyanidhi Info Tech Academy
PG DAC C++ Question Bank
{
return 100;
}
int main ()
}
f();
return 0;
}
a) yes b) no
164) Will the following code give linking error as function f is not defined?
int f( );
int main ( )
{
return 0;
}
a) yes b) no
165) Will the following code compile and link? If yes, what will be the output of the
following program?
#include <iostream>
using namespace std;
#ifdef 0
int main()
{
cout << ”First main called" < < endl;
return 0;
#else
int main()
{
cout << "Second main called" << endl;
return 0;
}
s
#endif
a) compiler error b) linking error c) successful output
41
Vidyanidhi Info Tech Academy
Vidyanidhi Info Tech Academy
PG DAC C++ Question Bank
#else
int main()
{
cout << "Second main called" << endl;
return 0;
}
#endif
a)compiler error b)First main called c)Second main called d)None of the following
return 0;
}
a) 10 11 b)10 10 c)Compiletime error d)None of the above
0 = 0;
return 0;
}
a) nothing wrong b) l-value error
int main ()
{
;
return 0;
}
a) nothing wrong b) u cant have ; without any c++ expression
171) What is wrong in the following code? Will the following code compile and link?
int main ()
{
return 0;
return 1;
}
a) yes b) no
int main()
{
int i = I;
cout<<i<<endl;
return 0;
}
a) 100 b) Garbage c)Compiler error d)None of the following
184) Which of the following swap functions is correct (Swapping 2 int using pass bypointer approach)?
a) void swap(int *x, int *y)
{
int *Z = 0;
*Z * *x;
*X = *y;
*Z * y ;
}
46
Vidyanidhi Info Tech Academy
Vidyanidhi Info Tech Academy
PG DAC C++ Question Bank
int main ()
{
cout << ”main called" << endl;
return 0;
}
a) There is no “using namespace std” b)Iostream.h should have been there
c) #include <cout> is not there d)None of the above
return 0;
}
a) Yes b) no
192) What is wrong in the following code? Will it compile and link?
int main ()
{{
return 0;
}}
a) It will compile but not linked b)It will not compile
c) It will compile, link but fail at runtime. d) It will compile , link and run successfully.
194) What does extern “C” int Func(int *, short int); mean?
a) Declare Func as extern
b) Will turn off “name mangling” for Func
c) None of the above
196) What result is in the variable num after execution of the following statements?
int num = 58;
num %= 11;
(a) 3 b) 5 c) 2 d) 1 1
a)Yes b) no
203) Malloc can call constructor , new can not call constructor. -
a) True b) False
204) Will the following C+ + program compile and link, or we need to include a header
file like stdio.h or iostream?
int main()
{
return 0;
}
a) It will compile but not linked b) It will not compile
c) It will compile, link but fail at runtim d) It will compile , link and run successfully.
205)Will the following C++ program compile and link, or we need to include a header
file like stdio.h or iostream?
int main()
{
}
a) It will compile but not linked b) It will not compile
c) It will compile, link but fail at runtime. d) It will compile , link and run successfully.
206) What kind of error we will get in the following code? Compilation Error or Linking
Error?
int main ()
{
0;
return 0;
}
a) It will compile but not linked b) It will not compile
c) It will compile, link but fail at runtime. d) It will compile , link and run successfully.
50
Vidyanidhi Info Tech Academy
Vidyanidhi Info Tech Academy
PG DAC C++ Question Bank
208) What kind of error we will get in the following code? Compilation Error on Linking
Error?
int main ()
{
i;
return 0;
}
a) It will compile but not linked b) It will not compile
c) It will compile, link but fail at runtime. d) It will compile , link and run successfully.
209) What kind of error we will get in the following code? Compilation Error or Linking
Error?
int main ()
{
i = 0;
return 0;
}
a) It will compile but not linked b) It will not compile
c) It will compile, link but fail at runtime. d)It will compile , link and run successfully.
211) Which type of variables can be referred from anywhere in the c++ code?
a) All variables c) Local variables
b) Universal variables d) Global variables
213) If value has not type, then the pointer pointing to this value will be known as
a) Empty pointer b)Null pointer c)Void pointer d)None of above
54
Vidyanidhi Info Tech Academy
Vidyanidhi Info Tech Academy
PG DAC C++ Question Bank
233) Will following code work ?
#include<iostream.h>
int * const fun()
{
static int num=40;
return #
}
void main()
{
const int * ptr=fun();
}
a) No b) Yes
Oops
1) Copy Constructor is called when
a) Object is initialized using another object b) Object is assigned to another object
c) A and B both d) none of the above
5) Which of the following is not required in a class that contains dynamic allocation?
a) The copy constructor b) A constructor that copies variables into private variables
c) Destructor d) All of the above are required
10) #include<iostream.h>
class Alpha
{
public:
char data[10000];
Alpha();
~Alpha();
};
class Beta
{
public:
Beta()
{
n=0;
}
void FillData(Alpha a);
private:
58
Vidyanidhi Info Tech Academy
Vidyanidhi Info Tech Academy
PG DAC C++ Question Bank
int n;
};
How do u make the above sample code more efficient ?
a) if possible, make the constructor for Beta private to reduce the overhead of public constructors
b) change the return type in FillData to int to negate the implicit return conversion from “int” to “void”
c) make the destructor for Alpha virtual
d) pass a const reference to Alpha in FillData
62
Vidyanidhi Info Tech Academy
Vidyanidhi Info Tech Academy
PG DAC C++ Question Bank
23) Namespaces
a) Provide a logical grouping of objects
b) Provide a logical grouping of classes
c) Provide a physical grouping of objects
d) Provide a physical grouping of classes
29) What operator is prepended onto the member function name to indicate that the function is a
destructor?
a) & b) * c) ~ d) :: e) –
30) Which one of the following statements is true about constructors and destructors?
a) Both explicitly declared constructors and explicitly declared destructors are required in a class.
b) Neither constructors nor destructors can take parameters.
c) In a given class, constructors are always required, but destructors are not.
d) Constructors can take parameters, but destructors cannot.
e) It is illegal to define either a constructor or a destructor as virtual
64
Vidyanidhi Info Tech Academy
Vidyanidhi Info Tech Academy
PG DAC C++ Question Bank
36) copy constructor is called whenever object is initialized using another reference.
a) true b) false
65
Vidyanidhi Info Tech Academy
Vidyanidhi Info Tech Academy
PG DAC C++ Question Bank
38) #include<iostream.h>
class myclass
{
public:
void myclass()
{
cout<<endl<<"in myclass def\n";
}
myclass(int k)
{
cout<<endl<<"in param const\n";
}
};
void main()
{
myclass m2(30);
}
a) output “ in param const “ b) compilation error c) runtime error d) linker error
39) A _________ is a special member function used to initialize the data members of a class.
41) Member functions of a class are normally made ________ and data members of a class are normally
made ________.
42) The three member access specifiers are ________, ________ and _________.
43) ______________ is called when we initialized one object using other object.
44) The size of a class with no data members and member functions is ________ byte.
66
Vidyanidhi Info Tech Academy
Vidyanidhi Info Tech Academy
PG DAC C++ Question Bank
Is replaced by
mho operator – mho(&y)
And main function is coded as
mho a;
a=a-a;
Then the output will be
a) There was There was b) There was There was a certain man
c) There was a certain man There was a certain man d) compile time error
Operator Overloading
1) Operator= can be overloaded using
a) friend function b) member function c) both A and B d) none of the above
4) In C++ programs the operation of the assignment operator and that of the copy constructor are
a) similar except that the copy constructor creates a new object
b) different except that they both copy member data.
c) both (1) and (2)
d) None of the above.
5) The next three questions are based on the following program segment
#include<iostream.h>
class mho
{
public:
mho(void)
{
cout<<"There was";
}
mho(mho &x)
cout<<"a certain man";
}
{
mho operator-(mho y)
{
67
Vidyanidhi Info Tech Academy
Vidyanidhi Info Tech Academy
PG DAC C++ Question Bank
mho ohm;
return ohm;
}
};
if the function main is coded as
mho a , b;
then output will besss
a) There was There was b) Nothing
c) a runtime error d) There was a certain man There was a certain man.
13) In C++ programs the operation of the assignment operator and that of the copy constructor are
a) different except similar except that the copy constructor creates a new object
b) that they both copy member data.
c) both (1) and (2)
69
Vidyanidhi Info Tech Academy
Vidyanidhi Info Tech Academy
PG DAC C++ Question Bank
d) None of the above.
14) We can’t do anything in source when converting from user defined to primitive type.
a) True b) False.
15) When you overload assignment operator using friend function 2 arguments are required.
a) true b) false
Conversion
1) Which of the following statements is true?
a) Conversion operator function can have a void return type.
b) Conversion operator function must be written in destination
c) Conversion operator function does not accept any argument
d) Conversion operator function can be a friend function.
Inheritance
1) What will happen to following code?
#include<iostream.h>
class SomeClass
{
public:
SomeClass()
{
cout<<endl<<"in SomeClass Def.Const\n";
}
Consider the class inheritance:
class B
{
public:
B();
B(int nn);
void f();
void g();
private:
int n;
};
class D: public B
70
Vidyanidhi Info Tech Academy
Vidyanidhi Info Tech Academy
PG DAC C++ Question Bank
{
public:
D(int nn, float dd);
void h();
private:
double d;
};
Which of the following functions can be invoked by an object of class D?
a) f() b) g() c) h() d) All of the above
5) If parent class has a method which is non-virtual, and child class defines the same method. It is called as
a) overloading b) overriding c) redefinition d) None of these.
7) When two or more objects are derived from a common base class, u can prevent multiple copies of the
base class from being present in an object derived from those objects by declaring base class when it is
inherited.
a) public b) protected c) virtual d) private
8) #include<iostream.h>
class Base
{
public:
int a;
protected:
int b;
private:
int c;
};
class Derived:Base
{
int d;
friend class Friend;
};
class Friend
{
Derived derived;
};
In the above code, which of the following variables can be accessed in “Friend “ ?
72
Vidyanidhi Info Tech Academy
Vidyanidhi Info Tech Academy
PG DAC C++ Question Bank
a) only a and b b) a, b and d c) only a d) error
9) #include<iostream.h>
class A
{
int a;
public:
void fun()
{
cout<<"from fun";
}
};
class B:public A
{
};
class C:virtual A
{
};
class D:public B,C
{
};
void main()
{
D d;
d.fun();
}
What will be the output of this program?
a) from fun b) compile time error c) run time error d) No output
10) #include<iostream.h>
class base
{
public:
base()
{
cout<<"\nbase def\n";
base::disp();
}
void disp()
{
cout<<"base disp\n";
}
};
class sub:public base
{
public:
sub()
73
Vidyanidhi Info Tech Academy
Vidyanidhi Info Tech Academy
PG DAC C++ Question Bank
{
cout<<"sub def\n";
base::disp();
}
void disp()
{
cout<<"sub disp";
}
};
void main()
{
base *b=new base;
}
a) output “ base def base disp” b) compilation error
c) output “base def base disp sub def sub disp” d) output “ base def sub def base disp sub disp “
a) output “base def sub def” b) compilation error c) output “base def base disp sub def “
d) output “base def sub def base disp “ e) compilation error “disp not available in sub”
74
Vidyanidhi Info Tech Academy
Vidyanidhi Info Tech Academy
PG DAC C++ Question Bank
12) #include<iostream.h>
class base
{
public:
base()
{
cout<<"\nbase def\n";
sub::disp();
}
void disp()
{
cout<<"base disp\n";
}
};
class sub:public base
{
public:
sub()
{
cout<<"sub def\n";
}
void disp()
{
cout<<"sub disp\n";
}
};
void main()
{
sub s;
}
a) compilation error b) output “base def sub disp sub def”
c) output “in base def sub def sub disp “ d) output “base def base disp sub disp”
a) compilation error b) output “in base def in sub def in sub disp”
c) output “in base def in sub disp in sub def” d) output “in sub def in base def in sub disp”
14) When child class object is assigned to parent class object, object slicing takes place.
a) True b) False
15) Private members can be inherited but not accessible in derived class.
a) True b) False
a) compilation error
b) output “in sub def in base def in base disp”
c) output “in base def in sub def in sub disp”
d) output “in base def in base disp in sub def”
19) When child class object is assigned to parent class object it is called as _____________________
20) #include<iostream.h>
class Base
{
78
Vidyanidhi Info Tech Academy
Vidyanidhi Info Tech Academy
PG DAC C++ Question Bank
int static i;
public:
Base()
{
}
};
class Sub1:public virtual Base
{
};
class Sub2:public Base
{
};
class Multi:public Sub1,public Sub2
{
};
void main()
{
Multi m;
}
In the above program, how many times Base class constructor will be called ?
a) 1 b) 2 c) 3 d) None
21) When two or more objects are derived from a common base class, u can prevent multiple copies of the
base class from being present in an object derived from those objects by declaring base class when it is
inherited.
a) public b) protected c) virtual d) private
22) class A {
public:
A();
void ~A();
}
class B : public A { };
What is WRONG with the class declarations above?
a) Class B must explicitly define a constructor. b) The destructor in "A" cannot have a void return type.
c) Nothing is wrong with the code above. d) Class B must define a destructor
e) "A" must provide a copy constructor in order for it to be used as a base class.
23) class X {
int i;
protected:
float f;
public:
char c;
};
class Y : protected X { };
79
Vidyanidhi Info Tech Academy
Vidyanidhi Info Tech Academy
PG DAC C++ Question Bank
Referring to the sample code above, which one of the following data members are accessible from class Y?
a) c only b) f and c only c) i and c only d) i and f only e) i, f, and c
25) A class in C++ would be assumed as abstract if it has at least one virtual method
a) true b) False
80
Vidyanidhi Info Tech Academy
Vidyanidhi Info Tech Academy
PG DAC C++ Question Bank
public:
child(int m):parent1(100),parent2(200)
{
cout<<m<<endl;
}
};
void main()
{
child s(300);
}
s
a) 420 100 200 300 b) 420 200 100 300 c) 0 200 100 300 d) 0 420 200 100 300
a) 420 100 200 300 b) 420 200 100 300 c) compilation error d) 0 420 200 100 300
82
Vidyanidhi Info Tech Academy
Vidyanidhi Info Tech Academy
PG DAC C++ Question Bank
class professor
{
public:
professor()
{
cout<<endl<<"professor";
}
};
class researcher
{
public:
researcher()
{
cout<<endl<<"researcher\n";
}
};
class teacher:public professor
{
public:
teacher()
{
cout<<endl<<"teacher";
}
};
class myprofessor:public teacher,public virtual researcher
{
public:
myprofessor()
{
cout<<endl<<"myprofessor\n";
}
};
void main()
{
myprofessor obj;
}
a) professor researcher teacher myprofessor b) researcher professor teacher myprofessor
c) myprofessor teacher researcher professor d)myprofessor researcher professor teacher
31) What is the order of execution of constructors in the hierarchy involving virtual base classes ?
a) i. virtual base class constructor , in the order of their inheritance
ii.non-virtual base class constructor, in the order of their inheritance
iii. derived class constructor
iv. constructors of member objects, in the order of their declaration.
83
Vidyanidhi Info Tech Academy
Vidyanidhi Info Tech Academy
PG DAC C++ Question Bank
b) i. virtual base class constructor, in the order of their inheritance
ii. derived class constructor.
iii. constructors of member objects, in the order of their declaration
iv. non-virtual base class constructor, in the order of their inheritance.
32) ___________ enables reusability which saves time in development , and encourages using
previouslyproven and high quality software.
34) When address of child class object is assigned to parent class pointer or reference, object slicing takes
place.
a) True b) False
35) Protected members can be inherited but not accessible in derived class.
A) True b) False
36) In public inheritance mode protected and public members of parent class becomes ________ and
_______ in child class respectively.
Late Binding
1) #include<iostream.h>
class myclass
{
public:
virtual void f2()
{
cout<<endl<<"in f2\n";
}
virtual void f1()
{
cout<<endl<<"in f1\n";
}
void fun()
{
84
Vidyanidhi Info Tech Academy
Vidyanidhi Info Tech Academy
PG DAC C++ Question Bank
int *ptr=(int*)this;
ptr=(int *)*ptr;
ptr=(int*)*ptr;
}
};
void main()
{
myclass m;
m.fun();
}
when fun() function is over, what does ptr stores ?
a) address of virtual poiner b) address of f1 c) address of f2 d) none of the above
b=new base;
85
Vidyanidhi Info Tech Academy
Vidyanidhi Info Tech Academy
PG DAC C++ Question Bank
s2=dynamic_cast<sub1*>(b);
if(s2)
{
s2->disp();
}
else
{
cout<<"failed\n";
}
b=&s3;
s4=dynamic_cast<sub2*>(b);
if(s4)
{
s4->disp();
}
else
{
cout<<"failed\n";
}
}
a) sub1 disp sub2 disp b) compilation error c) sub2 disp sub2 disp d) failed sub2 disp
4) VTABLE contains
a) addresses of virtual functions b) addresses of virtual pointers
c) address of virtual table d) None of the above
b=&s1;
s2=dynamic_cast<sub1*>(b);
if(s2)
{
s2->disp();
}
else
{
cout<<"failed\n";
}
s4=dynamic_cast<sub2*>(b);
if(s4)
{
s4->disp();
}
88
Vidyanidhi Info Tech Academy
Vidyanidhi Info Tech Academy
PG DAC C++ Question Bank
else
{
cout<<"failed\n";
}
}
a) Error b) sub1 disp sub2 disp c) sub1 disp failed
d) sub1 disp sub1 disp e) sub2 disp sub2 disp
b=&s3;
s2=dynamic_cast<sub1*>(b);
if(s2)
{
s2->disp();
}
89
Vidyanidhi Info Tech Academy
Vidyanidhi Info Tech Academy
PG DAC C++ Question Bank
else
{
cout<<"failed\n";
}
s4=dynamic_cast<sub2*>(b);
if(s4)
{
s4->disp();
}
else
{
cout<<"failed\n";
}
}
a) sub2 disp sub2 disp b) sub1 disp sub2 disp c) failed sub2 disp
d) compilation error e) sub2 disp failed
};
class sub1:public base
{
public:
void disp()
{
}
void print1()
{
cout<<endl<<"in print1\n";
}
};
void main()
{
base *b;
sub1 s1,*s2,*s3;
b=new base;
s2=static_cast<sub1*>(b);
90
Vidyanidhi Info Tech Academy
Vidyanidhi Info Tech Academy
PG DAC C++ Question Bank
s3=dynamic_cast<sub1*>(b);
cout<<s2<<endl;
cout<<s3<<endl;
}
s
a) s2 will contain NULL, s3 not null b) s3 will contain NULL, s2 not null
c) both will contain NULL d) both will contain Not NULL
91
Vidyanidhi Info Tech Academy
Vidyanidhi Info Tech Academy
PG DAC C++ Question Bank
{
cout<<endl<<"in sub disp\n";
}
void print()
{
cout<<endl<<"in print";
}
};
void main()
{
base *b=new sub;
b->disp();
b->print();
}
a) output “in base disp in print” b)output “in sub disp in print”
c) compilation error d) output “in sub disp in base disp in print”
12) #include<iostream.h>
class myclass
{
public:
virtual void f2()
{
cout<<endl<<"in f2\n";
}
virtual void f1()
{
cout<<endl<<"in f1\n";
}
void fun()
{
int *ptr=(int*)this;
ptr=(int *)*ptr;
ptr++;
ptr=(int*)*ptr;
}
};
void main()
{
myclass m;
m.fun();
}
when fun() function is over, what does ptr stores ?
a) address of virtual poiner b) address of f1 c) address of f2 d) none of the above
92
Vidyanidhi Info Tech Academy
Vidyanidhi Info Tech Academy
PG DAC C++ Question Bank
13) What is the output ?
#include<iostream.h>
class base
{
public:
virtual void disp()
{
cout<<"base disp\n";
}
};
class sub1:public base
{
public:
void disp()
{
cout<<"sub1 disp\n";
}
};
class sub2:public sub1
{
public:
void disp()
{
cout<<endl<<"sub2 disp\n";
}
};
void main()
{
base *b;
sub1 s1,*s2;
sub2 s3,*s4;
b=&s3;
s2=dynamic_cast<sub1*>(b);
if(s2)
{
s2->disp();
}
else
{
cout<<"failed\n";
}
s4=dynamic_cast<sub2*>(b);
if(s4)
{
s4->disp();
93
Vidyanidhi Info Tech Academy
Vidyanidhi Info Tech Academy
PG DAC C++ Question Bank
}
else
{
cout<<"failed\n";
}
}
a) sub1 disp sub2 disp b) compilation error c) sub2 disp sub2 disp d) failed sub2 disp
a) s2 will contain NULL, s3 not null b) s3 will contain NULL, s2 not null
c) both will contain NULL d) both will contain Not NULL
16) To get polymorphism for a class you have to mark your methods as
a) Static b) Virtual
c) Pure virtual d) Final
20) #include<iostream.h>
class first
95
Vidyanidhi Info Tech Academy
Vidyanidhi Info Tech Academy
PG DAC C++ Question Bank
{
int a;
virtual void fun(){}
};
What is the size of the class ? (assume 16 bit architecture)
1. 1 byte
2. 2 byte
3. 3 byte
4. 4 byte
22) If a class has 5 virtual functions, then 5 virtual tables will be created.
a)True b) False
23) There is only one virtual table gets created per object.
a)True b) False
24) In case of virtual functions all the objects of a class share virtual pointer.
a. True b. False
96
Vidyanidhi Info Tech Academy
Vidyanidhi Info Tech Academy
PG DAC C++ Question Bank
}
};
void main()
{
base *b=new sub;
}
};
27) In case of dynamic polymorphism, availability of child class object in a base pointer can be checked
using either ___________________ or _____________________.
97
Vidyanidhi Info Tech Academy
Vidyanidhi Info Tech Academy
PG DAC C++ Question Bank
28) Virtual pointer (vptr) points to virtual function.
a)true b)false
29) There is only one virtual table gets created for a class no matter how many instances are created.
a) true b) false
File Handling
1) Difference between text and binary mode is based on
a) How newline is treated b) How End Of File is represented
c) How numeric data is stored d) all of the above
4) The objects that correspond to the standard devices on the system include
a) cin b) cout c) clog d) All of the above.
5) Which of the following is the base class of C++ steam class hierarchy?
a) istream b) iostream c) stream d) ios e) ostream
7) Which is the proper prototype for overloading the “>>” operator for a class like Cpoint
a) istream operator>>(istream, CPoint); b) istream operator>>(istream&, CPoint);
c) istream& operator>>(istream&, CPoint); d) istream& operator>>(istream&, CPoint&)
98
Vidyanidhi Info Tech Academy
Vidyanidhi Info Tech Academy
PG DAC C++ Question Bank
Templates
1) Templates can be distributed to the client through
a) header file b) lib file c) both A and B d) templates can not be distributed at all
2) Which of the following is not a valid initialization of a template class, assuming the class is declared as
follows:
template<class T>
class Pair { }
a) Pair <int>
b) Pair<char>
c) Pair <abc> (assuming abc is a user defined class)
d)All of the above are valid initializations of a template class
5) #include<iostream.h>
template<class T,class X>
class obj
{
T my_t;
X my_x;
public:
obj(T t,X x):my_t(t),my_x(x)
{
}
};
Referring to the sample code above which one of the following is a valid conversion operator for the type T ?
a) T operator T(){ return my_t;} b) T operator (T) const{return my_t;}
c) operator(T) {return my_t;} d) operator T() const{ return my_t;}
99
Vidyanidhi Info Tech Academy
Vidyanidhi Info Tech Academy
PG DAC C++ Question Bank
7) Which one support unknown data types in a single framework ?
a) inheritance b)virtual functions c) abstract base class d) templates.
Exception
1. What happens to the automatic objects that have been constructed in a try block when that block
throws an exception ?
a) only throws exception
b) Destructors are called for each of the objects
c) same as for other variables.
d) None of the above.
100
Vidyanidhi Info Tech Academy