What are the 3 automatically generated functions for any class(The three amigos)?
Answer
Look at the template definitions below.
1. template <typename T> 2. class FOO{ 3. public: 4. int B(T t); //(A) 5. int B(int i); //(B) 6. int B(int i) const; //(C) 7. }; 8. 9. template<> 10. class FOO<bool>{ 11. public: 12. int B(int i); //(D) 13. int B(int i) const; //(E) 14. }; 15. 16. int main() { 17. const FOO<bool> f; 18. f.B(10); //(F) 19. } |
(1) A
(2) B
(3) C
(4) D
(5) E
(6)Wont compile / compiler dependent.
Answer
What is the value of the expression 5["abcdef"] ? Does it even compile?
Answer
1. class base{ 2. public: 3. ~base(){} //(A) 4. }; 5. class child: public base{ 6. public: 7. ~child(){} //(B) 8. }; 9. 10. int main(){ 11. base * b = new child(); 12. delete b; 13. } |
(1) A and then B
(2) B and then A
(3) A only
(4) B only
(5) Wont compile
Answer
Given that #define sum(a,b) a+b what is the value of: 5*sum(3+1,2);
(1) 30
(2) 18
(3) 22
(4) None of the above
(5) Implementation dependent
Answer
1. void allocateme(int* p){ 2. p = new int; 3. *p = 2; 4. } 5. 6. int main(){ 7. int i = 1; 8. allocateme(&i); 9. std::cout<<i; 10. return 0; 11. } |
(1) Outputs '1'
(2) Outputs '2'
(3) Outputs some rubbish value
(4) Will not compile or compiler dependent
Answer
1. class a{ 2. public: 3. virtual void show(){} //(A) 4. }; 5. class b: public a{ 6. public: 7. virtual void show(){} //(B) 8. }; 9. class c{ 10. public: 11. void f(a* ap){ 12. ap->show(); 13. } 14. void f(b* ab){ 15. ab->show(); 16. } 17. }; 18. int main(){ 19. a* px = new b(); 20. c* pc = new c(); 21. px->show(); //(C) 22. pc->f(px); //(D) 23. } |
(1) B and B
(2) A and B
(3) B and A
(4) A and A
Answer
What is the final value of 'v'?
1. int m=0,v=0; 2. 3. for(m=5;m>0;){ 4. v=v+(m++)+(++m); 5. m=m-3; 6. } 7. 8. cout<<v<<endl; |
Answer
Destructors
1. class X{ 2. public: 3. ~X(){ 4. cout<<"X"; 5. } 6. }; 7. class Y{ 8. public: 9. ~Y(){ 10. cout<<"Y"; 11. } 12. }; 13. class Z: public X{ 14. Y y; 15. public: 16. ~Z(){ 17. cout<<"Z"; 18. } 19. }; 20. 21. int main(int argv, char** argc){ 22. Z* z = new Z(); 23. delete z; 24. } |
(1) XYZ
(2) ZYX
(3) ZXY
(4) YXZ
Answer
Constructors
1. class X{ 2. public: 3. X(){ 4. cout<<"X"; 5. } 6. }; 7. class Y{ 8. public: 9. Y(){ 10. cout<<"Y"; 11. } 12. }; 13. class Z: public X{ 14. Y y; 15. public: 16. Z(){ 17. cout<<"Z"; 18. } 19. }; 20. 21. int main(int argv, char** argc){ 22. Z* z = new Z(); 23. delete z; 24. } |
(1) XYZ
(2) XZY
(3) XZ
(4) YXZ
Answer
Back C++ home | Home
8 comments:
On the question "What is the final value of 'v'?"
The correct answer is 35 !! 40 is WRONG answer.
Did you copy this, compile it and run it? If so and you got 35 can you tell me what compiler you used?
" What is the final value of 'v'? "
That question is wrong. The code
is an undefined behavior.
Specifically this part :
[code]
(m++)+(++m)
[/code]
Look up sequence point. Thus the
result will be different in
different compiler.
m++ and ++m have well defined behaviour in C++ and Java. If you compile the code using some compiler you bought from a 2$ shop, then you will have compiler dependent behaviour. Otherwise it's set in stone.
The virtual functions problem gives the wrong answer. The correct answer is "B and B", because "show()" is a virtual function that is called with late binding even from C's function members. The way to get (A) to execute would involve scope resolution operators (none in this example), or have objects of class A (none in this example), or make these functions called from A's destructor (again, missing here).
Tried it on G++ 4.0.2 - and sure enough, "B and B", with a minor warning about the lack of virtual destructors.
Cheers.
m++ and ++m are both well defined. But an expression containing both mm++ and ++m without a sequence point in between is undefined according to the standard just like the examples m=m++, and f(m)+f(m++). I suggest you check http://en.wikipedia.org/wiki/Sequence_point
Next to last question is wrong, it's B,B. On Line D, because of static linking, method on line 12 will get called, but inside it will still call B's "show" because it's a virtual function.
The question ++m and m++ is really compiler dependent. I have tried gnu c/c++ compiler, visual studio 2005 and 2008. The answers are not the same. So, This is a stupid question.
Post a Comment