{"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_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_feature_clip_id":0,"_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,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[10],"tags":[],"class_list":["post-37","post","type-post","status-publish","format-standard","hentry","category-tricks-tips-tools"],"aioseo_notices":[],"aioseo_head":"\n\t\t<!-- All in One SEO 4.9.9 - aioseo.com -->\n\t<meta name=\"description\" content=\"The C++ Interview These 40 questions and answers will help you land the assignment by Alex Bykov How do you rank your C++ skills on a scale of 1 to 10? This is often the first question you will hear on an interview for a C++ contract. You will be tempted to rate yourself high, and you should. This is your chance to convince the client that you are just what he is looking for--an assertive and knowledgeable professional who will be productive either working on a team or on your own. Naturally, though, you should be able to support the ranking you gave yourself by doing well on the interview. This article will help you prepare for your C++ interview. I put together a list of 40 questions that I have had to answer during numerous technical interviews in the past few years. You, too, will have to answer at least some of them during an interview. Even if you use C++ on a daily basis, it pays to go through the questions. Most of us, no matter how experienced, use only a segment of the language that we are most comfortable with. Brief answers are included, but you can find more information in the references listed. Q1. Is there anything you can do in C++ that you cannot do in C? A1. No. There is nothing you can do in C++ that you cannot do in C. After all you can write a C++ compiler in C. Q2. What is the difference between C++ structure and C++ class? A2. The default access level assigned to members of struct is public while the default access level assigned to a class is private. Q3. What is encapsulation? A3. Encapsulation is welding of code and data together into objects. Q4. What is inheritance? A4. Inheritance is a mechanism through which a subclass inherits the properties and behavior of its superclass. Q5. What is polymorphism? A5. In Greek this means &quot;many shapes.&quot; As a consequence of inheritance and virtual functions, a single task (for example, drawing a geometrical shape) can be implemented using the same name (like draw()) and implemented differently (via virtual functions) as each type in object hierarchy requires(circle.draw() or rectangle.draw()). Later, when a polymorphic object (whose type is not known at compile time) executes the draw() virtual function, the correct implementation is chosen and executed at run time. Q6. What would you say if you saw &quot;delete this&quot; while reviewing your peer&#039;s code? A6. You should never do this. Since compiler does not know whether the object was allocated on the stack or on the heap, &quot;delete this&quot; could cause a disaster. Q7. What is the difference between public, protected, and private members of a class? A7. Private members are accessible only by members and friends of the class. Protected members are accessible by members and friends of the class and by members and friends of derived classes. Public members are accessible by everyone. Q8. What is the difference between non-virtual and virtual functions? A8. The behavior of a non-virtual function is known at compile time while the behavior of a virtual function is not known until the run time. Q9. What is a pure virtual function? A9. &quot;A pure virtual function is a function declared in a base class that has no definition relative to the base.&quot; Q10. What is an abstract base class? A10. It is a class that has one or more pure virtual functions. Q11. What is the difference between MyClass p; and MyClass p();? A11. MyClass p; creates an instance of class MyClass by calling a constructor for MyClass. MyClass p(); declares function p which takes no parameters and returns an object of class MyClass by value. Q12. How do you know that your class needs a virtual destructor? A12. If your class has at least one virtual function, you should make a destructor for this class virtual. This will allow you to delete a dynamic object through a pointer to a base class object. If the destructor is non-virtual, then wrong destructor will be invoked during deletion of the dynamic object. Q13. Why were the templates introduced? A13. Many data structures and algorithms can be defined independently of the type of data they work with. You can increase the amount of shared code by separating data-dependent portions from data-independent portions, and templates were introduced to help you do that. Q14. What is a static member of a class? A14. Static data members exist once for the entire class, as opposed to non-static data members, which exist individually in each instance of a class. Q15. What feature of C++ would you use if you wanted to design a member function that guarantees to leave &quot;this\u00c3\u0192\u00e2\u20ac\u0153 object unchanged? A15. It is &quot;const&quot; as in: &quot;int MyFunc (int test) const;&quot; Q16. Can you overload a function based only on whether a parameter is a value or a reference? A16. No. Passing by value and by reference looks identical to the caller. Q17. What is the difference between function overloading and function overriding? A17. Overloading is a method that allows defining multiple member functions with the same name but different signatures. The compiler will pick the correct function based on the signature. Overriding is a method that allows the derived class to redefine the behavior of member functions which the derived class inherits from a base class. The signatures of both base class member function and derived class member function are the same; however, the implementation and, therefore, the behavior will differ. Q18. Can derived class override some but not all of a set of overloaded virtual member functions inherited from the base class? A18. Compiler will allow this, but it is a bad practice since overridden member functions will hide all of the inherited overloads from the base class. You should really override all of them. Q19. What is the difference between assignment and initialization in C++? A19. Assignment changes the value of the object that has already been constructed. Initialization constructs a new object and gives it a value at the same time. Q20. When are copy constructors called? A20. Copy constructors are called in three cases: when a function returns an object of that class by value, when the object of that class is passed by value as an argument to a function, and, finally, when you construct an object based on another object of the same class (Circle c1=c2;). Q21. Why do you have to provide your own copy constructor and assignment operator for classes with dynamically allocated memory? A21. If you don&#039;t, the compiler will supply and execute the default constructor and the assignment operator, but they will not do the job correctly. The default assignment operator does memberwise assignment and the default copy constructor does memberwise copy. In both cases you will only assign and manipulate pointers to dynamic memory, which will lead to memory leaks and other abnormalities. You should write your own assignment operator and copy constructor, which would copy the pointer to memory so that each object has its own copy. Q22. Does compiler guarantee that initializers will be executed in the same order as they appear on the initialization list? A22. No. C++ guarantees that base class subobjects and member objects will be destroyed in the opposite order from which they were constructed. This means that initializers are executed in the order, which supports the above-mentioned guarantee. Q23. What is function&#039;s signature? A23. Function&#039;s signature is its name plus the number and types of the parameters it accepts. Q24. What does extern &quot;C&quot; int func(int *, Foo) accomplish? A24. It will turn off &quot;name mangling&quot; for this function so that one can link to code compiled by C compiler. Q25. Why do C++ compilers need name mangling? A25. Name mangling is the rule according to which C++ changes function&#039;s name into function signature before passing that function to a linker. This is how the linker differentiates between different functions with the same name. Q26. What is the difference between a pointer and a reference? A26. A reference must always refer to some object and, therefore, must always be initialized; pointers do not have such restrictions. A pointer can be reassigned to point to different objects while a reference always refers to an object with which it was initialized. Q27. How can you access the static member of a class? A27. ::. Q28. How are prefix and postfix versions of operator++() differentiated? A28. The postfix version of operator++() has a dummy parameter of type int. The prefix version does not have dummy parameter. Q29. What functions does C++ silently write and call? A29. Constructors, destructors, copy constructors, assignment operators, and address-of operators. Q30. What is the difference between new\/delete and malloc\/free? A30. Malloc\/free do not know about constructors and destructors. New and delete create and destroy objects, while malloc and free allocate and deallocate memory. Q31. What is the difference between delete and delete[ ]? A31. Delete deletes one object; delete[ ] deletes an array of objects. Q32. Name two cases where you MUST use initialization list as opposed to assignment in constructors. A32. Both non-static const data members and reference data members cannot be assigned values; instead, you should use initialization list to initialize them. Q33. What is the difference between const char *myPointer and char *const myPointer? A33. Const char *myPointer is a non constant pointer to constant data; while char *const myPointer is a constant pointer to non constant data. Q34. Suppose that objects A, B, and C are instances of class MyClass (MyClass A, B, C;). How should you design an assignment operator so that the &quot;A=B=C;&quot; statement would be allowed by a compiler but &quot;(A=B)=C;&quot; would not be allowed by a compiler? A34. Make operator=return a reference to a const object. Q35. Is there any problem with the following: char *a=NULL; char&amp; p = *a;? A35. The result is undefined. You should never do this. A reference must always refer to some object. Q36. Class B is derived from class A. Function f is A&#039;s friend. Is f B&#039;s friend as well? A36. No. Friendship cannot be inherited. Q37. What issue do auto_ptr objects address? A37. If you use auto_ptr objects you would not have to be concerned with heap objects not being deleted even if the exception is thrown. Q38. What happens when a function throws an exception that was not specified by an exception specification for this function? A38. Unexpected() is called, which, by default, will eventually trigger abort(). Q39. Why should you prefer throw\/catch mechanism to setjmp\/longjmp? A39. The main problem with longjmp() is that it does not destroy local objects properly. Q40. Can you think of a situation where your program would crash without reaching the breakpoint which you set at the beginning of main()? A40. C++ allows for dynamic initialization of global variables before main() is invoked. It is possible that initialization of global will invoke some function. If this function crashes the crash will occur before main() is entered. If you feel comfortable answering these questions, then rest assured that your chances of impressing any interviewer are very high. Be prepared to know basic computer science concepts such as data structures, search and sort algorithms, basic database concepts, etc. The client&#039;s needs will determine what particular branch of computer science you have to be familiar with, but you should always be ready to implement the stock, the queue, and the linked list data structures with either C or C++ programming languages. And know how to write your own version of strcpy (string copy) in C programming language since very often they ask you to do that.\" \/>\n\t<meta name=\"robots\" content=\"max-image-preview:large\" \/>\n\t<meta name=\"author\" content=\"m\"\/>\n\t<meta name=\"keywords\" content=\"tricks tips tools\" \/>\n\t<link rel=\"canonical\" href=\"https:\/\/bitpost.com\/news\/2002\/reuse-dammit\/\" \/>\n\t<meta name=\"generator\" content=\"All in One SEO (AIOSEO) 4.9.9\" \/>\n\t\t<meta property=\"og:locale\" content=\"en_US\" \/>\n\t\t<meta property=\"og:site_name\" content=\"bitpost.com\/news | Note to self\" \/>\n\t\t<meta property=\"og:type\" content=\"article\" \/>\n\t\t<meta property=\"og:title\" content=\"C++ Skills Interview Questions | bitpost.com\/news\" \/>\n\t\t<meta property=\"og:description\" content=\"The C++ Interview These 40 questions and answers will help you land the assignment by Alex Bykov How do you rank your C++ skills on a scale of 1 to 10? This is often the first question you will hear on an interview for a C++ contract. You will be tempted to rate yourself high, and you should. This is your chance to convince the client that you are just what he is looking for--an assertive and knowledgeable professional who will be productive either working on a team or on your own. Naturally, though, you should be able to support the ranking you gave yourself by doing well on the interview. This article will help you prepare for your C++ interview. I put together a list of 40 questions that I have had to answer during numerous technical interviews in the past few years. You, too, will have to answer at least some of them during an interview. Even if you use C++ on a daily basis, it pays to go through the questions. Most of us, no matter how experienced, use only a segment of the language that we are most comfortable with. Brief answers are included, but you can find more information in the references listed. Q1. Is there anything you can do in C++ that you cannot do in C? A1. No. There is nothing you can do in C++ that you cannot do in C. After all you can write a C++ compiler in C. Q2. What is the difference between C++ structure and C++ class? A2. The default access level assigned to members of struct is public while the default access level assigned to a class is private. Q3. What is encapsulation? A3. Encapsulation is welding of code and data together into objects. Q4. What is inheritance? A4. Inheritance is a mechanism through which a subclass inherits the properties and behavior of its superclass. Q5. What is polymorphism? A5. In Greek this means &quot;many shapes.&quot; As a consequence of inheritance and virtual functions, a single task (for example, drawing a geometrical shape) can be implemented using the same name (like draw()) and implemented differently (via virtual functions) as each type in object hierarchy requires(circle.draw() or rectangle.draw()). Later, when a polymorphic object (whose type is not known at compile time) executes the draw() virtual function, the correct implementation is chosen and executed at run time. Q6. What would you say if you saw &quot;delete this&quot; while reviewing your peer&#039;s code? A6. You should never do this. Since compiler does not know whether the object was allocated on the stack or on the heap, &quot;delete this&quot; could cause a disaster. Q7. What is the difference between public, protected, and private members of a class? A7. Private members are accessible only by members and friends of the class. Protected members are accessible by members and friends of the class and by members and friends of derived classes. Public members are accessible by everyone. Q8. What is the difference between non-virtual and virtual functions? A8. The behavior of a non-virtual function is known at compile time while the behavior of a virtual function is not known until the run time. Q9. What is a pure virtual function? A9. &quot;A pure virtual function is a function declared in a base class that has no definition relative to the base.&quot; Q10. What is an abstract base class? A10. It is a class that has one or more pure virtual functions. Q11. What is the difference between MyClass p; and MyClass p();? A11. MyClass p; creates an instance of class MyClass by calling a constructor for MyClass. MyClass p(); declares function p which takes no parameters and returns an object of class MyClass by value. Q12. How do you know that your class needs a virtual destructor? A12. If your class has at least one virtual function, you should make a destructor for this class virtual. This will allow you to delete a dynamic object through a pointer to a base class object. If the destructor is non-virtual, then wrong destructor will be invoked during deletion of the dynamic object. Q13. Why were the templates introduced? A13. Many data structures and algorithms can be defined independently of the type of data they work with. You can increase the amount of shared code by separating data-dependent portions from data-independent portions, and templates were introduced to help you do that. Q14. What is a static member of a class? A14. Static data members exist once for the entire class, as opposed to non-static data members, which exist individually in each instance of a class. Q15. What feature of C++ would you use if you wanted to design a member function that guarantees to leave &quot;this\u00c3\u0192\u00e2\u20ac\u0153 object unchanged? A15. It is &quot;const&quot; as in: &quot;int MyFunc (int test) const;&quot; Q16. Can you overload a function based only on whether a parameter is a value or a reference? A16. No. Passing by value and by reference looks identical to the caller. Q17. What is the difference between function overloading and function overriding? A17. Overloading is a method that allows defining multiple member functions with the same name but different signatures. The compiler will pick the correct function based on the signature. Overriding is a method that allows the derived class to redefine the behavior of member functions which the derived class inherits from a base class. The signatures of both base class member function and derived class member function are the same; however, the implementation and, therefore, the behavior will differ. Q18. Can derived class override some but not all of a set of overloaded virtual member functions inherited from the base class? A18. Compiler will allow this, but it is a bad practice since overridden member functions will hide all of the inherited overloads from the base class. You should really override all of them. Q19. What is the difference between assignment and initialization in C++? A19. Assignment changes the value of the object that has already been constructed. Initialization constructs a new object and gives it a value at the same time. Q20. When are copy constructors called? A20. Copy constructors are called in three cases: when a function returns an object of that class by value, when the object of that class is passed by value as an argument to a function, and, finally, when you construct an object based on another object of the same class (Circle c1=c2;). Q21. Why do you have to provide your own copy constructor and assignment operator for classes with dynamically allocated memory? A21. If you don&#039;t, the compiler will supply and execute the default constructor and the assignment operator, but they will not do the job correctly. The default assignment operator does memberwise assignment and the default copy constructor does memberwise copy. In both cases you will only assign and manipulate pointers to dynamic memory, which will lead to memory leaks and other abnormalities. You should write your own assignment operator and copy constructor, which would copy the pointer to memory so that each object has its own copy. Q22. Does compiler guarantee that initializers will be executed in the same order as they appear on the initialization list? A22. No. C++ guarantees that base class subobjects and member objects will be destroyed in the opposite order from which they were constructed. This means that initializers are executed in the order, which supports the above-mentioned guarantee. Q23. What is function&#039;s signature? A23. Function&#039;s signature is its name plus the number and types of the parameters it accepts. Q24. What does extern &quot;C&quot; int func(int *, Foo) accomplish? A24. It will turn off &quot;name mangling&quot; for this function so that one can link to code compiled by C compiler. Q25. Why do C++ compilers need name mangling? A25. Name mangling is the rule according to which C++ changes function&#039;s name into function signature before passing that function to a linker. This is how the linker differentiates between different functions with the same name. Q26. What is the difference between a pointer and a reference? A26. A reference must always refer to some object and, therefore, must always be initialized; pointers do not have such restrictions. A pointer can be reassigned to point to different objects while a reference always refers to an object with which it was initialized. Q27. How can you access the static member of a class? A27. ::. Q28. How are prefix and postfix versions of operator++() differentiated? A28. The postfix version of operator++() has a dummy parameter of type int. The prefix version does not have dummy parameter. Q29. What functions does C++ silently write and call? A29. Constructors, destructors, copy constructors, assignment operators, and address-of operators. Q30. What is the difference between new\/delete and malloc\/free? A30. Malloc\/free do not know about constructors and destructors. New and delete create and destroy objects, while malloc and free allocate and deallocate memory. Q31. What is the difference between delete and delete[ ]? A31. Delete deletes one object; delete[ ] deletes an array of objects. Q32. Name two cases where you MUST use initialization list as opposed to assignment in constructors. A32. Both non-static const data members and reference data members cannot be assigned values; instead, you should use initialization list to initialize them. Q33. What is the difference between const char *myPointer and char *const myPointer? A33. Const char *myPointer is a non constant pointer to constant data; while char *const myPointer is a constant pointer to non constant data. Q34. Suppose that objects A, B, and C are instances of class MyClass (MyClass A, B, C;). How should you design an assignment operator so that the &quot;A=B=C;&quot; statement would be allowed by a compiler but &quot;(A=B)=C;&quot; would not be allowed by a compiler? A34. Make operator=return a reference to a const object. Q35. Is there any problem with the following: char *a=NULL; char&amp; p = *a;? A35. The result is undefined. You should never do this. A reference must always refer to some object. Q36. Class B is derived from class A. Function f is A&#039;s friend. Is f B&#039;s friend as well? A36. No. Friendship cannot be inherited. Q37. What issue do auto_ptr objects address? A37. If you use auto_ptr objects you would not have to be concerned with heap objects not being deleted even if the exception is thrown. Q38. What happens when a function throws an exception that was not specified by an exception specification for this function? A38. Unexpected() is called, which, by default, will eventually trigger abort(). Q39. Why should you prefer throw\/catch mechanism to setjmp\/longjmp? A39. The main problem with longjmp() is that it does not destroy local objects properly. Q40. Can you think of a situation where your program would crash without reaching the breakpoint which you set at the beginning of main()? A40. C++ allows for dynamic initialization of global variables before main() is invoked. It is possible that initialization of global will invoke some function. If this function crashes the crash will occur before main() is entered. If you feel comfortable answering these questions, then rest assured that your chances of impressing any interviewer are very high. Be prepared to know basic computer science concepts such as data structures, search and sort algorithms, basic database concepts, etc. The client&#039;s needs will determine what particular branch of computer science you have to be familiar with, but you should always be ready to implement the stock, the queue, and the linked list data structures with either C or C++ programming languages. And know how to write your own version of strcpy (string copy) in C programming language since very often they ask you to do that.\" \/>\n\t\t<meta property=\"og:url\" content=\"https:\/\/bitpost.com\/news\/2002\/reuse-dammit\/\" \/>\n\t\t<meta property=\"article:published_time\" content=\"2002-09-27T01:26:13+00:00\" \/>\n\t\t<meta property=\"article:modified_time\" content=\"2008-07-25T04:53:47+00:00\" \/>\n\t\t<meta name=\"twitter:card\" content=\"summary\" \/>\n\t\t<meta name=\"twitter:title\" content=\"C++ Skills Interview Questions | bitpost.com\/news\" \/>\n\t\t<meta name=\"twitter:description\" content=\"The C++ Interview These 40 questions and answers will help you land the assignment by Alex Bykov How do you rank your C++ skills on a scale of 1 to 10? This is often the first question you will hear on an interview for a C++ contract. You will be tempted to rate yourself high, and you should. This is your chance to convince the client that you are just what he is looking for--an assertive and knowledgeable professional who will be productive either working on a team or on your own. Naturally, though, you should be able to support the ranking you gave yourself by doing well on the interview. This article will help you prepare for your C++ interview. I put together a list of 40 questions that I have had to answer during numerous technical interviews in the past few years. You, too, will have to answer at least some of them during an interview. Even if you use C++ on a daily basis, it pays to go through the questions. Most of us, no matter how experienced, use only a segment of the language that we are most comfortable with. Brief answers are included, but you can find more information in the references listed. Q1. Is there anything you can do in C++ that you cannot do in C? A1. No. There is nothing you can do in C++ that you cannot do in C. After all you can write a C++ compiler in C. Q2. What is the difference between C++ structure and C++ class? A2. The default access level assigned to members of struct is public while the default access level assigned to a class is private. Q3. What is encapsulation? A3. Encapsulation is welding of code and data together into objects. Q4. What is inheritance? A4. Inheritance is a mechanism through which a subclass inherits the properties and behavior of its superclass. Q5. What is polymorphism? A5. In Greek this means &quot;many shapes.&quot; As a consequence of inheritance and virtual functions, a single task (for example, drawing a geometrical shape) can be implemented using the same name (like draw()) and implemented differently (via virtual functions) as each type in object hierarchy requires(circle.draw() or rectangle.draw()). Later, when a polymorphic object (whose type is not known at compile time) executes the draw() virtual function, the correct implementation is chosen and executed at run time. Q6. What would you say if you saw &quot;delete this&quot; while reviewing your peer&#039;s code? A6. You should never do this. Since compiler does not know whether the object was allocated on the stack or on the heap, &quot;delete this&quot; could cause a disaster. Q7. What is the difference between public, protected, and private members of a class? A7. Private members are accessible only by members and friends of the class. Protected members are accessible by members and friends of the class and by members and friends of derived classes. Public members are accessible by everyone. Q8. What is the difference between non-virtual and virtual functions? A8. The behavior of a non-virtual function is known at compile time while the behavior of a virtual function is not known until the run time. Q9. What is a pure virtual function? A9. &quot;A pure virtual function is a function declared in a base class that has no definition relative to the base.&quot; Q10. What is an abstract base class? A10. It is a class that has one or more pure virtual functions. Q11. What is the difference between MyClass p; and MyClass p();? A11. MyClass p; creates an instance of class MyClass by calling a constructor for MyClass. MyClass p(); declares function p which takes no parameters and returns an object of class MyClass by value. Q12. How do you know that your class needs a virtual destructor? A12. If your class has at least one virtual function, you should make a destructor for this class virtual. This will allow you to delete a dynamic object through a pointer to a base class object. If the destructor is non-virtual, then wrong destructor will be invoked during deletion of the dynamic object. Q13. Why were the templates introduced? A13. Many data structures and algorithms can be defined independently of the type of data they work with. You can increase the amount of shared code by separating data-dependent portions from data-independent portions, and templates were introduced to help you do that. Q14. What is a static member of a class? A14. Static data members exist once for the entire class, as opposed to non-static data members, which exist individually in each instance of a class. Q15. What feature of C++ would you use if you wanted to design a member function that guarantees to leave &quot;this\u00c3\u0192\u00e2\u20ac\u0153 object unchanged? A15. It is &quot;const&quot; as in: &quot;int MyFunc (int test) const;&quot; Q16. Can you overload a function based only on whether a parameter is a value or a reference? A16. No. Passing by value and by reference looks identical to the caller. Q17. What is the difference between function overloading and function overriding? A17. Overloading is a method that allows defining multiple member functions with the same name but different signatures. The compiler will pick the correct function based on the signature. Overriding is a method that allows the derived class to redefine the behavior of member functions which the derived class inherits from a base class. The signatures of both base class member function and derived class member function are the same; however, the implementation and, therefore, the behavior will differ. Q18. Can derived class override some but not all of a set of overloaded virtual member functions inherited from the base class? A18. Compiler will allow this, but it is a bad practice since overridden member functions will hide all of the inherited overloads from the base class. You should really override all of them. Q19. What is the difference between assignment and initialization in C++? A19. Assignment changes the value of the object that has already been constructed. Initialization constructs a new object and gives it a value at the same time. Q20. When are copy constructors called? A20. Copy constructors are called in three cases: when a function returns an object of that class by value, when the object of that class is passed by value as an argument to a function, and, finally, when you construct an object based on another object of the same class (Circle c1=c2;). Q21. Why do you have to provide your own copy constructor and assignment operator for classes with dynamically allocated memory? A21. If you don&#039;t, the compiler will supply and execute the default constructor and the assignment operator, but they will not do the job correctly. The default assignment operator does memberwise assignment and the default copy constructor does memberwise copy. In both cases you will only assign and manipulate pointers to dynamic memory, which will lead to memory leaks and other abnormalities. You should write your own assignment operator and copy constructor, which would copy the pointer to memory so that each object has its own copy. Q22. Does compiler guarantee that initializers will be executed in the same order as they appear on the initialization list? A22. No. C++ guarantees that base class subobjects and member objects will be destroyed in the opposite order from which they were constructed. This means that initializers are executed in the order, which supports the above-mentioned guarantee. Q23. What is function&#039;s signature? A23. Function&#039;s signature is its name plus the number and types of the parameters it accepts. Q24. What does extern &quot;C&quot; int func(int *, Foo) accomplish? A24. It will turn off &quot;name mangling&quot; for this function so that one can link to code compiled by C compiler. Q25. Why do C++ compilers need name mangling? A25. Name mangling is the rule according to which C++ changes function&#039;s name into function signature before passing that function to a linker. This is how the linker differentiates between different functions with the same name. Q26. What is the difference between a pointer and a reference? A26. A reference must always refer to some object and, therefore, must always be initialized; pointers do not have such restrictions. A pointer can be reassigned to point to different objects while a reference always refers to an object with which it was initialized. Q27. How can you access the static member of a class? A27. ::. Q28. How are prefix and postfix versions of operator++() differentiated? A28. The postfix version of operator++() has a dummy parameter of type int. The prefix version does not have dummy parameter. Q29. What functions does C++ silently write and call? A29. Constructors, destructors, copy constructors, assignment operators, and address-of operators. Q30. What is the difference between new\/delete and malloc\/free? A30. Malloc\/free do not know about constructors and destructors. New and delete create and destroy objects, while malloc and free allocate and deallocate memory. Q31. What is the difference between delete and delete[ ]? A31. Delete deletes one object; delete[ ] deletes an array of objects. Q32. Name two cases where you MUST use initialization list as opposed to assignment in constructors. A32. Both non-static const data members and reference data members cannot be assigned values; instead, you should use initialization list to initialize them. Q33. What is the difference between const char *myPointer and char *const myPointer? A33. Const char *myPointer is a non constant pointer to constant data; while char *const myPointer is a constant pointer to non constant data. Q34. Suppose that objects A, B, and C are instances of class MyClass (MyClass A, B, C;). How should you design an assignment operator so that the &quot;A=B=C;&quot; statement would be allowed by a compiler but &quot;(A=B)=C;&quot; would not be allowed by a compiler? A34. Make operator=return a reference to a const object. Q35. Is there any problem with the following: char *a=NULL; char&amp; p = *a;? A35. The result is undefined. You should never do this. A reference must always refer to some object. Q36. Class B is derived from class A. Function f is A&#039;s friend. Is f B&#039;s friend as well? A36. No. Friendship cannot be inherited. Q37. What issue do auto_ptr objects address? A37. If you use auto_ptr objects you would not have to be concerned with heap objects not being deleted even if the exception is thrown. Q38. What happens when a function throws an exception that was not specified by an exception specification for this function? A38. Unexpected() is called, which, by default, will eventually trigger abort(). Q39. Why should you prefer throw\/catch mechanism to setjmp\/longjmp? A39. The main problem with longjmp() is that it does not destroy local objects properly. Q40. Can you think of a situation where your program would crash without reaching the breakpoint which you set at the beginning of main()? A40. C++ allows for dynamic initialization of global variables before main() is invoked. It is possible that initialization of global will invoke some function. If this function crashes the crash will occur before main() is entered. If you feel comfortable answering these questions, then rest assured that your chances of impressing any interviewer are very high. Be prepared to know basic computer science concepts such as data structures, search and sort algorithms, basic database concepts, etc. The client&#039;s needs will determine what particular branch of computer science you have to be familiar with, but you should always be ready to implement the stock, the queue, and the linked list data structures with either C or C++ programming languages. And know how to write your own version of strcpy (string copy) in C programming language since very often they ask you to do that.\" \/>\n\t\t<script type=\"application\/ld+json\" class=\"aioseo-schema\">\n\t\t\t{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/bitpost.com\\\/news\\\/2002\\\/reuse-dammit\\\/#article\",\"name\":\"C++ Skills Interview Questions | bitpost.com\\\/news\",\"headline\":\"C++ Skills Interview Questions\",\"author\":{\"@id\":\"https:\\\/\\\/bitpost.com\\\/news\\\/author\\\/m\\\/#author\"},\"publisher\":{\"@id\":\"https:\\\/\\\/bitpost.com\\\/news\\\/#person\"},\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\\\/\\\/bitpost.com\\\/news\\\/2002\\\/reuse-dammit\\\/#articleImage\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/884c1dbbf1027f261dbf20652687af7ad0030bfa71ff0ef56540938bdc70be2f?s=96&d=monsterid&r=pg\",\"width\":96,\"height\":96,\"caption\":\"m\"},\"datePublished\":\"2002-09-27T01:26:13-04:00\",\"dateModified\":\"2008-07-24T23:53:47-04:00\",\"inLanguage\":\"en-US\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/bitpost.com\\\/news\\\/2002\\\/reuse-dammit\\\/#webpage\"},\"isPartOf\":{\"@id\":\"https:\\\/\\\/bitpost.com\\\/news\\\/2002\\\/reuse-dammit\\\/#webpage\"},\"articleSection\":\"Tricks Tips Tools\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/bitpost.com\\\/news\\\/2002\\\/reuse-dammit\\\/#breadcrumblist\",\"itemListElement\":[{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/bitpost.com\\\/news#listItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/bitpost.com\\\/news\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/bitpost.com\\\/news\\\/category\\\/tricks-tips-tools\\\/#listItem\",\"name\":\"Tricks Tips Tools\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/bitpost.com\\\/news\\\/category\\\/tricks-tips-tools\\\/#listItem\",\"position\":2,\"name\":\"Tricks Tips Tools\",\"item\":\"https:\\\/\\\/bitpost.com\\\/news\\\/category\\\/tricks-tips-tools\\\/\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/bitpost.com\\\/news\\\/2002\\\/reuse-dammit\\\/#listItem\",\"name\":\"C++ Skills Interview Questions\"},\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/bitpost.com\\\/news#listItem\",\"name\":\"Home\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/bitpost.com\\\/news\\\/2002\\\/reuse-dammit\\\/#listItem\",\"position\":3,\"name\":\"C++ Skills Interview Questions\",\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/bitpost.com\\\/news\\\/category\\\/tricks-tips-tools\\\/#listItem\",\"name\":\"Tricks Tips Tools\"}}]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/bitpost.com\\\/news\\\/#person\",\"name\":\"m\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\\\/\\\/bitpost.com\\\/news\\\/2002\\\/reuse-dammit\\\/#personImage\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/884c1dbbf1027f261dbf20652687af7ad0030bfa71ff0ef56540938bdc70be2f?s=96&d=monsterid&r=pg\",\"width\":96,\"height\":96,\"caption\":\"m\"}},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/bitpost.com\\\/news\\\/author\\\/m\\\/#author\",\"url\":\"https:\\\/\\\/bitpost.com\\\/news\\\/author\\\/m\\\/\",\"name\":\"m\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\\\/\\\/bitpost.com\\\/news\\\/2002\\\/reuse-dammit\\\/#authorImage\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/884c1dbbf1027f261dbf20652687af7ad0030bfa71ff0ef56540938bdc70be2f?s=96&d=monsterid&r=pg\",\"width\":96,\"height\":96,\"caption\":\"m\"}},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/bitpost.com\\\/news\\\/2002\\\/reuse-dammit\\\/#webpage\",\"url\":\"https:\\\/\\\/bitpost.com\\\/news\\\/2002\\\/reuse-dammit\\\/\",\"name\":\"C++ Skills Interview Questions | bitpost.com\\\/news\",\"description\":\"The C++ Interview These 40 questions and answers will help you land the assignment by Alex Bykov How do you rank your C++ skills on a scale of 1 to 10? This is often the first question you will hear on an interview for a C++ contract. You will be tempted to rate yourself high, and you should. This is your chance to convince the client that you are just what he is looking for--an assertive and knowledgeable professional who will be productive either working on a team or on your own. Naturally, though, you should be able to support the ranking you gave yourself by doing well on the interview. This article will help you prepare for your C++ interview. I put together a list of 40 questions that I have had to answer during numerous technical interviews in the past few years. You, too, will have to answer at least some of them during an interview. Even if you use C++ on a daily basis, it pays to go through the questions. Most of us, no matter how experienced, use only a segment of the language that we are most comfortable with. Brief answers are included, but you can find more information in the references listed. Q1. Is there anything you can do in C++ that you cannot do in C? A1. No. There is nothing you can do in C++ that you cannot do in C. After all you can write a C++ compiler in C. Q2. What is the difference between C++ structure and C++ class? A2. The default access level assigned to members of struct is public while the default access level assigned to a class is private. Q3. What is encapsulation? A3. Encapsulation is welding of code and data together into objects. Q4. What is inheritance? A4. Inheritance is a mechanism through which a subclass inherits the properties and behavior of its superclass. Q5. What is polymorphism? A5. In Greek this means \\\"many shapes.\\\" As a consequence of inheritance and virtual functions, a single task (for example, drawing a geometrical shape) can be implemented using the same name (like draw()) and implemented differently (via virtual functions) as each type in object hierarchy requires(circle.draw() or rectangle.draw()). Later, when a polymorphic object (whose type is not known at compile time) executes the draw() virtual function, the correct implementation is chosen and executed at run time. Q6. What would you say if you saw \\\"delete this\\\" while reviewing your peer's code? A6. You should never do this. Since compiler does not know whether the object was allocated on the stack or on the heap, \\\"delete this\\\" could cause a disaster. Q7. What is the difference between public, protected, and private members of a class? A7. Private members are accessible only by members and friends of the class. Protected members are accessible by members and friends of the class and by members and friends of derived classes. Public members are accessible by everyone. Q8. What is the difference between non-virtual and virtual functions? A8. The behavior of a non-virtual function is known at compile time while the behavior of a virtual function is not known until the run time. Q9. What is a pure virtual function? A9. \\\"A pure virtual function is a function declared in a base class that has no definition relative to the base.\\\" Q10. What is an abstract base class? A10. It is a class that has one or more pure virtual functions. Q11. What is the difference between MyClass p; and MyClass p();? A11. MyClass p; creates an instance of class MyClass by calling a constructor for MyClass. MyClass p(); declares function p which takes no parameters and returns an object of class MyClass by value. Q12. How do you know that your class needs a virtual destructor? A12. If your class has at least one virtual function, you should make a destructor for this class virtual. This will allow you to delete a dynamic object through a pointer to a base class object. If the destructor is non-virtual, then wrong destructor will be invoked during deletion of the dynamic object. Q13. Why were the templates introduced? A13. Many data structures and algorithms can be defined independently of the type of data they work with. You can increase the amount of shared code by separating data-dependent portions from data-independent portions, and templates were introduced to help you do that. Q14. What is a static member of a class? A14. Static data members exist once for the entire class, as opposed to non-static data members, which exist individually in each instance of a class. Q15. What feature of C++ would you use if you wanted to design a member function that guarantees to leave \\\"this\\u00c3\\u0192\\u00e2\\u20ac\\u0153 object unchanged? A15. It is \\\"const\\\" as in: \\\"int MyFunc (int test) const;\\\" Q16. Can you overload a function based only on whether a parameter is a value or a reference? A16. No. Passing by value and by reference looks identical to the caller. Q17. What is the difference between function overloading and function overriding? A17. Overloading is a method that allows defining multiple member functions with the same name but different signatures. The compiler will pick the correct function based on the signature. Overriding is a method that allows the derived class to redefine the behavior of member functions which the derived class inherits from a base class. The signatures of both base class member function and derived class member function are the same; however, the implementation and, therefore, the behavior will differ. Q18. Can derived class override some but not all of a set of overloaded virtual member functions inherited from the base class? A18. Compiler will allow this, but it is a bad practice since overridden member functions will hide all of the inherited overloads from the base class. You should really override all of them. Q19. What is the difference between assignment and initialization in C++? A19. Assignment changes the value of the object that has already been constructed. Initialization constructs a new object and gives it a value at the same time. Q20. When are copy constructors called? A20. Copy constructors are called in three cases: when a function returns an object of that class by value, when the object of that class is passed by value as an argument to a function, and, finally, when you construct an object based on another object of the same class (Circle c1=c2;). Q21. Why do you have to provide your own copy constructor and assignment operator for classes with dynamically allocated memory? A21. If you don't, the compiler will supply and execute the default constructor and the assignment operator, but they will not do the job correctly. The default assignment operator does memberwise assignment and the default copy constructor does memberwise copy. In both cases you will only assign and manipulate pointers to dynamic memory, which will lead to memory leaks and other abnormalities. You should write your own assignment operator and copy constructor, which would copy the pointer to memory so that each object has its own copy. Q22. Does compiler guarantee that initializers will be executed in the same order as they appear on the initialization list? A22. No. C++ guarantees that base class subobjects and member objects will be destroyed in the opposite order from which they were constructed. This means that initializers are executed in the order, which supports the above-mentioned guarantee. Q23. What is function's signature? A23. Function's signature is its name plus the number and types of the parameters it accepts. Q24. What does extern \\\"C\\\" int func(int *, Foo) accomplish? A24. It will turn off \\\"name mangling\\\" for this function so that one can link to code compiled by C compiler. Q25. Why do C++ compilers need name mangling? A25. Name mangling is the rule according to which C++ changes function's name into function signature before passing that function to a linker. This is how the linker differentiates between different functions with the same name. Q26. What is the difference between a pointer and a reference? A26. A reference must always refer to some object and, therefore, must always be initialized; pointers do not have such restrictions. A pointer can be reassigned to point to different objects while a reference always refers to an object with which it was initialized. Q27. How can you access the static member of a class? A27. ::. Q28. How are prefix and postfix versions of operator++() differentiated? A28. The postfix version of operator++() has a dummy parameter of type int. The prefix version does not have dummy parameter. Q29. What functions does C++ silently write and call? A29. Constructors, destructors, copy constructors, assignment operators, and address-of operators. Q30. What is the difference between new\\\/delete and malloc\\\/free? A30. Malloc\\\/free do not know about constructors and destructors. New and delete create and destroy objects, while malloc and free allocate and deallocate memory. Q31. What is the difference between delete and delete[ ]? A31. Delete deletes one object; delete[ ] deletes an array of objects. Q32. Name two cases where you MUST use initialization list as opposed to assignment in constructors. A32. Both non-static const data members and reference data members cannot be assigned values; instead, you should use initialization list to initialize them. Q33. What is the difference between const char *myPointer and char *const myPointer? A33. Const char *myPointer is a non constant pointer to constant data; while char *const myPointer is a constant pointer to non constant data. Q34. Suppose that objects A, B, and C are instances of class MyClass (MyClass A, B, C;). How should you design an assignment operator so that the \\\"A=B=C;\\\" statement would be allowed by a compiler but \\\"(A=B)=C;\\\" would not be allowed by a compiler? A34. Make operator=return a reference to a const object. Q35. Is there any problem with the following: char *a=NULL; char& p = *a;? A35. The result is undefined. You should never do this. A reference must always refer to some object. Q36. Class B is derived from class A. Function f is A's friend. Is f B's friend as well? A36. No. Friendship cannot be inherited. Q37. What issue do auto_ptr objects address? A37. If you use auto_ptr objects you would not have to be concerned with heap objects not being deleted even if the exception is thrown. Q38. What happens when a function throws an exception that was not specified by an exception specification for this function? A38. Unexpected() is called, which, by default, will eventually trigger abort(). Q39. Why should you prefer throw\\\/catch mechanism to setjmp\\\/longjmp? A39. The main problem with longjmp() is that it does not destroy local objects properly. Q40. Can you think of a situation where your program would crash without reaching the breakpoint which you set at the beginning of main()? A40. C++ allows for dynamic initialization of global variables before main() is invoked. It is possible that initialization of global will invoke some function. If this function crashes the crash will occur before main() is entered. If you feel comfortable answering these questions, then rest assured that your chances of impressing any interviewer are very high. Be prepared to know basic computer science concepts such as data structures, search and sort algorithms, basic database concepts, etc. The client's needs will determine what particular branch of computer science you have to be familiar with, but you should always be ready to implement the stock, the queue, and the linked list data structures with either C or C++ programming languages. And know how to write your own version of strcpy (string copy) in C programming language since very often they ask you to do that.\",\"inLanguage\":\"en-US\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/bitpost.com\\\/news\\\/#website\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/bitpost.com\\\/news\\\/2002\\\/reuse-dammit\\\/#breadcrumblist\"},\"author\":{\"@id\":\"https:\\\/\\\/bitpost.com\\\/news\\\/author\\\/m\\\/#author\"},\"creator\":{\"@id\":\"https:\\\/\\\/bitpost.com\\\/news\\\/author\\\/m\\\/#author\"},\"datePublished\":\"2002-09-27T01:26:13-04:00\",\"dateModified\":\"2008-07-24T23:53:47-04:00\"},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/bitpost.com\\\/news\\\/#website\",\"url\":\"https:\\\/\\\/bitpost.com\\\/news\\\/\",\"name\":\"bitpost.com\\\/news\",\"description\":\"Note to self\",\"inLanguage\":\"en-US\",\"publisher\":{\"@id\":\"https:\\\/\\\/bitpost.com\\\/news\\\/#person\"}}]}\n\t\t<\/script>\n\t\t<!-- All in One SEO -->\n\n","aioseo_head_json":{"title":"C++ Skills Interview Questions | bitpost.com\/news","description":"The C++ Interview These 40 questions and answers will help you land the assignment by Alex Bykov How do you rank your C++ skills on a scale of 1 to 10? This is often the first question you will hear on an interview for a C++ contract. You will be tempted to rate yourself high, and you should. This is your chance to convince the client that you are just what he is looking for--an assertive and knowledgeable professional who will be productive either working on a team or on your own. Naturally, though, you should be able to support the ranking you gave yourself by doing well on the interview. This article will help you prepare for your C++ interview. I put together a list of 40 questions that I have had to answer during numerous technical interviews in the past few years. You, too, will have to answer at least some of them during an interview. Even if you use C++ on a daily basis, it pays to go through the questions. Most of us, no matter how experienced, use only a segment of the language that we are most comfortable with. Brief answers are included, but you can find more information in the references listed. Q1. Is there anything you can do in C++ that you cannot do in C? A1. No. There is nothing you can do in C++ that you cannot do in C. After all you can write a C++ compiler in C. Q2. What is the difference between C++ structure and C++ class? A2. The default access level assigned to members of struct is public while the default access level assigned to a class is private. Q3. What is encapsulation? A3. Encapsulation is welding of code and data together into objects. Q4. What is inheritance? A4. Inheritance is a mechanism through which a subclass inherits the properties and behavior of its superclass. Q5. What is polymorphism? A5. In Greek this means \"many shapes.\" As a consequence of inheritance and virtual functions, a single task (for example, drawing a geometrical shape) can be implemented using the same name (like draw()) and implemented differently (via virtual functions) as each type in object hierarchy requires(circle.draw() or rectangle.draw()). Later, when a polymorphic object (whose type is not known at compile time) executes the draw() virtual function, the correct implementation is chosen and executed at run time. Q6. What would you say if you saw \"delete this\" while reviewing your peer's code? A6. You should never do this. Since compiler does not know whether the object was allocated on the stack or on the heap, \"delete this\" could cause a disaster. Q7. What is the difference between public, protected, and private members of a class? A7. Private members are accessible only by members and friends of the class. Protected members are accessible by members and friends of the class and by members and friends of derived classes. Public members are accessible by everyone. Q8. What is the difference between non-virtual and virtual functions? A8. The behavior of a non-virtual function is known at compile time while the behavior of a virtual function is not known until the run time. Q9. What is a pure virtual function? A9. \"A pure virtual function is a function declared in a base class that has no definition relative to the base.\" Q10. What is an abstract base class? A10. It is a class that has one or more pure virtual functions. Q11. What is the difference between MyClass p; and MyClass p();? A11. MyClass p; creates an instance of class MyClass by calling a constructor for MyClass. MyClass p(); declares function p which takes no parameters and returns an object of class MyClass by value. Q12. How do you know that your class needs a virtual destructor? A12. If your class has at least one virtual function, you should make a destructor for this class virtual. This will allow you to delete a dynamic object through a pointer to a base class object. If the destructor is non-virtual, then wrong destructor will be invoked during deletion of the dynamic object. Q13. Why were the templates introduced? A13. Many data structures and algorithms can be defined independently of the type of data they work with. You can increase the amount of shared code by separating data-dependent portions from data-independent portions, and templates were introduced to help you do that. Q14. What is a static member of a class? A14. Static data members exist once for the entire class, as opposed to non-static data members, which exist individually in each instance of a class. Q15. What feature of C++ would you use if you wanted to design a member function that guarantees to leave \"this\u00c3\u0192\u00e2\u20ac\u0153 object unchanged? A15. It is \"const\" as in: \"int MyFunc (int test) const;\" Q16. Can you overload a function based only on whether a parameter is a value or a reference? A16. No. Passing by value and by reference looks identical to the caller. Q17. What is the difference between function overloading and function overriding? A17. Overloading is a method that allows defining multiple member functions with the same name but different signatures. The compiler will pick the correct function based on the signature. Overriding is a method that allows the derived class to redefine the behavior of member functions which the derived class inherits from a base class. The signatures of both base class member function and derived class member function are the same; however, the implementation and, therefore, the behavior will differ. Q18. Can derived class override some but not all of a set of overloaded virtual member functions inherited from the base class? A18. Compiler will allow this, but it is a bad practice since overridden member functions will hide all of the inherited overloads from the base class. You should really override all of them. Q19. What is the difference between assignment and initialization in C++? A19. Assignment changes the value of the object that has already been constructed. Initialization constructs a new object and gives it a value at the same time. Q20. When are copy constructors called? A20. Copy constructors are called in three cases: when a function returns an object of that class by value, when the object of that class is passed by value as an argument to a function, and, finally, when you construct an object based on another object of the same class (Circle c1=c2;). Q21. Why do you have to provide your own copy constructor and assignment operator for classes with dynamically allocated memory? A21. If you don't, the compiler will supply and execute the default constructor and the assignment operator, but they will not do the job correctly. The default assignment operator does memberwise assignment and the default copy constructor does memberwise copy. In both cases you will only assign and manipulate pointers to dynamic memory, which will lead to memory leaks and other abnormalities. You should write your own assignment operator and copy constructor, which would copy the pointer to memory so that each object has its own copy. Q22. Does compiler guarantee that initializers will be executed in the same order as they appear on the initialization list? A22. No. C++ guarantees that base class subobjects and member objects will be destroyed in the opposite order from which they were constructed. This means that initializers are executed in the order, which supports the above-mentioned guarantee. Q23. What is function's signature? A23. Function's signature is its name plus the number and types of the parameters it accepts. Q24. What does extern \"C\" int func(int *, Foo) accomplish? A24. It will turn off \"name mangling\" for this function so that one can link to code compiled by C compiler. Q25. Why do C++ compilers need name mangling? A25. Name mangling is the rule according to which C++ changes function's name into function signature before passing that function to a linker. This is how the linker differentiates between different functions with the same name. Q26. What is the difference between a pointer and a reference? A26. A reference must always refer to some object and, therefore, must always be initialized; pointers do not have such restrictions. A pointer can be reassigned to point to different objects while a reference always refers to an object with which it was initialized. Q27. How can you access the static member of a class? A27. ::. Q28. How are prefix and postfix versions of operator++() differentiated? A28. The postfix version of operator++() has a dummy parameter of type int. The prefix version does not have dummy parameter. Q29. What functions does C++ silently write and call? A29. Constructors, destructors, copy constructors, assignment operators, and address-of operators. Q30. What is the difference between new\/delete and malloc\/free? A30. Malloc\/free do not know about constructors and destructors. New and delete create and destroy objects, while malloc and free allocate and deallocate memory. Q31. What is the difference between delete and delete[ ]? A31. Delete deletes one object; delete[ ] deletes an array of objects. Q32. Name two cases where you MUST use initialization list as opposed to assignment in constructors. A32. Both non-static const data members and reference data members cannot be assigned values; instead, you should use initialization list to initialize them. Q33. What is the difference between const char *myPointer and char *const myPointer? A33. Const char *myPointer is a non constant pointer to constant data; while char *const myPointer is a constant pointer to non constant data. Q34. Suppose that objects A, B, and C are instances of class MyClass (MyClass A, B, C;). How should you design an assignment operator so that the \"A=B=C;\" statement would be allowed by a compiler but \"(A=B)=C;\" would not be allowed by a compiler? A34. Make operator=return a reference to a const object. Q35. Is there any problem with the following: char *a=NULL; char& p = *a;? A35. The result is undefined. You should never do this. A reference must always refer to some object. Q36. Class B is derived from class A. Function f is A's friend. Is f B's friend as well? A36. No. Friendship cannot be inherited. Q37. What issue do auto_ptr objects address? A37. If you use auto_ptr objects you would not have to be concerned with heap objects not being deleted even if the exception is thrown. Q38. What happens when a function throws an exception that was not specified by an exception specification for this function? A38. Unexpected() is called, which, by default, will eventually trigger abort(). Q39. Why should you prefer throw\/catch mechanism to setjmp\/longjmp? A39. The main problem with longjmp() is that it does not destroy local objects properly. Q40. Can you think of a situation where your program would crash without reaching the breakpoint which you set at the beginning of main()? A40. C++ allows for dynamic initialization of global variables before main() is invoked. It is possible that initialization of global will invoke some function. If this function crashes the crash will occur before main() is entered. If you feel comfortable answering these questions, then rest assured that your chances of impressing any interviewer are very high. Be prepared to know basic computer science concepts such as data structures, search and sort algorithms, basic database concepts, etc. The client's needs will determine what particular branch of computer science you have to be familiar with, but you should always be ready to implement the stock, the queue, and the linked list data structures with either C or C++ programming languages. And know how to write your own version of strcpy (string copy) in C programming language since very often they ask you to do that.","canonical_url":"https:\/\/bitpost.com\/news\/2002\/reuse-dammit\/","robots":"max-image-preview:large","keywords":"tricks tips tools","webmasterTools":{"miscellaneous":""},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/bitpost.com\/news\/2002\/reuse-dammit\/#article","name":"C++ Skills Interview Questions | bitpost.com\/news","headline":"C++ Skills Interview Questions","author":{"@id":"https:\/\/bitpost.com\/news\/author\/m\/#author"},"publisher":{"@id":"https:\/\/bitpost.com\/news\/#person"},"image":{"@type":"ImageObject","@id":"https:\/\/bitpost.com\/news\/2002\/reuse-dammit\/#articleImage","url":"https:\/\/secure.gravatar.com\/avatar\/884c1dbbf1027f261dbf20652687af7ad0030bfa71ff0ef56540938bdc70be2f?s=96&d=monsterid&r=pg","width":96,"height":96,"caption":"m"},"datePublished":"2002-09-27T01:26:13-04:00","dateModified":"2008-07-24T23:53:47-04:00","inLanguage":"en-US","mainEntityOfPage":{"@id":"https:\/\/bitpost.com\/news\/2002\/reuse-dammit\/#webpage"},"isPartOf":{"@id":"https:\/\/bitpost.com\/news\/2002\/reuse-dammit\/#webpage"},"articleSection":"Tricks Tips Tools"},{"@type":"BreadcrumbList","@id":"https:\/\/bitpost.com\/news\/2002\/reuse-dammit\/#breadcrumblist","itemListElement":[{"@type":"ListItem","@id":"https:\/\/bitpost.com\/news#listItem","position":1,"name":"Home","item":"https:\/\/bitpost.com\/news","nextItem":{"@type":"ListItem","@id":"https:\/\/bitpost.com\/news\/category\/tricks-tips-tools\/#listItem","name":"Tricks Tips Tools"}},{"@type":"ListItem","@id":"https:\/\/bitpost.com\/news\/category\/tricks-tips-tools\/#listItem","position":2,"name":"Tricks Tips Tools","item":"https:\/\/bitpost.com\/news\/category\/tricks-tips-tools\/","nextItem":{"@type":"ListItem","@id":"https:\/\/bitpost.com\/news\/2002\/reuse-dammit\/#listItem","name":"C++ Skills Interview Questions"},"previousItem":{"@type":"ListItem","@id":"https:\/\/bitpost.com\/news#listItem","name":"Home"}},{"@type":"ListItem","@id":"https:\/\/bitpost.com\/news\/2002\/reuse-dammit\/#listItem","position":3,"name":"C++ Skills Interview Questions","previousItem":{"@type":"ListItem","@id":"https:\/\/bitpost.com\/news\/category\/tricks-tips-tools\/#listItem","name":"Tricks Tips Tools"}}]},{"@type":"Person","@id":"https:\/\/bitpost.com\/news\/#person","name":"m","image":{"@type":"ImageObject","@id":"https:\/\/bitpost.com\/news\/2002\/reuse-dammit\/#personImage","url":"https:\/\/secure.gravatar.com\/avatar\/884c1dbbf1027f261dbf20652687af7ad0030bfa71ff0ef56540938bdc70be2f?s=96&d=monsterid&r=pg","width":96,"height":96,"caption":"m"}},{"@type":"Person","@id":"https:\/\/bitpost.com\/news\/author\/m\/#author","url":"https:\/\/bitpost.com\/news\/author\/m\/","name":"m","image":{"@type":"ImageObject","@id":"https:\/\/bitpost.com\/news\/2002\/reuse-dammit\/#authorImage","url":"https:\/\/secure.gravatar.com\/avatar\/884c1dbbf1027f261dbf20652687af7ad0030bfa71ff0ef56540938bdc70be2f?s=96&d=monsterid&r=pg","width":96,"height":96,"caption":"m"}},{"@type":"WebPage","@id":"https:\/\/bitpost.com\/news\/2002\/reuse-dammit\/#webpage","url":"https:\/\/bitpost.com\/news\/2002\/reuse-dammit\/","name":"C++ Skills Interview Questions | bitpost.com\/news","description":"The C++ Interview These 40 questions and answers will help you land the assignment by Alex Bykov How do you rank your C++ skills on a scale of 1 to 10? This is often the first question you will hear on an interview for a C++ contract. You will be tempted to rate yourself high, and you should. This is your chance to convince the client that you are just what he is looking for--an assertive and knowledgeable professional who will be productive either working on a team or on your own. Naturally, though, you should be able to support the ranking you gave yourself by doing well on the interview. This article will help you prepare for your C++ interview. I put together a list of 40 questions that I have had to answer during numerous technical interviews in the past few years. You, too, will have to answer at least some of them during an interview. Even if you use C++ on a daily basis, it pays to go through the questions. Most of us, no matter how experienced, use only a segment of the language that we are most comfortable with. Brief answers are included, but you can find more information in the references listed. Q1. Is there anything you can do in C++ that you cannot do in C? A1. No. There is nothing you can do in C++ that you cannot do in C. After all you can write a C++ compiler in C. Q2. What is the difference between C++ structure and C++ class? A2. The default access level assigned to members of struct is public while the default access level assigned to a class is private. Q3. What is encapsulation? A3. Encapsulation is welding of code and data together into objects. Q4. What is inheritance? A4. Inheritance is a mechanism through which a subclass inherits the properties and behavior of its superclass. Q5. What is polymorphism? A5. In Greek this means \"many shapes.\" As a consequence of inheritance and virtual functions, a single task (for example, drawing a geometrical shape) can be implemented using the same name (like draw()) and implemented differently (via virtual functions) as each type in object hierarchy requires(circle.draw() or rectangle.draw()). Later, when a polymorphic object (whose type is not known at compile time) executes the draw() virtual function, the correct implementation is chosen and executed at run time. Q6. What would you say if you saw \"delete this\" while reviewing your peer's code? A6. You should never do this. Since compiler does not know whether the object was allocated on the stack or on the heap, \"delete this\" could cause a disaster. Q7. What is the difference between public, protected, and private members of a class? A7. Private members are accessible only by members and friends of the class. Protected members are accessible by members and friends of the class and by members and friends of derived classes. Public members are accessible by everyone. Q8. What is the difference between non-virtual and virtual functions? A8. The behavior of a non-virtual function is known at compile time while the behavior of a virtual function is not known until the run time. Q9. What is a pure virtual function? A9. \"A pure virtual function is a function declared in a base class that has no definition relative to the base.\" Q10. What is an abstract base class? A10. It is a class that has one or more pure virtual functions. Q11. What is the difference between MyClass p; and MyClass p();? A11. MyClass p; creates an instance of class MyClass by calling a constructor for MyClass. MyClass p(); declares function p which takes no parameters and returns an object of class MyClass by value. Q12. How do you know that your class needs a virtual destructor? A12. If your class has at least one virtual function, you should make a destructor for this class virtual. This will allow you to delete a dynamic object through a pointer to a base class object. If the destructor is non-virtual, then wrong destructor will be invoked during deletion of the dynamic object. Q13. Why were the templates introduced? A13. Many data structures and algorithms can be defined independently of the type of data they work with. You can increase the amount of shared code by separating data-dependent portions from data-independent portions, and templates were introduced to help you do that. Q14. What is a static member of a class? A14. Static data members exist once for the entire class, as opposed to non-static data members, which exist individually in each instance of a class. Q15. What feature of C++ would you use if you wanted to design a member function that guarantees to leave \"this\u00c3\u0192\u00e2\u20ac\u0153 object unchanged? A15. It is \"const\" as in: \"int MyFunc (int test) const;\" Q16. Can you overload a function based only on whether a parameter is a value or a reference? A16. No. Passing by value and by reference looks identical to the caller. Q17. What is the difference between function overloading and function overriding? A17. Overloading is a method that allows defining multiple member functions with the same name but different signatures. The compiler will pick the correct function based on the signature. Overriding is a method that allows the derived class to redefine the behavior of member functions which the derived class inherits from a base class. The signatures of both base class member function and derived class member function are the same; however, the implementation and, therefore, the behavior will differ. Q18. Can derived class override some but not all of a set of overloaded virtual member functions inherited from the base class? A18. Compiler will allow this, but it is a bad practice since overridden member functions will hide all of the inherited overloads from the base class. You should really override all of them. Q19. What is the difference between assignment and initialization in C++? A19. Assignment changes the value of the object that has already been constructed. Initialization constructs a new object and gives it a value at the same time. Q20. When are copy constructors called? A20. Copy constructors are called in three cases: when a function returns an object of that class by value, when the object of that class is passed by value as an argument to a function, and, finally, when you construct an object based on another object of the same class (Circle c1=c2;). Q21. Why do you have to provide your own copy constructor and assignment operator for classes with dynamically allocated memory? A21. If you don't, the compiler will supply and execute the default constructor and the assignment operator, but they will not do the job correctly. The default assignment operator does memberwise assignment and the default copy constructor does memberwise copy. In both cases you will only assign and manipulate pointers to dynamic memory, which will lead to memory leaks and other abnormalities. You should write your own assignment operator and copy constructor, which would copy the pointer to memory so that each object has its own copy. Q22. Does compiler guarantee that initializers will be executed in the same order as they appear on the initialization list? A22. No. C++ guarantees that base class subobjects and member objects will be destroyed in the opposite order from which they were constructed. This means that initializers are executed in the order, which supports the above-mentioned guarantee. Q23. What is function's signature? A23. Function's signature is its name plus the number and types of the parameters it accepts. Q24. What does extern \"C\" int func(int *, Foo) accomplish? A24. It will turn off \"name mangling\" for this function so that one can link to code compiled by C compiler. Q25. Why do C++ compilers need name mangling? A25. Name mangling is the rule according to which C++ changes function's name into function signature before passing that function to a linker. This is how the linker differentiates between different functions with the same name. Q26. What is the difference between a pointer and a reference? A26. A reference must always refer to some object and, therefore, must always be initialized; pointers do not have such restrictions. A pointer can be reassigned to point to different objects while a reference always refers to an object with which it was initialized. Q27. How can you access the static member of a class? A27. ::. Q28. How are prefix and postfix versions of operator++() differentiated? A28. The postfix version of operator++() has a dummy parameter of type int. The prefix version does not have dummy parameter. Q29. What functions does C++ silently write and call? A29. Constructors, destructors, copy constructors, assignment operators, and address-of operators. Q30. What is the difference between new\/delete and malloc\/free? A30. Malloc\/free do not know about constructors and destructors. New and delete create and destroy objects, while malloc and free allocate and deallocate memory. Q31. What is the difference between delete and delete[ ]? A31. Delete deletes one object; delete[ ] deletes an array of objects. Q32. Name two cases where you MUST use initialization list as opposed to assignment in constructors. A32. Both non-static const data members and reference data members cannot be assigned values; instead, you should use initialization list to initialize them. Q33. What is the difference between const char *myPointer and char *const myPointer? A33. Const char *myPointer is a non constant pointer to constant data; while char *const myPointer is a constant pointer to non constant data. Q34. Suppose that objects A, B, and C are instances of class MyClass (MyClass A, B, C;). How should you design an assignment operator so that the \"A=B=C;\" statement would be allowed by a compiler but \"(A=B)=C;\" would not be allowed by a compiler? A34. Make operator=return a reference to a const object. Q35. Is there any problem with the following: char *a=NULL; char& p = *a;? A35. The result is undefined. You should never do this. A reference must always refer to some object. Q36. Class B is derived from class A. Function f is A's friend. Is f B's friend as well? A36. No. Friendship cannot be inherited. Q37. What issue do auto_ptr objects address? A37. If you use auto_ptr objects you would not have to be concerned with heap objects not being deleted even if the exception is thrown. Q38. What happens when a function throws an exception that was not specified by an exception specification for this function? A38. Unexpected() is called, which, by default, will eventually trigger abort(). Q39. Why should you prefer throw\/catch mechanism to setjmp\/longjmp? A39. The main problem with longjmp() is that it does not destroy local objects properly. Q40. Can you think of a situation where your program would crash without reaching the breakpoint which you set at the beginning of main()? A40. C++ allows for dynamic initialization of global variables before main() is invoked. It is possible that initialization of global will invoke some function. If this function crashes the crash will occur before main() is entered. If you feel comfortable answering these questions, then rest assured that your chances of impressing any interviewer are very high. Be prepared to know basic computer science concepts such as data structures, search and sort algorithms, basic database concepts, etc. The client's needs will determine what particular branch of computer science you have to be familiar with, but you should always be ready to implement the stock, the queue, and the linked list data structures with either C or C++ programming languages. And know how to write your own version of strcpy (string copy) in C programming language since very often they ask you to do that.","inLanguage":"en-US","isPartOf":{"@id":"https:\/\/bitpost.com\/news\/#website"},"breadcrumb":{"@id":"https:\/\/bitpost.com\/news\/2002\/reuse-dammit\/#breadcrumblist"},"author":{"@id":"https:\/\/bitpost.com\/news\/author\/m\/#author"},"creator":{"@id":"https:\/\/bitpost.com\/news\/author\/m\/#author"},"datePublished":"2002-09-27T01:26:13-04:00","dateModified":"2008-07-24T23:53:47-04:00"},{"@type":"WebSite","@id":"https:\/\/bitpost.com\/news\/#website","url":"https:\/\/bitpost.com\/news\/","name":"bitpost.com\/news","description":"Note to self","inLanguage":"en-US","publisher":{"@id":"https:\/\/bitpost.com\/news\/#person"}}]},"og:locale":"en_US","og:site_name":"bitpost.com\/news | Note to self","og:type":"article","og:title":"C++ Skills Interview Questions | bitpost.com\/news","og:description":"The C++ Interview These 40 questions and answers will help you land the assignment by Alex Bykov How do you rank your C++ skills on a scale of 1 to 10? This is often the first question you will hear on an interview for a C++ contract. You will be tempted to rate yourself high, and you should. This is your chance to convince the client that you are just what he is looking for--an assertive and knowledgeable professional who will be productive either working on a team or on your own. Naturally, though, you should be able to support the ranking you gave yourself by doing well on the interview. This article will help you prepare for your C++ interview. I put together a list of 40 questions that I have had to answer during numerous technical interviews in the past few years. You, too, will have to answer at least some of them during an interview. Even if you use C++ on a daily basis, it pays to go through the questions. Most of us, no matter how experienced, use only a segment of the language that we are most comfortable with. Brief answers are included, but you can find more information in the references listed. Q1. Is there anything you can do in C++ that you cannot do in C? A1. No. There is nothing you can do in C++ that you cannot do in C. After all you can write a C++ compiler in C. Q2. What is the difference between C++ structure and C++ class? A2. The default access level assigned to members of struct is public while the default access level assigned to a class is private. Q3. What is encapsulation? A3. Encapsulation is welding of code and data together into objects. Q4. What is inheritance? A4. Inheritance is a mechanism through which a subclass inherits the properties and behavior of its superclass. Q5. What is polymorphism? A5. In Greek this means &quot;many shapes.&quot; As a consequence of inheritance and virtual functions, a single task (for example, drawing a geometrical shape) can be implemented using the same name (like draw()) and implemented differently (via virtual functions) as each type in object hierarchy requires(circle.draw() or rectangle.draw()). Later, when a polymorphic object (whose type is not known at compile time) executes the draw() virtual function, the correct implementation is chosen and executed at run time. Q6. What would you say if you saw &quot;delete this&quot; while reviewing your peer's code? A6. You should never do this. Since compiler does not know whether the object was allocated on the stack or on the heap, &quot;delete this&quot; could cause a disaster. Q7. What is the difference between public, protected, and private members of a class? A7. Private members are accessible only by members and friends of the class. Protected members are accessible by members and friends of the class and by members and friends of derived classes. Public members are accessible by everyone. Q8. What is the difference between non-virtual and virtual functions? A8. The behavior of a non-virtual function is known at compile time while the behavior of a virtual function is not known until the run time. Q9. What is a pure virtual function? A9. &quot;A pure virtual function is a function declared in a base class that has no definition relative to the base.&quot; Q10. What is an abstract base class? A10. It is a class that has one or more pure virtual functions. Q11. What is the difference between MyClass p; and MyClass p();? A11. MyClass p; creates an instance of class MyClass by calling a constructor for MyClass. MyClass p(); declares function p which takes no parameters and returns an object of class MyClass by value. Q12. How do you know that your class needs a virtual destructor? A12. If your class has at least one virtual function, you should make a destructor for this class virtual. This will allow you to delete a dynamic object through a pointer to a base class object. If the destructor is non-virtual, then wrong destructor will be invoked during deletion of the dynamic object. Q13. Why were the templates introduced? A13. Many data structures and algorithms can be defined independently of the type of data they work with. You can increase the amount of shared code by separating data-dependent portions from data-independent portions, and templates were introduced to help you do that. Q14. What is a static member of a class? A14. Static data members exist once for the entire class, as opposed to non-static data members, which exist individually in each instance of a class. Q15. What feature of C++ would you use if you wanted to design a member function that guarantees to leave &quot;this\u00c3\u0192\u00e2\u20ac\u0153 object unchanged? A15. It is &quot;const&quot; as in: &quot;int MyFunc (int test) const;&quot; Q16. Can you overload a function based only on whether a parameter is a value or a reference? A16. No. Passing by value and by reference looks identical to the caller. Q17. What is the difference between function overloading and function overriding? A17. Overloading is a method that allows defining multiple member functions with the same name but different signatures. The compiler will pick the correct function based on the signature. Overriding is a method that allows the derived class to redefine the behavior of member functions which the derived class inherits from a base class. The signatures of both base class member function and derived class member function are the same; however, the implementation and, therefore, the behavior will differ. Q18. Can derived class override some but not all of a set of overloaded virtual member functions inherited from the base class? A18. Compiler will allow this, but it is a bad practice since overridden member functions will hide all of the inherited overloads from the base class. You should really override all of them. Q19. What is the difference between assignment and initialization in C++? A19. Assignment changes the value of the object that has already been constructed. Initialization constructs a new object and gives it a value at the same time. Q20. When are copy constructors called? A20. Copy constructors are called in three cases: when a function returns an object of that class by value, when the object of that class is passed by value as an argument to a function, and, finally, when you construct an object based on another object of the same class (Circle c1=c2;). Q21. Why do you have to provide your own copy constructor and assignment operator for classes with dynamically allocated memory? A21. If you don't, the compiler will supply and execute the default constructor and the assignment operator, but they will not do the job correctly. The default assignment operator does memberwise assignment and the default copy constructor does memberwise copy. In both cases you will only assign and manipulate pointers to dynamic memory, which will lead to memory leaks and other abnormalities. You should write your own assignment operator and copy constructor, which would copy the pointer to memory so that each object has its own copy. Q22. Does compiler guarantee that initializers will be executed in the same order as they appear on the initialization list? A22. No. C++ guarantees that base class subobjects and member objects will be destroyed in the opposite order from which they were constructed. This means that initializers are executed in the order, which supports the above-mentioned guarantee. Q23. What is function's signature? A23. Function's signature is its name plus the number and types of the parameters it accepts. Q24. What does extern &quot;C&quot; int func(int *, Foo) accomplish? A24. It will turn off &quot;name mangling&quot; for this function so that one can link to code compiled by C compiler. Q25. Why do C++ compilers need name mangling? A25. Name mangling is the rule according to which C++ changes function's name into function signature before passing that function to a linker. This is how the linker differentiates between different functions with the same name. Q26. What is the difference between a pointer and a reference? A26. A reference must always refer to some object and, therefore, must always be initialized; pointers do not have such restrictions. A pointer can be reassigned to point to different objects while a reference always refers to an object with which it was initialized. Q27. How can you access the static member of a class? A27. ::. Q28. How are prefix and postfix versions of operator++() differentiated? A28. The postfix version of operator++() has a dummy parameter of type int. The prefix version does not have dummy parameter. Q29. What functions does C++ silently write and call? A29. Constructors, destructors, copy constructors, assignment operators, and address-of operators. Q30. What is the difference between new\/delete and malloc\/free? A30. Malloc\/free do not know about constructors and destructors. New and delete create and destroy objects, while malloc and free allocate and deallocate memory. Q31. What is the difference between delete and delete[ ]? A31. Delete deletes one object; delete[ ] deletes an array of objects. Q32. Name two cases where you MUST use initialization list as opposed to assignment in constructors. A32. Both non-static const data members and reference data members cannot be assigned values; instead, you should use initialization list to initialize them. Q33. What is the difference between const char *myPointer and char *const myPointer? A33. Const char *myPointer is a non constant pointer to constant data; while char *const myPointer is a constant pointer to non constant data. Q34. Suppose that objects A, B, and C are instances of class MyClass (MyClass A, B, C;). How should you design an assignment operator so that the &quot;A=B=C;&quot; statement would be allowed by a compiler but &quot;(A=B)=C;&quot; would not be allowed by a compiler? A34. Make operator=return a reference to a const object. Q35. Is there any problem with the following: char *a=NULL; char&amp; p = *a;? A35. The result is undefined. You should never do this. A reference must always refer to some object. Q36. Class B is derived from class A. Function f is A's friend. Is f B's friend as well? A36. No. Friendship cannot be inherited. Q37. What issue do auto_ptr objects address? A37. If you use auto_ptr objects you would not have to be concerned with heap objects not being deleted even if the exception is thrown. Q38. What happens when a function throws an exception that was not specified by an exception specification for this function? A38. Unexpected() is called, which, by default, will eventually trigger abort(). Q39. Why should you prefer throw\/catch mechanism to setjmp\/longjmp? A39. The main problem with longjmp() is that it does not destroy local objects properly. Q40. Can you think of a situation where your program would crash without reaching the breakpoint which you set at the beginning of main()? A40. C++ allows for dynamic initialization of global variables before main() is invoked. It is possible that initialization of global will invoke some function. If this function crashes the crash will occur before main() is entered. If you feel comfortable answering these questions, then rest assured that your chances of impressing any interviewer are very high. Be prepared to know basic computer science concepts such as data structures, search and sort algorithms, basic database concepts, etc. The client's needs will determine what particular branch of computer science you have to be familiar with, but you should always be ready to implement the stock, the queue, and the linked list data structures with either C or C++ programming languages. And know how to write your own version of strcpy (string copy) in C programming language since very often they ask you to do that.","og:url":"https:\/\/bitpost.com\/news\/2002\/reuse-dammit\/","article:published_time":"2002-09-27T01:26:13+00:00","article:modified_time":"2008-07-25T04:53:47+00:00","twitter:card":"summary","twitter:title":"C++ Skills Interview Questions | bitpost.com\/news","twitter:description":"The C++ Interview These 40 questions and answers will help you land the assignment by Alex Bykov How do you rank your C++ skills on a scale of 1 to 10? This is often the first question you will hear on an interview for a C++ contract. You will be tempted to rate yourself high, and you should. This is your chance to convince the client that you are just what he is looking for--an assertive and knowledgeable professional who will be productive either working on a team or on your own. Naturally, though, you should be able to support the ranking you gave yourself by doing well on the interview. This article will help you prepare for your C++ interview. I put together a list of 40 questions that I have had to answer during numerous technical interviews in the past few years. You, too, will have to answer at least some of them during an interview. Even if you use C++ on a daily basis, it pays to go through the questions. Most of us, no matter how experienced, use only a segment of the language that we are most comfortable with. Brief answers are included, but you can find more information in the references listed. Q1. Is there anything you can do in C++ that you cannot do in C? A1. No. There is nothing you can do in C++ that you cannot do in C. After all you can write a C++ compiler in C. Q2. What is the difference between C++ structure and C++ class? A2. The default access level assigned to members of struct is public while the default access level assigned to a class is private. Q3. What is encapsulation? A3. Encapsulation is welding of code and data together into objects. Q4. What is inheritance? A4. Inheritance is a mechanism through which a subclass inherits the properties and behavior of its superclass. Q5. What is polymorphism? A5. In Greek this means &quot;many shapes.&quot; As a consequence of inheritance and virtual functions, a single task (for example, drawing a geometrical shape) can be implemented using the same name (like draw()) and implemented differently (via virtual functions) as each type in object hierarchy requires(circle.draw() or rectangle.draw()). Later, when a polymorphic object (whose type is not known at compile time) executes the draw() virtual function, the correct implementation is chosen and executed at run time. Q6. What would you say if you saw &quot;delete this&quot; while reviewing your peer's code? A6. You should never do this. Since compiler does not know whether the object was allocated on the stack or on the heap, &quot;delete this&quot; could cause a disaster. Q7. What is the difference between public, protected, and private members of a class? A7. Private members are accessible only by members and friends of the class. Protected members are accessible by members and friends of the class and by members and friends of derived classes. Public members are accessible by everyone. Q8. What is the difference between non-virtual and virtual functions? A8. The behavior of a non-virtual function is known at compile time while the behavior of a virtual function is not known until the run time. Q9. What is a pure virtual function? A9. &quot;A pure virtual function is a function declared in a base class that has no definition relative to the base.&quot; Q10. What is an abstract base class? A10. It is a class that has one or more pure virtual functions. Q11. What is the difference between MyClass p; and MyClass p();? A11. MyClass p; creates an instance of class MyClass by calling a constructor for MyClass. MyClass p(); declares function p which takes no parameters and returns an object of class MyClass by value. Q12. How do you know that your class needs a virtual destructor? A12. If your class has at least one virtual function, you should make a destructor for this class virtual. This will allow you to delete a dynamic object through a pointer to a base class object. If the destructor is non-virtual, then wrong destructor will be invoked during deletion of the dynamic object. Q13. Why were the templates introduced? A13. Many data structures and algorithms can be defined independently of the type of data they work with. You can increase the amount of shared code by separating data-dependent portions from data-independent portions, and templates were introduced to help you do that. Q14. What is a static member of a class? A14. Static data members exist once for the entire class, as opposed to non-static data members, which exist individually in each instance of a class. Q15. What feature of C++ would you use if you wanted to design a member function that guarantees to leave &quot;this\u00c3\u0192\u00e2\u20ac\u0153 object unchanged? A15. It is &quot;const&quot; as in: &quot;int MyFunc (int test) const;&quot; Q16. Can you overload a function based only on whether a parameter is a value or a reference? A16. No. Passing by value and by reference looks identical to the caller. Q17. What is the difference between function overloading and function overriding? A17. Overloading is a method that allows defining multiple member functions with the same name but different signatures. The compiler will pick the correct function based on the signature. Overriding is a method that allows the derived class to redefine the behavior of member functions which the derived class inherits from a base class. The signatures of both base class member function and derived class member function are the same; however, the implementation and, therefore, the behavior will differ. Q18. Can derived class override some but not all of a set of overloaded virtual member functions inherited from the base class? A18. Compiler will allow this, but it is a bad practice since overridden member functions will hide all of the inherited overloads from the base class. You should really override all of them. Q19. What is the difference between assignment and initialization in C++? A19. Assignment changes the value of the object that has already been constructed. Initialization constructs a new object and gives it a value at the same time. Q20. When are copy constructors called? A20. Copy constructors are called in three cases: when a function returns an object of that class by value, when the object of that class is passed by value as an argument to a function, and, finally, when you construct an object based on another object of the same class (Circle c1=c2;). Q21. Why do you have to provide your own copy constructor and assignment operator for classes with dynamically allocated memory? A21. If you don't, the compiler will supply and execute the default constructor and the assignment operator, but they will not do the job correctly. The default assignment operator does memberwise assignment and the default copy constructor does memberwise copy. In both cases you will only assign and manipulate pointers to dynamic memory, which will lead to memory leaks and other abnormalities. You should write your own assignment operator and copy constructor, which would copy the pointer to memory so that each object has its own copy. Q22. Does compiler guarantee that initializers will be executed in the same order as they appear on the initialization list? A22. No. C++ guarantees that base class subobjects and member objects will be destroyed in the opposite order from which they were constructed. This means that initializers are executed in the order, which supports the above-mentioned guarantee. Q23. What is function's signature? A23. Function's signature is its name plus the number and types of the parameters it accepts. Q24. What does extern &quot;C&quot; int func(int *, Foo) accomplish? A24. It will turn off &quot;name mangling&quot; for this function so that one can link to code compiled by C compiler. Q25. Why do C++ compilers need name mangling? A25. Name mangling is the rule according to which C++ changes function's name into function signature before passing that function to a linker. This is how the linker differentiates between different functions with the same name. Q26. What is the difference between a pointer and a reference? A26. A reference must always refer to some object and, therefore, must always be initialized; pointers do not have such restrictions. A pointer can be reassigned to point to different objects while a reference always refers to an object with which it was initialized. Q27. How can you access the static member of a class? A27. ::. Q28. How are prefix and postfix versions of operator++() differentiated? A28. The postfix version of operator++() has a dummy parameter of type int. The prefix version does not have dummy parameter. Q29. What functions does C++ silently write and call? A29. Constructors, destructors, copy constructors, assignment operators, and address-of operators. Q30. What is the difference between new\/delete and malloc\/free? A30. Malloc\/free do not know about constructors and destructors. New and delete create and destroy objects, while malloc and free allocate and deallocate memory. Q31. What is the difference between delete and delete[ ]? A31. Delete deletes one object; delete[ ] deletes an array of objects. Q32. Name two cases where you MUST use initialization list as opposed to assignment in constructors. A32. Both non-static const data members and reference data members cannot be assigned values; instead, you should use initialization list to initialize them. Q33. What is the difference between const char *myPointer and char *const myPointer? A33. Const char *myPointer is a non constant pointer to constant data; while char *const myPointer is a constant pointer to non constant data. Q34. Suppose that objects A, B, and C are instances of class MyClass (MyClass A, B, C;). How should you design an assignment operator so that the &quot;A=B=C;&quot; statement would be allowed by a compiler but &quot;(A=B)=C;&quot; would not be allowed by a compiler? A34. Make operator=return a reference to a const object. Q35. Is there any problem with the following: char *a=NULL; char&amp; p = *a;? A35. The result is undefined. You should never do this. A reference must always refer to some object. Q36. Class B is derived from class A. Function f is A's friend. Is f B's friend as well? A36. No. Friendship cannot be inherited. Q37. What issue do auto_ptr objects address? A37. If you use auto_ptr objects you would not have to be concerned with heap objects not being deleted even if the exception is thrown. Q38. What happens when a function throws an exception that was not specified by an exception specification for this function? A38. Unexpected() is called, which, by default, will eventually trigger abort(). Q39. Why should you prefer throw\/catch mechanism to setjmp\/longjmp? A39. The main problem with longjmp() is that it does not destroy local objects properly. Q40. Can you think of a situation where your program would crash without reaching the breakpoint which you set at the beginning of main()? A40. C++ allows for dynamic initialization of global variables before main() is invoked. It is possible that initialization of global will invoke some function. If this function crashes the crash will occur before main() is entered. If you feel comfortable answering these questions, then rest assured that your chances of impressing any interviewer are very high. Be prepared to know basic computer science concepts such as data structures, search and sort algorithms, basic database concepts, etc. The client's needs will determine what particular branch of computer science you have to be familiar with, but you should always be ready to implement the stock, the queue, and the linked list data structures with either C or C++ programming languages. And know how to write your own version of strcpy (string copy) in C programming language since very often they ask you to do that."},"aioseo_meta_data":{"post_id":"37","title":null,"description":null,"keywords":null,"keyphrases":null,"primary_term":null,"canonical_url":null,"og_title":null,"og_description":null,"og_object_type":"default","og_image_type":"default","og_image_url":null,"og_image_width":null,"og_image_height":null,"og_image_custom_url":null,"og_image_custom_fields":null,"og_video":null,"og_custom_url":null,"og_article_section":null,"og_article_tags":null,"twitter_use_og":false,"twitter_card":"default","twitter_image_type":"default","twitter_image_url":null,"twitter_image_custom_url":null,"twitter_image_custom_fields":null,"twitter_title":null,"twitter_description":null,"schema":{"blockGraphs":[],"customGraphs":[],"default":{"data":{"Article":[],"Course":[],"Dataset":[],"FAQPage":[],"Movie":[],"Person":[],"Product":[],"ProductReview":[],"Car":[],"Recipe":[],"Service":[],"SoftwareApplication":[],"WebPage":[]},"graphName":"","isEnabled":true},"graphs":[]},"schema_type":null,"schema_type_options":null,"pillar_content":false,"robots_default":true,"robots_noindex":false,"robots_noarchive":false,"robots_nosnippet":false,"robots_nofollow":false,"robots_noimageindex":false,"robots_noodp":false,"robots_notranslate":false,"robots_max_snippet":null,"robots_max_videopreview":null,"robots_max_imagepreview":"large","priority":null,"frequency":null,"local_seo":null,"breadcrumb_settings":null,"limit_modified_date":false,"ai":null,"created":"2021-03-13 19:48:22","updated":"2025-08-17 18:40:28","seo_analyzer_scan_date":null},"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}]}}