Shriram Mantri Vidyanidhi Info Tech Academy: PG DAC C++ Question Bank
Shriram Mantri Vidyanidhi Info Tech Academy: PG DAC C++ Question Bank
Basic
1) A relational operator
a) assigns one operand to another b) Compares two operands
c) Logically combincs two operands d) assigns value to a operand
7) A functions argument is
a) a variable in the function that receives a value from the calling program
b) a reference value returned by the function
c) a value sent to function by the calling program
d) a value returned by the function to the calling program
12) What will be the storage class of variable in the following code
int main()
{
int i=1;
cout<<i;
return 0;
}
a) Automatic storage class b) External storage class
c) Static storage class d) Register storage class
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
3
Shriram Mantri Vidyanidhi Info Tech Academy
PG DAC C++ Question Bank
6) It is legal to return local variables from a function, through reference .
a) True b) False
4
Shriram Mantri Vidyanidhi Info Tech Academy
PG DAC C++ Question Bank
mystruct *ptr=new mystruct;
ptr->k=#
// here
getch();
}
a) *(*ptr).k or *ptr->k b) *ptr.k c) ptr->k d) ptr->*k
13) *p++ ;
a) increments value b) increments address c) Error d) None
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.
7
Shriram Mantri Vidyanidhi Info Tech Academy
PG DAC C++ Question Bank
a) 20 20 b) 20 21 c) 21 22 d) 22 22
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
8
Shriram Mantri Vidyanidhi Info Tech Academy
PG DAC C++ Question Bank
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
34) For the following allocation which would be the proper deallocation?
int *p = new int[5]
a) Free(p) b) Delete p c) Delete [] p d) None of the above
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
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
57) #include<iostream.h>
void main()
{
char *p="Hello";
12
Shriram Mantri Vidyanidhi Info Tech Academy
PG DAC C++ Question Bank
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
15
Shriram Mantri Vidyanidhi Info Tech Academy
PG DAC C++ Question Bank
a) Successful output b) Warning c) Compilation Error
16
Shriram Mantri Vidyanidhi Info Tech Academy
PG DAC C++ Question Bank
a)Runtime Error b) Neither compilation nor Runtime Error c) Compilation Error
In the above code after disp() method is over, the situation becomes
a) Dangling Poiner b) Memory Leak c) None of these
87) Given
19
Shriram Mantri Vidyanidhi Info Tech Academy
PG DAC C++ Question Bank
#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
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()
21
Shriram Mantri 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()
{
char * const t=”hello”;
t=”world”;
}
a) Runtime Error b) Compilation Error c) Neither Compilation or Runtime Error
24
Shriram Mantri Vidyanidhi Info Tech Academy
PG DAC C++ Question Bank
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;
}
a) Compilation Error b) Warning c) successful output
111) #include<iostream.h>
void main()
{
int a=30;
f();
}
25
Shriram Mantri Vidyanidhi Info Tech Academy
PG DAC C++ Question Bank
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
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
136) Given
#include<iostream.h>
void disp()
{
int *ptr=new int;
}
void main()
{
disp();
}
31
Shriram Mantri Vidyanidhi Info Tech Academy
PG DAC C++ Question Bank
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;
33
Shriram Mantri Vidyanidhi Info Tech Academy
PG DAC C++ Question Bank
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;
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
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
36
Shriram Mantri Vidyanidhi Info Tech Academy
PG DAC C++ Question Bank
154) Will the following code compile and link?
int main ()
{
int stdio = 0;
int iostream = 0;
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;
}
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
161) What is the value of the following on MS Windows 2000 or 32-bit implementation of Linux?
sizeof (unsigned short int)
a) 2 bytes b) 3 bytes c) 4 bytes d) 8 bytes
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?
38
Shriram Mantri Vidyanidhi Info Tech Academy
PG DAC C++ Question Bank
int f ()
{
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
return 0;
}
a) 10 11 b)10 10 c)Compiletime error d)None of the above
0 = 0;
return 0;
40
Shriram Mantri Vidyanidhi Info Tech Academy
PG DAC C++ Question Bank
}
a) nothing wrong b) l-value error
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
42
Shriram Mantri Vidyanidhi Info Tech Academy
PG DAC C++ Question Bank
using namespace std;
int main ()
{
cout << "Hi\n\tHello” < < endl;
return 0;
}
a) Hi and Hello on same line separated by tab
b) Hi and Hello on different lines
c) Compiler error as \n and \t can not be combined together
d) Hello
181) What will be the output of the following code?
#include <iostream>
using namespace std;
int main ()
{
int default = 0;
43
Shriram Mantri Vidyanidhi Info Tech Academy
PG DAC C++ Question Bank
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 ;
}
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()
47
Shriram Mantri Vidyanidhi Info Tech Academy
PG DAC C++ Question Bank
{
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 LinkingError?
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.
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.
48
Shriram Mantri Vidyanidhi Info Tech Academy
PG DAC C++ Question Bank
210) Inline functions are replaced at
a) Run time b)Compile time c) Debug time d) None of above
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
244) For the object for which it was called, a const member function (enhancement)
a) can modify both const and non-const member data
b) can modify only const member data
c) can modify only non-const member data
d) can modify neither const or non-const member data
53
Shriram Mantri Vidyanidhi Info Tech Academy
PG DAC C++ Question Bank
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];
55
Shriram Mantri Vidyanidhi Info Tech Academy
PG DAC C++ Question Bank
Alpha();
~Alpha();
};
class Beta
{
public:
Beta()
{
n=0;
}
void FillData(Alpha a);
private:
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
22) Where does memory get allocated for a static data members of a class
a) Code/text b) Stack
c) Heap d) Data
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
60
Shriram Mantri Vidyanidhi Info Tech Academy
PG DAC C++ Question Bank
24) class Foo
{
int i;
};
In the above sample, what is the member access specifier of the member data "i"?
a) default b) virtual c) protected d) private e) public
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
36) copy constructor is called whenever object is initialized using another reference.
a) true b) false
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
62
Shriram Mantri Vidyanidhi Info Tech Academy
PG DAC C++ Question Bank
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.
45) ___________ keyword if used , constructor will not be available for conversion.
63
Shriram Mantri Vidyanidhi Info Tech Academy
PG DAC C++ Question Bank
51) A copy constructor is invoked when
a) a function returns by value
b) an object is passed by value to a function
c) a function is returned by reference
d) an object is passed by reference to a function
52) The dot operator( or class member access operator) connects the following two entities reading from left
to right
a) A class member and a class object b) A class object and a class
c) A class and a member of that class d) A class object and a member of that class
54) which among the following type of pointer is used to represent an object that invokes a member function
a) void pointer b) null pointer c) this pointer d) base pointer
55) Which among the following operator is used to identify the class to which a member function belongs
a) Í b) [] c) :: d) .*
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)
64
Shriram Mantri Vidyanidhi Info Tech Academy
PG DAC C++ Question Bank
cout<<"a certain man";
}
{
mho operator-(mho y)
{
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)
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.
66
Shriram Mantri Vidyanidhi Info Tech Academy
PG DAC C++ Question Bank
15) When you overload assignment operator using friend function 2 arguments are required.
a) true b) false
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
{
public:
D(int nn, float dd);
void h();
private:
67
Shriram Mantri Vidyanidhi Info Tech Academy
PG DAC C++ Question Bank
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 “ ?
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()
{
69
Shriram Mantri Vidyanidhi Info Tech Academy
PG DAC C++ Question Bank
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()
{
cout<<"sub def\n";
base::disp();
}
void disp()
{
cout<<"sub disp";
}
};
void main()
{
base *b=new base;
70
Shriram Mantri Vidyanidhi Info Tech Academy
PG DAC C++ Question Bank
}
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 “
12) #include<iostream.h>
class base
{
public:
base()
{
cout<<"\nbase def\n";
sub::disp();
}
void disp()
{
cout<<"base disp\n";
71
Shriram Mantri Vidyanidhi Info Tech Academy
PG DAC C++ Question Bank
}
};
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”
72
Shriram Mantri Vidyanidhi Info Tech Academy
PG DAC C++ Question Bank
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
{
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
75
Shriram Mantri Vidyanidhi Info Tech Academy
PG DAC C++ Question Bank
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 { };
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
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.
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.
37) Which of the following access specifier is used as default in a class definition?
a) protected b) Public c) Private d) Friend
41) Which of the following cannot be used with the keyword virtual?
a) class b) member functions c) constructor d) destructor
44) Assume a class Derv that is privately derived from class Base. An object of class Derv located in main() can
access
a) public member of Derv b) protected members of derv
c) public member of Base d) protected members of Base
45) When both base and derived class contain constructors & destructor’s which of the following choice is
correct
a) Both constructors & Destructors are executed in reverse order of derivation.
b) Both Constructors & Destructors are executed in their order of derivation.
c) Constructors are executed in their order of derivation and Destructors are executed in the reverse order
of derivation
d) Constructors are executed in reverse order of derivation and Destructors are executed in their order of
derivation
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()
81
Shriram Mantri 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;
s2=dynamic_cast<sub1*>(b);
if(s2)
{
82
Shriram Mantri Vidyanidhi Info Tech Academy
PG DAC C++ Question Bank
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) 01101
01010
c) 01011
11010
d) 10100
11011
b=&s1;
s2=dynamic_cast<sub1*>(b);
if(s2)
{
s2->disp();
}
else
{
cout<<"failed\n";
}
s4=dynamic_cast<sub2*>(b);
if(s4)
{
s4->disp();
}
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();
}
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
86
Shriram Mantri Vidyanidhi Info Tech Academy
PG DAC C++ Question Bank
9) Given the following code :
#include<iostream.h>
class base
{
public:
virtual void disp()
{
cout<<endl<<"in base disp\n";
}
};
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);
s3=dynamic_cast<sub1*>(b);
cout<<s2<<endl;
cout<<s3<<endl;
}
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
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";
88
Shriram Mantri Vidyanidhi Info Tech Academy
PG DAC C++ Question Bank
}
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
b=&s3;
s2=dynamic_cast<sub1*>(b);
if(s2)
{
s2->disp();
}
else
{
cout<<"failed\n";
}
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
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
{
int a;
virtual void fun(){}
};
What is the size of the class ? (assume 16 bit architecture)
a) 1 byte
b) 2 byte
c) 3 byte
d) 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
};
class sub:public base
{
public:
sub()
{
cout<<"sub def\n";
}
void disp()
{
cout<<endl<<"in sub disp\n";
}
};
void main()
{
93
Shriram Mantri Vidyanidhi Info Tech Academy
PG DAC C++ Question Bank
27) In case of dynamic polymorphism, availability of child class object in a base pointer can be checked
using either ___________________ or _____________________.
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&)
94
Shriram Mantri 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;}
8) An STL algorithm is
a) A standalone function that operates on containers.
b) A Link between member functions and containers.
c) a friend function of appropriate container classes.
d) a member function of appropriate container classes.
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.
96