Click here to load reader
Upload
amyy23
View
215
Download
0
Embed Size (px)
Citation preview
8/12/2019 C++InterviewQuestions CAP
http://slidepdf.com/reader/full/cinterviewquestions-cap 1/17
C++ FAQs
1. What does the following do:
void afunction(int *x){ x=new int; *x=12;}int main(){ int v=10; afunction(&v); cout<<v;}
a) Outputs 12b) Outputs 10c) Outputs the address of v
2. How long does this loop run: for(int x=! x="! x++#a) Never b) Three timesc) Forever
". Whi$h uses less %e%or&'a)struct astruct{ int x; float y; int v;};
b)union aunion{ int x; float v;};
c)car array!10";
. )*aluate:
8/12/2019 C++InterviewQuestions CAP
http://slidepdf.com/reader/full/cinterviewquestions-cap 2/17
int fn(int v){ if(v==1 ## v==0) return 1; if(v$2==0)
return fn(v%2)2; else return fn(v'1);}
for fn(7);
a) 10b) 11c) 1
. Whi$h of the ,tandard C++ $asts $an -e used to perfor% a safe/down$ast:a) reinterpret_castb) dynamic_castc) static_castd) const_cast
0.class rofessor {};class teacer u+lic virtual rofessor {};class researcer u+lic virtual rofessor {};class myrofessor u+lic teacer, u+lic researcer {};
eferring to the sa%ple $ode a-o*e if an o-3e$t of $lass 4%&professor4were $reated how %an& instan$es of professor will it $ontain'a) 0b) 1c) 2d) e) !
5.string somestring ;
Whi$h of the following $hoi$es will $on*ert a standard C++ string o-3e$t4so%estring4 to a C string'a) -oy.somestrin/ () ;
b) somestrin/.cstr ()
c) &somestrin/ !1"
d) stdcstrin/ (somestrin/)
8/12/2019 C++InterviewQuestions CAP
http://slidepdf.com/reader/full/cinterviewquestions-cap 3/17
e) (car *) somestrin/
6.class +asex
{ int x;u+licvoid setx(int y) {x=y;}
};class derived +asex {};
What is the a$$ess le*el for the %e%-er fun$tion 4setx4 in the $lass4deri*ed4 a-o*e'a) privateb) "oca"c) #"oba"
d) pub"ice) protected
7.class la {u+lic
car data!10000";la();la();
};class 3eta {u+lic
3eta() { n = 0; }void 4ill5ata(la a);
rivateint n;
};
How do &ou %a8e the a-o*e sa%ple $ode %ore effi$ient'a) $f possib"e% ma&e the constructor for 'eta private to reduce the overhead ofpub"ic constructorsb) han#e the return type in Fi""*ata to int to ne#ate the imp"icit return
conversion from +int+ to +void+c) ,a&e the destructor for -"pha virtua"d) ,a&e the constructor for -"pha virtua"e) .ass a const reference to -"pha in Fi""*ata
1.class 4oo {
int x;
8/12/2019 C++InterviewQuestions CAP
http://slidepdf.com/reader/full/cinterviewquestions-cap 4/17
u+lic4oo(int 6);
};
9f a $lass does not ha*e a $op& $onstru$tor expli$itl& defined one will -ei%pli$itl& defined for it. eferring to the sa%ple $ode a-o*e whi$h one of
the following de$larations is the i%pli$itl& $reated $op& $onstru$tor'a) 4oo(4oo *f);
b) 4oo(4oo &f);
c) 4oo(const 4oo *f);
d) 4oo(const 4oo &f);
e) 4oo(int);
11.class 7as8tatic {
static int 6;};
eferring to the sa%ple $ode a-o*e what is the appropriate %ethod ofdefining the %e%-er *aria-le 494 and assigning it the *alue 1 outside ofthe $lass de$laration'a) 7as8tatic 6 = 10;
b) int static 6 = 10;
c) static 6(10);
d) static 6 = 10;
e) int 7as8tatic6 = 10;
12.
class 9{rivateint a;
rotected9(){cout<<:9 constructor was called:<<endl;}9(){cout<<:9 destructor was called:<<endl}
};
eferring to the $ode a-o*e whi$h one of the following state%entsregarding 44 is ;<)'a) / is an abstract c"ass
b) On"y subc"asses of / may create / obectsc) $nstances of / cannot be createdd) / obects can on"y be created usin# the defau"t copy constructore) On"y friends can create instances of / obects
1".class 4oo {
const int x;
8/12/2019 C++InterviewQuestions CAP
http://slidepdf.com/reader/full/cinterviewquestions-cap 5/17
rotected4oo(int f);4oo();
};4oo f;
eferring to the sa%ple $ode a-o*e wh& will the $lass de$laration not$o%pile'a) The variab"e is constb) The destructor is protectedc) The destructor is not pub"icd) The constructor is protectede) There is no defau"t constructor
1.class 4oo {u+lic
4oo(int i) { }};class 3ar virtual 4oo {u+lic
3ar() { }};
3ar +;
eferring to the a-o*e $ode when the o-3e$t - is defined a $o%piler errorwill o$$ur. What a$tion fixes the $o%piler error'
a) -ddin# a virtua" destructor to the c"ass 'arb) -ddin# a constructor to 'ar hich ta&es an int parameterc) -ddin# +Foo()+ to the 'ar constructord) -ddin# a copy constructor to the c"ass Fooe) -ddin# +Foo(0)+ to the 'ar33'ar initia"i4er "ist
1. Whi$h one of the following des$ri-es $hara$teristi$s of 4prote$ted4inheritan$e'a) The base c"ass has access on"y to the pub"ic or protected members of thederived c"ass
b) The derived c"ass has non5pub"ic% inheritab"e% access to a"" but the privatemembers of the base c"assc) The derived c"ass has access to a"" members of the base c"assd) The private members of the base c"ass are visib"e ithin the derived c"asse) .ub"ic members of the derived c"ass are private"y accessib"e from the basec"ass
8/12/2019 C++InterviewQuestions CAP
http://slidepdf.com/reader/full/cinterviewquestions-cap 6/17
10. ;he 4*irtual4 spe$ifier in a %e%-er fun$tion ena-les whi$h one of thefollowing'a) ,onmorphismb) 6ate bindin#c) ,etamorphism
d) o"omorphisme) $nheritance
15. class 9{u+licint x;static void f(int );
};void 9f(int y) {x=y;}
What is the error in the sa%ple $ode a-o*e'a) The c"ass / does not have any protected membersb) The static member function f() accesses the non5static 4c) The static member function f() accesses the non5static d) The member function f() must return a va"uee) The c"ass / does not have any private members
16.temlate<class , class 9 class >+? { myt;9 myx;
u+lic>+?( t, 9 x) myt(t), myx(x) { }
};
eferring to the sa%ple $ode a-o*e whi$h one of the following is a *alid$on*ersion operator for the t&pe ;'a) oerator () { return myt; }
b) oerator() const { return myt; }
c) oerator() { return myt; }
d) oerator (const >+? &o+?) { return o+?.myt; }
e) oerator () const { return myt; }
17.catch(exception &e){
. . .}
eferring to the sa%ple $ode a-o*e whi$h one of the following lines of$ode produ$es a written des$ription of the t&pe of ex$eption that 4e4 refersto'a) cout << e.tye();
8/12/2019 C++InterviewQuestions CAP
http://slidepdf.com/reader/full/cinterviewquestions-cap 7/17
b) cout << e.name();
c) cout << tyeid(e).name();
d) cout << e.wat();
e) cout << e;
2. int f() {
int 6 = 12;int &r = 6;r = r % @;int * = &r;* = r;return 6;
}
eferring to the sa%ple $ode a-o*e what is the return *alue of the fun$tion4f(#4'
a) 12b) 18c) 2!d) 17e) 0
21. 54 __ 7 = 5. Which arithmetic operator can replace the blank to make the statement
correct?
A)+ B)- )! ")# $)%
22. What is the o&tp&t o' the 'ollo(in co*e?
'or int a = 1, a = 1, a++)
co&t a++, co&t a,
A)22 B)12 )here (ill be a r&n-time error ")2/ $)1/
2/. Which one o' the choices (o&l* pro*&ce the 'ollo(in o&tp&t? 0ello
Worl*
A)co&t 0ello Worl*, B)co&t 0ello n Worl*,
)co&t 0ello Worl*n, ")cin 33 0ello Worl*,
24. What is the o&tp&t o' the 'ollo(in co*e?
int n=1,i' n=) co&t n = ,
else co&t n = n,
A)n = B)n = n )n = 1 ")he co*e enerates an
error $)n =
25. What is the o&tp&t o' the 'ollo(in co*e? 'or int i=, i1, i++) co&t
i%2 ,
8/12/2019 C++InterviewQuestions CAP
http://slidepdf.com/reader/full/cinterviewquestions-cap 8/17
A) 1 2 / 4 5 6 7 8 B) 2 4 6 1 12 14 16 1 ) 1
1 1 1 1
")1 1 1 1 1 $)1
26. What is the o&tp&t o' this co*e?
9oi* s(apint a: int b) ;
int temp=a, a=b, b=temp,
9oi* main) ;
int a=/, b=5,
s(apa: b),
co&t a b,
A)5 / B)/ 5 )5 5 ")/ / $)he proram pro*&ces a compiler error
27. Which one o' the 'ollo(in choices represents a bene'it o' inheritance?
A) <t res<s in co*e re&se.B) <t re*&ces memor &sae.
) <t &ni'orml re*&ces loa* mo*&le si>e across all the classes.
$) <t helps spee* eec&tion o' a proram since it is a pre-compile*
block.
2.char a @ = AB"n ,
0o( man elements (ill be in the arra create* b the *eclaration in the
sample abo9e?
A) 4
B) 5
)
") 8
28. class base
;
int ,
p&blic
9oi* setint ) ;=, ,class *eri9e* base ;,
What is the access le9el 'or the member '&nction set in the class
*eri9e* abo9e?
A) local
B) Cri9ate
) p&blic
8/12/2019 C++InterviewQuestions CAP
http://slidepdf.com/reader/full/cinterviewquestions-cap 9/17
/. Which o' the 'ollo(in choices is not an eample o' an int?
A) B)-/2 )72 ")655/1 $)-4.
31.class base
{ public:
void baseFun(){ cout<<"from base"; }
};
class deri:public base
{
public:
void baseFun(){ cout<< "from derived"; }
};
void SomeFunc(base base!b)
;
base!b#$baseFun();
void main()
;
base base!bect;
SomeFunc(%base!bect);
deri deri!bect;
SomeFunc(%deri!bect);
a. 'rom base
'rom base
b. 'rom base
'rom *eri9e*
c. 'rom *eri9e*
'rom base
*. 'rom *eri9e*
'rom *eri9e*
8/12/2019 C++InterviewQuestions CAP
http://slidepdf.com/reader/full/cinterviewquestions-cap 10/17
/2. What is o&tp&t o' 'ollo(in
Dincl&*e iostream.h3
int main) ;
co&t Eatri en*l,
ret&rn ,
a .$ec&tion time error
b. Fnta error
c. Eatri
*. Gone
//.<nheritance is calle* as
a. 0as a relationship
b. <s a relationship
c. He&sabilit
*. Gone
/4.Which o' the 'ollo(in is not a correct 9ariable tpe?
e. 'loat
'. real
. int
h. *o&ble
35. class base
{
public:
virtual void baseFun(){ cout<<"from base"; }
};
class deri:public base {
public:
void baseFun(){ cout<< "from derived"; }
};
void SomeFunc(base base!b)
{
8/12/2019 C++InterviewQuestions CAP
http://slidepdf.com/reader/full/cinterviewquestions-cap 11/17
base!b#$baseFun();
}
int main()
{
base base!bect;SomeFunc(%base!bect);
deri deri!bect;
SomeFunc(%deri!bect);
}
a. 'rom base
'rom *eri9e*
b. 'rom base
'rom *eri9e*
c. 'rom *eri9e*
'rom base
*. 'rom *eri9e*
'rom *eri9e*
/6. haracteristics o' static member '&nctions are I
a. his pointer can be &se*
b. annot be *eclare* const or 9irt&al
c. <t canJt *irectl access the non-static members o' its class
*. All the abo9e
e. Knl b an* c
/7. const int si>e = 5,
9oi* printint !ptr)
;
co&tptr@,
8/12/2019 C++InterviewQuestions CAP
http://slidepdf.com/reader/full/cinterviewquestions-cap 12/17
9oi* printint ptr@si>e)
;
co&tptr@,
9oi* main)
;
int a@si>e = ;1:2:/:4:5,
int !b = ne( intsi>e),
printa),
printb),
a. prints contents o' a
b. prints si>e o' a
c. both a an* b
*. compiler error
/.class comple;
*o&ble re,
*o&ble im,
p&blic
comple) re):im) ;
comple*o&ble n) ; re=n:im=n,,
compleint m:int n) ; re=m:im=n,
9oi* print) ; co&tre, co&tim,
,
9oi* main);
comple c/,
*o&ble i=5,
c/ = i,c/.print),
a. :
b. 5:5
c. Larbae 9al&e
*. H&ntime error
/8.Which is not an iterator tpe?
8/12/2019 C++InterviewQuestions CAP
http://slidepdf.com/reader/full/cinterviewquestions-cap 13/17
a. Mor(ar* iterator
b. Back(ar* iterator
c. <np&t iterator
*. Bi*irectional iterator
4.lass template is
a. Leneric class
b. Lro&p o' '&nction templates
c. FN
*. Gone
41.B *e'a< '&n act like as _________ '&nction
a. Gormal '&nction
b. Eacro
c. <nline
*. All42.Oirt&al table is
a. "namic
b. reate* 'or base class
c. reate* 'or *eri9e* class
*. Both a an* b
4/.Game the operators that cannot be o9erloa*e*.
a. Fi>eo'
b.
c. Both a an* b
8/12/2019 C++InterviewQuestions CAP
http://slidepdf.com/reader/full/cinterviewquestions-cap 14/17
*. Gone
44. M&nctions can be *eclare* (ith *e'a< 9al&es in parameters. We &se
*e'a< ke(or* to speci' the 9al&e o' s&ch parameters.
a. r&e
b. Malse
45.Oirt&al constr&ctors are &se'&l I
a. o implement so't(are *esin
b. o implement '&nction o9erri*in
c. Oirt&al constr&ctors *o not eist
*.Gone
46.(o '&nctions (ith same name :same n&mber o' parameters onl
*i''er in ret&rn tpe is calle* '&nction o9erloa*in
a. tr&e
b. 'alse
47. Eemor leak is
a. Fame as memor eception
b. Fame as con*ition that occ&rs *&e to ecee*in bo&n*s o'
arra
c. Fame as *anlin pointer
*. All the abo9e
e. Gone o' the abo9e
4 . Hear*in the &se o' ne( line character n) an* en*l
manip&lator (ith co&t statement
a.Both (as are eactl same
b.Both are similar b&t en*l a**itionall per'orms 'l&shin o' b&''er
8/12/2019 C++InterviewQuestions CAP
http://slidepdf.com/reader/full/cinterviewquestions-cap 15/17
c.en*l canPt be &se* (ith co&t
*. n canPt be &se* (ith co&t
48.he si>e o' an empt class is
a.Qero
b.Kne bte
c.(o btes
*.$mpt class is not possible
5.<*enti' the KHH$ Crototpe 'or Oirt&al '&nction.
e. Oirt&al ret&rn_tpe '&nction_nameparameter_list)=,
'. Oirt&al ret&rn_tpe '&nction_nameparameter_list),
. Oirt&al '&nction_nameparameter_list)=GRNN,
h. Oirt&al 9oi* '&nction_nameparameter_list,)
***ALL THE BEST***
8/12/2019 C++InterviewQuestions CAP
http://slidepdf.com/reader/full/cinterviewquestions-cap 16/17
8/12/2019 C++InterviewQuestions CAP
http://slidepdf.com/reader/full/cinterviewquestions-cap 17/17
Answers:
1. -2. $". -
. -. -0. -5. -6. a7. e1. d11. e12. -1". e1. e1. -10. -
15. $16. e17. $2. e
$f u need "atest papers of any company mai" me atp"acement0!20029yahoocom