{"id":37,"date":"2002-09-27T01:26:13","date_gmt":"2002-09-27T01:26:13","guid":{"rendered":"http:\/\/bitpost.com\/news\/?p=14"},"modified":"2008-07-24T23:53:47","modified_gmt":"2008-07-25T04:53:47","slug":"reuse-dammit","status":"publish","type":"post","link":"https:\/\/bitpost.com\/news\/2002\/reuse-dammit\/","title":{"rendered":"C++ Skills Interview Questions"},"content":{"rendered":"<li>ABB: &#8220;Interview these 12 people TODAY!&#8221;<\/li>\n<li>Me:  &#8220;OK&#8230;&#8221;, HOURS later&#8230; &#8220;I liked these 2, what&#8217;s next?&#8221;<\/li>\n<li>ABB: &#8220;Didn&#8217;t you hear?  Hiring freeze in effect for the next 12 months!&#8221;With all the non-productive interviews Zac and I have done here at ABB over the last few months, I&#8217;ve scrounged up a lame list <a href=\"http:\/\/www.cpuniverse.com\/newsite\/archives\/1999\/mar\/c++.html\">like this<\/a> at least two dozen times.  NO MORE! \ud83d\ude1b\n<p>And to guarantee this is the final time, I&#8217;m cutting and pasting it into the body of this article (hit &#8220;Read More&#8221; to see it)&#8230;<!--more--><\/p>\n<h2>The C++ Interview<\/h2>\n<h3>These 40 questions and answers will help you land the assignment<\/h3>\n<p><i>by Alex Bykov<\/i><br \/>\n<b>How do you rank your C++ skills on a scale of 1 to 10?<\/b><br \/>\nThis is often the first question you will hear on an interview for a<br \/>\nC++ contract. You will be tempted to rate yourself high, and you should.<br \/>\nThis is your chance to convince the client that you are just what he is<br \/>\nlooking for&#8211;an assertive and knowledgeable professional who will be productive<br \/>\neither working on a team or on your own. Naturally, though, you should<br \/>\nbe able to support the ranking you gave yourself by doing well on the<br \/>\ninterview. This article will help you prepare for your C++ interview.<\/p>\n<p>I put together a list of 40 questions that I have had to answer during<br \/>\nnumerous technical interviews in the past few years. You, too, will have<br \/>\nto answer at least some of them during an interview. Even if you use C++<br \/>\non a daily basis, it pays to go through the questions. Most of us, no<br \/>\nmatter how experienced, use only a segment of the language that we are<br \/>\nmost comfortable with. Brief answers are included, but you can find more<br \/>\ninformation in the <a xhref=\"http:\/\/bitpost.com\/news\/wp-admin\/post.php#sidebar\">references<\/a> listed.<br \/>\n<b>Q1.<\/b> <i><b>Is there anything you can do in C++ that you cannot<br \/>\ndo in C?<\/b><\/i><br \/>\n<b>A1.<\/b> No. There is nothing you can do in C++ that you cannot do<br \/>\nin C. After all you can write a C++ compiler in C.<br \/>\n<b>Q2.<\/b> <b><i>What is the difference between C++ structure and C++<br \/>\nclass?<\/i><\/b><br \/>\n<b>A2.<\/b> The default access level assigned to members of struct is<br \/>\npublic while the default access level assigned to a class is private.<\/p>\n<p><b>Q3.<\/b> <b><i>What is encapsulation?<\/i> <\/b><br \/>\n<b>A3.<\/b> Encapsulation is welding of code and data together into objects.<br \/>\n<b>Q4.<\/b> <b><i>What is inheritance?<\/i><\/b><br \/>\n<b>A4.<\/b> Inheritance is a mechanism through which a subclass inherits<br \/>\nthe properties and behavior of its superclass.<br \/>\n<b>Q5. <i>What is polymorphism?<\/i><\/b><br \/>\n<b>A5.<\/b> In Greek this means &quot;many shapes.&quot; As a consequence<br \/>\nof inheritance and virtual functions, a single task (for example, drawing<br \/>\na geometrical shape) can be implemented using the same name (like draw())<br \/>\nand implemented differently (via virtual functions) as each type in object<br \/>\nhierarchy requires(circle.draw() or rectangle.draw()). Later, when a polymorphic<br \/>\nobject (whose type is not known at compile time) executes the draw() virtual<br \/>\nfunction, the correct implementation is chosen and executed at run time.<\/p>\n<p><b>Q6. <i>What would you say if you saw &quot;delete this&quot; while<br \/>\nreviewing your peer&#8217;s code?<\/i><\/b><br \/>\n<b>A6.<\/b> You should never do this. Since compiler does not know whether<br \/>\nthe object was allocated on the stack or on the heap, &quot;delete this&quot;<br \/>\ncould cause a disaster.<br \/>\n<b>Q7. <i>What is the difference between public, protected, and private<br \/>\nmembers of a class?<\/i><\/b><br \/>\n<b>A7.<\/b> Private members are accessible only by members and friends<br \/>\nof the class. Protected members are accessible by members and friends<br \/>\nof the class and by members and friends of derived classes. Public members<br \/>\nare accessible by everyone.<br \/>\n<b>Q8. <i>What is the difference between non-virtual and virtual functions?<\/i><\/b><\/p>\n<p><b>A8.<\/b> The behavior of a non-virtual function is known at compile<br \/>\ntime while the behavior of a virtual function is not known until the run<br \/>\ntime.<br \/>\n<b>Q9. <i>What is a pure virtual function?<\/i><\/b><br \/>\n<b>A9.<\/b> &#8220;A pure virtual function is a function declared in a base<br \/>\nclass that has no definition relative to the base.&#8221;<br \/>\n<b>Q10. <i>What is an abstract base class?<\/i><\/b><br \/>\n<b>A10.<\/b> It is a class that has one or more pure virtual functions.<\/p>\n<p><b>Q11. <i>What is the difference between MyClass p; and MyClass p();?<\/i><\/b><\/p>\n<p><b>A11.<\/b> MyClass p; creates an instance of class MyClass by calling<br \/>\na constructor for MyClass. MyClass p(); declares function p which takes<br \/>\nno parameters and returns an object of class MyClass by value.<br \/>\n<b>Q12. <i>How do you know that your class needs a virtual destructor?<\/i><\/b><\/p>\n<p><b>A12.<\/b> If your class has at least one virtual function, you should<br \/>\nmake a destructor for this class virtual. This will allow you to delete<br \/>\na dynamic object through a pointer to a base class object. If the destructor<br \/>\nis non-virtual, then wrong destructor will be invoked during deletion<br \/>\nof the dynamic object.<br \/>\n<b>Q13. <i>Why were the templates introduced?<\/i><\/b><br \/>\n<b>A13.<\/b> Many data structures and algorithms can be defined independently<br \/>\nof the type of data they work with. You can increase the amount of shared<br \/>\ncode by separating data-dependent portions from data-independent portions,<br \/>\nand templates were introduced to help you do that.<br \/>\n<b>Q14. <i>What is a static member of a class?<\/i><\/b><br \/>\n<b>A14.<\/b> Static data members exist once for the entire class, as opposed<br \/>\nto non-static data members, which exist individually in each instance<br \/>\nof a class.<br \/>\n<b>Q15.<\/b> <b><i>What feature of C++ would you use if you wanted to<br \/>\ndesign a member function that guarantees to leave &quot;this\u00c3\u201c object unchanged?<\/i><\/b><\/p>\n<p><b>A15.<\/b> It is &quot;const&quot; as in: &quot;int MyFunc (int test)<br \/>\nconst;&quot;<br \/>\n<b>Q16. <i>Can you overload a function based only on whether a parameter<br \/>\nis a value or a reference?<\/i><\/b><br \/>\n<b>A16.<\/b> No. Passing by value and by reference looks identical to<br \/>\nthe caller.<br \/>\n<b>Q17. <i>What is the difference between function overloading and function<br \/>\noverriding?<\/i><\/b><br \/>\n<b>A17.<\/b> Overloading is a method that allows defining multiple member<br \/>\nfunctions with the same name but different signatures. The compiler will<br \/>\npick the correct function based on the signature. Overriding is a method<br \/>\nthat allows the derived class to redefine the behavior of member functions<br \/>\nwhich the derived class inherits from a base class. The signatures of<br \/>\nboth base class member function and derived class member function are<br \/>\nthe same; however, the implementation and, therefore, the behavior will<br \/>\ndiffer.<br \/>\n<b>Q18. <i>Can derived class override some but not all of a set of overloaded<br \/>\nvirtual member functions inherited from the base class?<\/i><\/b><br \/>\n<b>A18.<\/b> Compiler will allow this, but it is a bad practice since<br \/>\noverridden member functions will hide all of the inherited overloads from<br \/>\nthe base class. You should really override all of them.<br \/>\n<b>Q19. <i>What is the difference between assignment and initialization<br \/>\nin C++?<\/i><\/b><br \/>\n<b>A19.<\/b> Assignment changes the value of the object that has already<br \/>\nbeen constructed. Initialization constructs a new object and gives it<br \/>\na value at the same time.<br \/>\n<b>Q20. <i>When are copy constructors called?<\/i><\/b><br \/>\n<b>A20.<\/b> Copy constructors are called in three cases: when a function<br \/>\nreturns an object of that class by value, when the object of that class<br \/>\nis passed by value as an argument to a function, and, finally, when you<br \/>\nconstruct an object based on another object of the same class (Circle<br \/>\nc1=c2;).<br \/>\n<b>Q21. <i>Why do you have to provide your own copy constructor and assignment<br \/>\noperator for classes with dynamically allocated memory?<\/i> <\/b><br \/>\n<b>A21.<\/b> If you don&#8217;t, the compiler will supply and execute the default<br \/>\nconstructor and the assignment operator, but they will not do the job<br \/>\ncorrectly. The default assignment operator does memberwise assignment<br \/>\nand the default copy constructor does memberwise copy. In both cases you<br \/>\nwill only assign and manipulate pointers to dynamic memory, which will<br \/>\nlead to memory leaks and other abnormalities. You should write your own<br \/>\nassignment operator and copy constructor, which would copy the pointer<br \/>\nto memory so that each object has its own copy.<br \/>\n<b>Q22. <i>Does compiler guarantee that initializers will be executed<br \/>\nin the same order as they appear on the initialization list?<\/i><\/b><br \/>\n<b>A22.<\/b> No. C++ guarantees that base class subobjects and member<br \/>\nobjects will be destroyed in the opposite order from which they were constructed.<br \/>\nThis means that initializers are executed in the order, which supports<br \/>\nthe above-mentioned guarantee.<br \/>\n<b>Q23. <i>What is function&#8217;s signature?<\/i><\/b><br \/>\n<b>A23.<\/b> Function&#8217;s signature is its name plus the number and types<br \/>\nof the parameters it accepts.<br \/>\n<b>Q24. <i>What does extern &quot;C&quot; int func(int *, Foo) accomplish?<\/i><\/b><\/p>\n<p><b>A24.<\/b> It will turn off &quot;name mangling&quot; for this function<br \/>\nso that one can link to code compiled by C compiler.<br \/>\n<b>Q25. <i>Why do C++ compilers need name mangling?<\/i><\/b><br \/>\n<b>A25.<\/b> Name mangling is the rule according to which C++ changes<br \/>\nfunction&#8217;s name into function signature before passing that function to<br \/>\na linker. This is how the linker differentiates between different functions<br \/>\nwith the same name.<br \/>\n<b>Q26. <i>What is the difference between a pointer and a reference?<\/i><\/b><\/p>\n<p><b>A26.<\/b> A reference must always refer to some object and, therefore,<br \/>\nmust always be initialized; pointers do not have such restrictions. A<br \/>\npointer can be reassigned to point to different objects while a reference<br \/>\nalways refers to an object with which it was initialized.<br \/>\n<b>Q27.<\/b> <b><i>How can you access the static member of a class?<\/i><\/b><\/p>\n<p><b>A27.<\/b> <classname>&lt;ClassName&gt;::&lt;StaticMemberName&gt;.<br \/>\n<b>Q28. <i>How are prefix and postfix versions of operator++() differentiated?<\/i><br \/>\n<\/b><br \/>\n<b>A28.<\/b> The postfix version of operator++() has a dummy parameter<br \/>\nof type int. The prefix version does not have dummy parameter.<br \/>\n<b>Q29. <i>What functions does C++ silently write and call?<\/i><\/b><br \/>\n<b>A29.<\/b> Constructors, destructors, copy constructors, assignment<br \/>\noperators, and address-of operators.<br \/>\n<b>Q30. <i>What is the difference between new\/delete and malloc\/free?<\/i><\/b><br \/>\n<b>A30.<\/b> Malloc\/free do not know about constructors and destructors.<br \/>\nNew and delete create and destroy objects, while malloc and free allocate<br \/>\nand deallocate memory.<br \/>\n<b>Q31. <i>What is the difference between delete and delete[ ]?<\/i><\/b><\/p>\n<p><b>A31.<\/b> Delete deletes one object; delete[ ] deletes an array of<br \/>\nobjects.<br \/>\n<b>Q32. <i>Name two cases where you MUST use initialization list as opposed<br \/>\nto assignment in constructors.<\/i><\/b><br \/>\n<b>A32.<\/b> Both non-static const data members and reference data members<br \/>\ncannot be assigned values; instead, you should use initialization list<br \/>\nto initialize them.<br \/>\n<b>Q33. <i>What is the difference between const char *myPointer and char<br \/>\n*const myPointer?<\/i><\/b><br \/>\n<b>A33.<\/b> Const char *myPointer is a non constant pointer to constant<br \/>\ndata; while char *const myPointer is a constant pointer to non constant<br \/>\ndata.<br \/>\n<b>Q34. <i>Suppose that objects A, B, and C are instances of class MyClass<br \/>\n(MyClass A, B, C;). How should you design an assignment operator so that<br \/>\nthe &quot;A=B=C;&quot; statement would be allowed by a compiler but &quot;(A=B)=C;&quot;<br \/>\nwould not be allowed by a compiler?<\/i> <\/b><br \/>\n<b>A34.<\/b> Make operator=return a reference to a const object.<br \/>\n<b>Q35. <i>Is there any problem with the following: char *a=NULL; char&#038;<br \/>\np = *a;?<\/i> <\/b><br \/>\n<b>A35.<\/b> The result is undefined. You should never do this. A reference<br \/>\nmust always refer to some object.<br \/>\n<b>Q36. <i>Class B is derived from class A. Function f is A&#8217;s friend.<br \/>\nIs f B&#8217;s friend as well?<\/i><\/b><br \/>\n<b>A36.<\/b> No. Friendship cannot be inherited.<br \/>\n<b>Q37. <i>What issue do auto_ptr objects address?<\/i><\/b><br \/>\n<b>A37.<\/b> If you use auto_ptr objects you would not have to be concerned<br \/>\nwith heap objects not being deleted even if the exception is thrown.<br \/>\n<b>Q38. <i>What happens when a function throws an exception that was<br \/>\nnot specified by an exception specification for this function?<\/i><\/b><\/p>\n<p><b>A38.<\/b> Unexpected() is called, which, by default, will eventually<br \/>\ntrigger abort().<br \/>\n<b>Q39. <i>Why should you prefer throw\/catch mechanism to setjmp\/longjmp?<\/i><\/b><\/p>\n<p><b>A39.<\/b> The main problem with longjmp() is that it does not destroy<br \/>\nlocal objects properly.<br \/>\n<b>Q40. <i>Can you think of a situation where your program would crash<br \/>\nwithout reaching the breakpoint which you set at the beginning of main()?<\/i><\/b><br \/>\n<b>A40.<\/b> C++ allows for dynamic initialization of global variables<br \/>\nbefore main() is invoked. It is possible that initialization of global<br \/>\nwill invoke some function. If this function crashes the crash will occur<br \/>\nbefore main() is entered.<br \/>\nIf you feel comfortable answering these questions, then rest assured<br \/>\nthat your chances of impressing any interviewer are very high. Be prepared<br \/>\nto know basic computer science concepts such as data structures, search<br \/>\nand sort algorithms, basic database concepts, etc. The client&#8217;s needs<br \/>\nwill determine what particular branch of computer science you have to<br \/>\nbe familiar with, but you should always be ready to implement the stock,<br \/>\nthe queue, and the linked list data structures with either C or C++ programming<br \/>\nlanguages. And know how to write your own version of strcpy (string copy)<br \/>\nin C programming language since very often they ask you to do that.<\/li>\n","protected":false},"excerpt":{"rendered":"<br \/>\n<h2>The C++ Interview<\/h2>\n<h3>These 40 questions and answers will help you land the assignment<\/h3>\n<p><i>by Alex Bykov<\/i><\/p>\n<p><b>How do you rank your C++ skills on a scale of 1 to 10?<\/b> <\/p>\n<p>This is often the first question you will hear on an interview for a<br \/>\n        C++ contract. You will be tempted to rate yourself high, and you should.<br \/>\n        This is your chance to convince the client that you are just what he is<br \/>\n        looking for&#8211;an assertive and knowledgeable professional who will be productive<br \/>\n        either working on a team or on your own. Naturally, though, you should<br \/>\n        be able to support the ranking you gave yourself by doing well on the<br \/>\n        interview. This article will help you prepare for your C++ interview.\n      <\/p>\n<p>I put together a list of 40 questions that I have had to answer during<br \/>\n        numerous technical interviews in the past few years. You, too, will have<br \/>\n        to answer at least some of them during an interview. Even if you use C++<br \/>\n        on a daily basis, it pays to go through the questions. Most of us, no<br \/>\n        matter how experienced, use only a segment of the language that we are<br \/>\n        most comfortable with. Brief answers are included, but you can find more<br \/>\n        information in the <a href=\"#sidebar\">references<\/a> listed. <\/p>\n<p><b>Q1.<\/b> <i><b>Is there anything you can do in C++ that you cannot<br \/>\n        do in C?<\/b><\/i> <\/p>\n<p><b>A1.<\/b> No. There is nothing you can do in C++ that you cannot do<br \/>\n        in C. After all you can write a C++ compiler in C. <\/p>\n<p><b>Q2.<\/b> <b><i>What is the difference between C++ structure and C++<br \/>\n        class?<\/i><\/b> <\/p>\n<p><b>A2.<\/b> The default access level assigned to members of struct is<br \/>\n        public while the default access level assigned to a class is private.\n      <\/p>\n<p><b>Q3.<\/b> <b><i>What is encapsulation?<\/i> <\/b><\/p>\n<p><b>A3.<\/b> Encapsulation is welding of code and data together into objects.<\/p>\n<p> <b>Q4.<\/b> <b><i>What is inheritance?<\/i><\/b> <\/p>\n<p><b>A4.<\/b> Inheritance is a mechanism through which a subclass inherits<br \/>\n        the properties and behavior of its superclass. <\/p>\n<p><b>Q5. <i>What is polymorphism?<\/i><\/b> <\/p>\n<p><b>A5.<\/b> In Greek this means &quot;many shapes.&quot; As a consequence<br \/>\n        of inheritance and virtual functions, a single task (for example, drawing<br \/>\n        a geometrical shape) can be implemented using the same name (like draw())<br \/>\n        and implemented differently (via virtual functions) as each type in object<br \/>\n        hierarchy requires(circle.draw() or rectangle.draw()). Later, when a polymorphic<br \/>\n        object (whose type is not known at compile time) executes the draw() virtual<br \/>\n        function, the correct implementation is chosen and executed at run time.\n      <\/p>\n<p><b>Q6. <i>What would you say if you saw &quot;delete this&quot; while<br \/>\n        reviewing your peer&#8217;s code?<\/i><\/b> <\/p>\n<p><b>A6.<\/b> You should never do this. Since compiler does not know whether<br \/>\n        the object was allocated on the stack or on the heap, &quot;delete this&quot;<br \/>\n        could cause a disaster. <\/p>\n<p><b>Q7. <i>What is the difference between public, protected, and private<br \/>\n        members of a class?<\/i><\/b> <\/p>\n<p><b>A7.<\/b> Private members are accessible only by members and friends<br \/>\n        of the class. Protected members are accessible by members and friends<br \/>\n        of the class and by members and friends of derived classes. Public members<br \/>\n        are accessible by everyone. <\/p>\n<p><b>Q8. <i>What is the difference between non-virtual and virtual functions?<\/i><\/b>\n      <\/p>\n<p><b>A8.<\/b> The behavior of a non-virtual function is known at compile<br \/>\n        time while the behavior of a virtual function is not known until the run<br \/>\n        time. <\/p>\n<p><b>Q9. <i>What is a pure virtual function?<\/i><\/b> <\/p>\n<p><b>A9.<\/b> &#8220;A pure virtual function is a function declared in a base<br \/>\n        class that has no definition relative to the base.&#8221;<\/p>\n<p><b>Q10. <i>What is an abstract base class?<\/i><\/b> <\/p>\n<p><b>A10.<\/b> It is a class that has one or more pure virtual functions.\n      <\/p>\n<p><b>Q11. <i>What is the difference between MyClass p; and MyClass p();?<\/i><\/b>\n      <\/p>\n<p><b>A11.<\/b> MyClass p; creates an instance of class MyClass by calling<br \/>\n        a constructor for MyClass. MyClass p(); declares function p which takes<br \/>\n        no parameters and returns an object of class MyClass by value. <\/p>\n<p><b>Q12. <i>How do you know that your class needs a virtual destructor?<\/i><\/b>\n      <\/p>\n<p><b>A12.<\/b> If your class has at least one virtual function, you should<br \/>\n        make a destructor for this class virtual. This will allow you to delete<br \/>\n        a dynamic object through a pointer to a base class object. If the destructor<br \/>\n        is non-virtual, then wrong destructor will be invoked during deletion<br \/>\n        of the dynamic object. <\/p>\n<p><b>Q13. <i>Why were the templates introduced?<\/i><\/b> <\/p>\n<p><b>A13.<\/b> Many data structures and algorithms can be defined independently<br \/>\n        of the type of data they work with. You can increase the amount of shared<br \/>\n        code by separating data-dependent portions from data-independent portions,<br \/>\n        and templates were introduced to help you do that. <\/p>\n<p><b>Q14. <i>What is a static member of a class?<\/i><\/b> <\/p>\n<p><b>A14.<\/b> Static data members exist once for the entire class, as opposed<br \/>\n        to non-static data members, which exist individually in each instance<br \/>\n        of a class. <\/p>\n<p><b>Q15.<\/b> <b><i>What feature of C++ would you use if you wanted to<br \/>\n        design a member function that guarantees to leave &quot;this\u00c3\u0192\u00e2\u20ac\u0153 object unchanged?<\/i><\/b>\n      <\/p>\n<p><b>A15.<\/b> It is &quot;const&quot; as in: &quot;int MyFunc (int test)<br \/>\n        const;&quot; <\/p>\n<p><b>Q16. <i>Can you overload a function based only on whether a parameter<br \/>\n        is a value or a reference?<\/i><\/b> <\/p>\n<p><b>A16.<\/b> No. Passing by value and by reference looks identical to<br \/>\n        the caller. <\/p>\n<p><b>Q17. <i>What is the difference between function overloading and function<br \/>\n        overriding?<\/i><\/b> <\/p>\n<p><b>A17.<\/b> Overloading is a method that allows defining multiple member<br \/>\n        functions with the same name but different signatures. The compiler will<br \/>\n        pick the correct function based on the signature. Overriding is a method<br \/>\n        that allows the derived class to redefine the behavior of member functions<br \/>\n        which the derived class inherits from a base class. The signatures of<br \/>\n        both base class member function and derived class member function are<br \/>\n        the same; however, the implementation and, therefore, the behavior will<br \/>\n        differ. <\/p>\n<p><b>Q18. <i>Can derived class override some but not all of a set of overloaded<br \/>\n        virtual member functions inherited from the base class?<\/i><\/b> <\/p>\n<p><b>A18.<\/b> Compiler will allow this, but it is a bad practice since<br \/>\n        overridden member functions will hide all of the inherited overloads from<br \/>\n        the base class. You should really override all of them. <\/p>\n<p><b>Q19. <i>What is the difference between assignment and initialization<br \/>\n        in C++?<\/i><\/b> <\/p>\n<p><b>A19.<\/b> Assignment changes the value of the object that has already<br \/>\n        been constructed. Initialization constructs a new object and gives it<br \/>\n        a value at the same time. <\/p>\n<p><b>Q20. <i>When are copy constructors called?<\/i><\/b> <\/p>\n<p><b>A20.<\/b> Copy constructors are called in three cases: when a function<br \/>\n        returns an object of that class by value, when the object of that class<br \/>\n        is passed by value as an argument to a function, and, finally, when you<br \/>\n        construct an object based on another object of the same class (Circle<br \/>\n        c1=c2;). <\/p>\n<p><b>Q21. <i>Why do you have to provide your own copy constructor and assignment<br \/>\n        operator for classes with dynamically allocated memory?<\/i> <\/b><\/p>\n<p><b>A21.<\/b> If you don&#8217;t, the compiler will supply and execute the default<br \/>\n        constructor and the assignment operator, but they will not do the job<br \/>\n        correctly. The default assignment operator does memberwise assignment<br \/>\n        and the default copy constructor does memberwise copy. In both cases you<br \/>\n        will only assign and manipulate pointers to dynamic memory, which will<br \/>\n        lead to memory leaks and other abnormalities. You should write your own<br \/>\n        assignment operator and copy constructor, which would copy the pointer<br \/>\n        to memory so that each object has its own copy. <\/p>\n<p><b>Q22. <i>Does compiler guarantee that initializers will be executed<br \/>\n        in the same order as they appear on the initialization list?<\/i><\/b> <\/p>\n<p><b>A22.<\/b> No. C++ guarantees that base class subobjects and member<br \/>\n        objects will be destroyed in the opposite order from which they were constructed.<br \/>\n        This means that initializers are executed in the order, which supports<br \/>\n        the above-mentioned guarantee. <\/p>\n<p><b>Q23. <i>What is function&#8217;s signature?<\/i><\/b> <\/p>\n<p><b>A23.<\/b> Function&#8217;s signature is its name plus the number and types<br \/>\n        of the parameters it accepts.<\/p>\n<p> <b>Q24. <i>What does extern &quot;C&quot; int func(int *, Foo) accomplish?<\/i><\/b>\n      <\/p>\n<p><b>A24.<\/b> It will turn off &quot;name mangling&quot; for this function<br \/>\n        so that one can link to code compiled by C compiler. <\/p>\n<p><b>Q25. <i>Why do C++ compilers need name mangling?<\/i><\/b> <\/p>\n<p><b>A25.<\/b> Name mangling is the rule according to which C++ changes<br \/>\n        function&#8217;s name into function signature before passing that function to<br \/>\n        a linker. This is how the linker differentiates between different functions<br \/>\n        with the same name. <\/p>\n<p><b>Q26. <i>What is the difference between a pointer and a reference?<\/i><\/b>\n      <\/p>\n<p><b>A26.<\/b> A reference must always refer to some object and, therefore,<br \/>\n        must always be initialized; pointers do not have such restrictions. A<br \/>\n        pointer can be reassigned to point to different objects while a reference<br \/>\n        always refers to an object with which it was initialized.<\/p>\n<p> <b>Q27.<\/b> <b><i>How can you access the static member of a class?<\/i><\/b>\n      <\/p>\n<p><b>A27.<\/b> <classname>&lt;ClassName&gt;::&lt;StaticMemberName&gt;. <\/p>\n<p><b>Q28. <i>How are prefix and postfix versions of operator++() differentiated?<\/i><br \/>\n        <\/b> <\/p>\n<p><b>A28.<\/b> The postfix version of operator++() has a dummy parameter<br \/>\n        of type int. The prefix version does not have dummy parameter. <\/p>\n<p><b>Q29. <i>What functions does C++ silently write and call?<\/i><\/b> <\/p>\n<p><b>A29.<\/b> Constructors, destructors, copy constructors, assignment<br \/>\n        operators, and address-of operators. <\/p>\n<p><b>Q30. <i>What is the difference between new\/delete and malloc\/free?<\/i><\/b><\/p>\n<p><b>A30.<\/b> Malloc\/free do not know about constructors and destructors.<br \/>\n        New and delete create and destroy objects, while malloc and free allocate<br \/>\n        and deallocate memory. <\/p>\n<p><b>Q31. <i>What is the difference between delete and delete[ ]?<\/i><\/b>\n      <\/p>\n<p><b>A31.<\/b> Delete deletes one object; delete[ ] deletes an array of<br \/>\n        objects. <\/p>\n<p><b>Q32. <i>Name two cases where you MUST use initialization list as opposed<br \/>\n        to assignment in constructors.<\/i><\/b> <\/p>\n<p><b>A32.<\/b> Both non-static const data members and reference data members<br \/>\n        cannot be assigned values; instead, you should use initialization list<br \/>\n        to initialize them. <\/p>\n<p><b>Q33. <i>What is the difference between const char *myPointer and char<br \/>\n        *const myPointer?<\/i><\/b> <\/p>\n<p><b>A33.<\/b> Const char *myPointer is a non constant pointer to constant<br \/>\n        data; while char *const myPointer is a constant pointer to non constant<br \/>\n        data. <\/p>\n<p><b>Q34. <i>Suppose that objects A, B, and C are instances of class MyClass<br \/>\n        (MyClass A, B, C;). How should you design an assignment operator so that<br \/>\n        the &quot;A=B=C;&quot; statement would be allowed by a compiler but &quot;(A=B)=C;&quot;<br \/>\n        would not be allowed by a compiler?<\/i> <\/b><\/p>\n<p><b>A34.<\/b> Make operator=return a reference to a const object. <\/p>\n<p><b>Q35. <i>Is there any problem with the following: char *a=NULL; char&#038;<br \/>\n        p = *a;?<\/i> <\/b><\/p>\n<p><b>A35.<\/b> The result is undefined. You should never do this. A reference<br \/>\n        must always refer to some object. <\/p>\n<p><b>Q36. <i>Class B is derived from class A. Function f is A&#8217;s friend.<br \/>\n        Is f B&#8217;s friend as well?<\/i><\/b> <\/p>\n<p><b>A36.<\/b> No. Friendship cannot be inherited.<\/p>\n<p> <b>Q37. <i>What issue do auto_ptr objects address?<\/i><\/b> <\/p>\n<p><b>A37.<\/b> If you use auto_ptr objects you would not have to be concerned<br \/>\n        with heap objects not being deleted even if the exception is thrown. <\/p>\n<p><b>Q38. <i>What happens when a function throws an exception that was<br \/>\n        not specified by an exception specification for this function?<\/i><\/b>\n      <\/p>\n<p><b>A38.<\/b> Unexpected() is called, which, by default, will eventually<br \/>\n        trigger abort(). <\/p>\n<p><b>Q39. <i>Why should you prefer throw\/catch mechanism to setjmp\/longjmp?<\/i><\/b>\n      <\/p>\n<p><b>A39.<\/b> The main problem with longjmp() is that it does not destroy<br \/>\n        local objects properly. <\/p>\n<p><b>Q40. <i>Can you think of a situation where your program would crash<br \/>\n        without reaching the breakpoint which you set at the beginning of main()?<\/i><\/b><\/p>\n<p> <b>A40.<\/b> C++ allows for dynamic initialization of global variables<br \/>\n        before main() is invoked. It is possible that initialization of global<br \/>\n        will invoke some function. If this function crashes the crash will occur<br \/>\n        before main() is entered. <\/p>\n<p>If you feel comfortable answering these questions, then rest assured<br \/>\n        that your chances of impressing any interviewer are very high. Be prepared<br \/>\n        to know basic computer science concepts such as data structures, search<br \/>\n        and sort algorithms, basic database concepts, etc. The client&#8217;s needs<br \/>\n        will determine what particular branch of computer science you have to<br \/>\n        be familiar with, but you should always be ready to implement the stock,<br \/>\n        the queue, and the linked list data structures with either C or C++ programming<br \/>\n        languages. And know how to write your own version of strcpy (string copy)<br \/>\n        in C programming language since very often they ask you to do that. <\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"enabled":false},"version":2}},"categories":[10],"tags":[],"class_list":["post-37","post","type-post","status-publish","format-standard","hentry","category-tricks-tips-tools"],"aioseo_notices":[],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p9M11L-B","jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/bitpost.com\/news\/wp-json\/wp\/v2\/posts\/37","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/bitpost.com\/news\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bitpost.com\/news\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bitpost.com\/news\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/bitpost.com\/news\/wp-json\/wp\/v2\/comments?post=37"}],"version-history":[{"count":2,"href":"https:\/\/bitpost.com\/news\/wp-json\/wp\/v2\/posts\/37\/revisions"}],"predecessor-version":[{"id":290,"href":"https:\/\/bitpost.com\/news\/wp-json\/wp\/v2\/posts\/37\/revisions\/290"}],"wp:attachment":[{"href":"https:\/\/bitpost.com\/news\/wp-json\/wp\/v2\/media?parent=37"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bitpost.com\/news\/wp-json\/wp\/v2\/categories?post=37"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bitpost.com\/news\/wp-json\/wp\/v2\/tags?post=37"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}