You are on page 1of 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 12 b) Outputs 10 c) Outputs the address of v 2. How long does this loop run: for(int x= ! x="! x++# a) Never b) Three times c) Forever ". Whi$h uses less %e%or&' a) struct astruct { int x; float y; int v; }; b) union aunion { int x; float v; }; c) c ar array!10"; (. )*aluate:

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) 10 b) 11 c) 1 +. Whi$h of the ,tandard C++ $asts $an -e used to perfor% a .safe/ down$ast: a) reinterpret_cast b) dynamic_cast c) static_cast d) const_cast 0. class class class class

)rofessor {}; teac er * )u+lic virtual )rofessor {}; researc er * )u+lic virtual )rofessor {}; my)rofessor * )u+lic teac er, )u+lic researc er {};

1eferring to the sa%ple $ode a-o*e2 if an o-3e$t of $lass 4%&professor4 were $reated2 how %an& instan$es of professor will it $ontain' a) 0 b) 1 c) 2 d) e) !

5. string somestring ; Whi$h of the following $hoi$es will $on*ert a standard C++ string o-3e$t 4so%estring4 to a C string' a) -o)y.somestrin/ () ; b) somestrin/.c0str () c) &somestrin/ !1" d) std**cstrin/ (somestrin/)

e) (c ar *) somestrin/ 6. class +asex { int x; )u+lic* void setx(int y) {x=y;} }; class derived * +asex {}; What is the a$$ess le*el for the %e%-er fun$tion 4setx4 in the $lass 4deri*ed4 a-o*e' a) private b) "oca" c) #"oba" d) pub"ic e) protected 7. class 1l) a { )u+lic* c ar data!10000"; 1l) a(); 21l) a(); }; class 3eta { )u+lic* 3eta() { n = 0; } void 4ill5ata(1l) a a); )rivate* int 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 of pub"ic constructors( b) )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;

)u+lic* 4oo(int 6); }; 9f a $lass does not ha*e a $op& $onstru$tor expli$itl& defined one will -e i%pli$itl& defined for it. 1eferring to the sa%ple $ode a-o*e2 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; }; 1eferring to the sa%ple $ode a-o*e2 what is the appropriate %ethod of defining the %e%-er *aria-le 4942 and assigning it the *alue 1 2 outside of the $lass de$laration' a) 7as8tatic 6 = 10; b) int static 6 = 10; c) static 6(10); d) static 6 = 10; e) int 7as8tatic**6 = 10; 12. class 9 { )rivate* int a; )rotected* 9(){cout<<:9 constructor was called:<<endl;} 29(){cout<<:9 destructor was called:<<endl} }; 1eferring to the $ode a-o*e2 whi$h one of the following state%ents regarding 4:4 is ;1<)' a) / is an abstract c"ass( b) On"y subc"asses of / may create / ob0ects( c) $nstances of / cannot be created( d) / ob0ects can on"y be created usin# the defau"t copy constructor( e) On"y friends can create instances of / ob0ects( 1". class 4oo { const int x;

)rotected* 4oo(int f); 24oo(); }; 4oo f; 1eferring to the sa%ple $ode a-o*e2 wh& will the $lass de$laration not $o%pile' a) The variab"e 1 is const( b) The destructor is protected( c) The destructor is not pub"ic( d) The constructor is protected( e) There is no defau"t constructor( 1(. class 4oo { )u+lic* 4oo(int i) { } }; class 3ar * virtual 4oo { )u+lic* 3ar() { } }; 3ar +; 1eferring to the a-o*e $ode2 when the o-3e$t =-= is defined2 a $o%piler error will o$$ur. What a$tion fixes the $o%piler error' a) -ddin# a virtua" destructor to the c"ass 'ar b) -ddin# a constructor to 'ar 2hich ta&es an int parameter c) -ddin# +Foo()+ to the 'ar constructor d) -ddin# a copy constructor to the c"ass Foo e) -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$ted4 inheritan$e' a) The base c"ass has access on"y to the pub"ic or protected members of the derived c"ass( b) The derived c"ass has non5pub"ic% inheritab"e% access to a"" but the private members of the base c"ass( c) The derived c"ass has access to a"" members of the base c"ass( d) The private members of the base c"ass are visib"e 2ithin the derived c"ass( e) .ub"ic members of the derived c"ass are private"y accessib"e from the base c"ass(

10. ;he 4*irtual4 spe$ifier in a %e%-er fun$tion ena-les whi$h one of the following' a) ,onmorphism b) 6ate bindin# c) ,etamorphism d) 7o"omorphism e) $nheritance 15. class 9 { )u+lic* int x; static void f(int ;); }; void 9**f(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 members( b) The static member function f() accesses the non5static 4( c) The static member function f() accesses the non5static 1( d) The member function f() must return a va"ue( e) The c"ass / does not have any private members( 16.tem)late<class <, class 9= class >+? { < my0t; 9 my0x; )u+lic* >+?(< t, 9 x) * my0t(t), my0x(x) { } }; 1eferring to the sa%ple $ode a-o*e2 whi$h one of the following is a *alid $on*ersion operator for the t&pe ;' a) < o)erator < () { return my0t; } b) < o)erator(<) const { return my0t; } c) o)erator(<) { return my0t; } d) < o)erator < (const >+? &o+?) { return o+?.my0t; } e) o)erator < () const { return my0t; }

17.catch(exception &e) { . . . } 1eferring to the sa%ple $ode a-o*e2 whi$h one of the following lines of $ode produ$es a written des$ription of the t&pe of ex$eption that 4e4 refers to' a) cout << e.ty)e();

b) cout c) cout d) cout e) cout 2 .

<< << << <<

e.name(); ty)eid(e).name(); e.w at(); e;

int f() { int 6 = 12; int &r = 6; r &= r % @; int *) = &r; *) &= r; return 6;

} 1eferring to the sa%ple $ode a-o*e2 what is the return *alue of the fun$tion 4f(#4' a) 12 b) 18 c) 2! d) 17 e) 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 -- 10ello1 -- 1Worl*1, B)co&t -- 10ello 2n Worl*1, )co&t -- 10ello Worl*2n1, ")cin 33 10ello Worl*1, 24. What is the o&tp&t o' the 'ollo(in) co*e? int n=1, i' +n=4) co&t --1n = 41, else co&t --1n = 1 -- n, A)n = 4 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=4, i-14, i++) co&t -- i%2 -- 1 1, 5

A)4 1 2 / 4 5 6 7 7 8 41414141 ")1 4 1 4 1 4 1 4 1 4

B)4 2 4 6 7 14 12 14 16 17 $)1

)4 1

26. What is the o&tp&t o' this co*e? 9oi* s(ap+int a: int b) ; int temp=a, a=b, b=temp, 5 9oi* main+) ; int a=/, b=5, s(ap+a: b), co&t -- a -- 1 1 -- b, 5 A)5 / B)/ 5 )5 5 ")/ / $).he pro)ram pro*&ces a compiler error 27. Which one o' the 'ollo(in) choices represents a bene'it o' inheritance? A) <t res&lts in co*e re&se. B) <t re*&ces memor= &sa)e. ) <t &ni'orml= re*&ces loa* mo*&le si>e across all the classes. $) <t helps spee* e?ec&tion o' a pro)ram since it is a pre-compile* block. 27.char a @A = 1AB "2n1 , 0o( man= elements (ill be in the arra= create* b= the *eclaration in the sample abo9e? A) 4 B) 5 ) 7 ") 8 28. class base? ; int ?, p&blicB 9oi* set?+int =) ;?==,5 5, class *eri9e* B base? ;5, What is the access le9el 'or the member '&nction 1set?1 in the class 1*eri9e*1 abo9e? A) local B) Cri9ate ) p&blic

/4. Which o' the 'ollo(in) choices is not an e?ample o' an int? A)4 B)-/2 )472 ")655/1 $)-4.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(); 5 void main() ; base base!b"ect; SomeFunc(%base!b"ect); deri deri!b"ect; SomeFunc(%deri!b"ect); 5
a.

'rom base 'rom base 'rom base 'rom *eri9e*

b.

c. 'rom *eri9e* 'rom base *. 'rom *eri9e* 'rom *eri9e*

/2. What is o&tp&t o' 'ollo(in) Dincl&*e -iostream.h3 int main+) ; co&t -- 1Eatri?1 -- en*l, ret&rn 4, 5 a .$?ec&tion time error b. F=nta? error c. Eatri? *. Gone //.<nheritance is calle* asB a. 0as a relationship b. <s a relationship c. He&sabilit= *. Gone /4.Which o' the 'ollo(in) is not a correct 9ariable t=pe? 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") {

base!b"#$baseFun(); } int main() { base base!b"ect; SomeFunc(%base!b"ect); deri deri!b"ect; SomeFunc(%deri!b"ect); }


a.

'rom base 'rom *eri9e* 'rom base 'rom *eri9e*

b.

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* print+int !ptr) ; co&t--ptr@4A, 5

9oi* print+int ptr@si>eA) ; co&t--ptr@4A, 5 9oi* main+) ; int a@si>eA = ;1:2:/:4:55, int !b = ne( int+si>e), print+a), print+b), 5 a. prints contents o' a b. prints si>e o' a c. both a an* b *. compiler error /7.class comple?; *o&ble re, *o&ble im, p&blicB comple?+) B re+4):im+4) ;5 comple?+*o&ble n) ; re=n:im=n,5, comple?+int m:int n) ; re=m:im=n,5 9oi* print+) ; co&t--re, co&t--im,5 5, 9oi* main+); comple? c/, *o&ble i=5, c/ = i, c/.print+), 5 a. 4:4 b. 5:5 c. Larba)e 9al&e *. H&ntime error /8.Which is not an iterator t=pe?

a. Mor(ar* iterator b. Back(ar* iterator c. <np&t iterator *. Bi*irectional iterator 44. lass template is a. Leneric class b. Lro&p o' '&nction templates c. F.N *. Gone 41.B= *e'a&lt '&n act like as _________ '&nction a. Gormal '&nction b. Eacro c. <nline *. All 42.Oirt&al table is a. "=namic b. c. reate* 'or base class reate* 'or *eri9e* class

*. Both a an* b 4/.Game the operators that cannot be o9erloa*e*. a. Fi>eo' b. BB c. Both a an* b

*. Gone
44.

M&nctions can be *eclare* (ith *e'a&lt 9al&es in parameters. We &se *e'a&lt 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 *esi)n b. .o implement '&nction o9erri*in) c. Oirt&al constr&ctors *o not e?ist *.Gone 46..(o '&nctions (ith same name :same n&mber o' parameters onl= *i''er in ret&rn t=pe is calle* '&nction o9erloa*in) a. tr&e b. 'alse 47. Eemor= leak is a. Fame as memor= e?ception b. Fame as con*ition that occ&rs *&e to e?cee*in) bo&n*s o' arra= c. Fame as *an)lin) pointer *. All the abo9e e. Gone o' the abo9e 47 . He)ar*in) the &se o' ne( line character +2n) an* en*l manip&lator (ith co&t statement a.Both (a=s are e?actl= same b.Both are similar b&t en*l a**itionall= per'orms 'l&shin) o' b&''er

c.en*l canPt be &se* (ith co&t *. 2n canPt be &se* (ith co&t 48..he si>e o' an empt= class is a.Qero b.Kne b=te c..(o b=tes *.$mpt= class is not possible 54.<*enti'= the KHH$ . Crotot=pe 'or Oirt&al '&nction. e. Oirt&al ret&rn_t=pe '&nction_name+parameter_list)=4, '. Oirt&al ret&rn_t=pe '&nction_name+parameter_list), ). Oirt&al '&nction_name+parameter_list)=GRNN, h. Oirt&al 9oi* '&nction_name+parameter_list,)

***ALL THE BEST***

Answers:
1. 2. ". (. +. 0. 5. 6. 7. 1 . 11. 12. 1". 1(. 1+. 10. 15. 16. 17. 2 . $ a e d e e e $ e $ e

$f u need "atest papers of any company mai" me at p"acement0!20029yahoo(com

You might also like