Introduction
You maybe a great programmer who uses C/C++ everyday but if you really want that new job, it maybe a good idea to review the core principles of C/C++ before the interview. I have compiled a list of 10 questions that I feel you should know the answers to if you are applying for any C++ position.
You maybe a great programmer who uses C/C++ everyday but if you really want that new job, it maybe a good idea to review the core principles of C/C++ before the interview. I have compiled a list of 10 questions that I feel you should know the answers to if you are applying for any C++ position.
What is the difference between C and C++ ? Would you prefer to use one over the other ?
C is based on structured programming whereas C++ supports the object-oriented programming paradigm.Due to the advantages inherent in object-oriented programs such as modularity and reuse, C++ is preferred. However almost anything that can be built using C++ can also be built using C.
C is based on structured programming whereas C++ supports the object-oriented programming paradigm.Due to the advantages inherent in object-oriented programs such as modularity and reuse, C++ is preferred. However almost anything that can be built using C++ can also be built using C.
What are the access privileges in C++ ? What is the default access level ?
The access privileges in C++ are private, public and protected. The default access level assigned to members of a class is private. Private members of a class are accessible only within the class and by friends of the class. Protected members are accessible by the class itself and it's sub-classes. Public members of a class can be accessed by anyone.
The access privileges in C++ are private, public and protected. The default access level assigned to members of a class is private. Private members of a class are accessible only within the class and by friends of the class. Protected members are accessible by the class itself and it's sub-classes. Public members of a class can be accessed by anyone.
What is data encapsulation ?
Data Encapsulation is also known as data hiding. The most important advantage of encapsulation is that it lets the programmer create an object and then provide an interface to the object that other objects can use to call the methods provided by the object. The programmer can change the internal workings of an object but this transparent to other interfacing programs as long as the interface remains unchanged.
What is inheritance ?
Inheritance is the process of deriving classes from other classes. In such a case, the sub-class has an 'is-a' relationship with the super class. For e.g. vehicle can be a super-class and car can be a sub-class derived from vehicle. In this case a car is a vehicle. The super class 'is not a' sub-class as the sub-class is more specialized and may contain additional members as compared to the super class. The greatest advantage of inheritance is that it promotes generic design and code reuse.
What is multiple inheritance ? What are it's advantages and disadvantages ?
Multiple Inheritance is the process whereby a sub-class can be derived from more than one super class. The advantage of multiple inheritance is that it allows a class to inherit the functionality of more than one base class thus allowing for modeling of complex relationships. The disadvantage of multiple inheritance is that it can lead to a lot of confusion when two base classes implement a method with the same name.
What is polymorphism?
Polymorphism refers to the ability to have more than one method with the same signature in an inheritance hierarchy. The correct method is invoked at run-time based on the context (object) on which the method is invoked. Polymorphism allows for a generic use of method names while providing specialized implementations for them.
What do the keyword static and const signify ?
When a class member is declared to be of a static type, it means that the member is not an instance variable but a class variable. Such a member is accessed using Classname.Membername (as opposed to Object.Membername). Const is a keyword used in C++ to specify that an object's value cannot be changed.
When a class member is declared to be of a static type, it means that the member is not an instance variable but a class variable. Such a member is accessed using Classname.Membername (as opposed to Object.Membername). Const is a keyword used in C++ to specify that an object's value cannot be changed.
How is memory allocated/deallocated in C ? How about C++ ?
Memory is allocated in C using malloc() and freed using free(). In C++ the new() operator is used to allocate memory to an object and the delete() operator is used to free the memory taken up by an object.
Memory is allocated in C using malloc() and freed using free(). In C++ the new() operator is used to allocate memory to an object and the delete() operator is used to free the memory taken up by an object.
What is UML ?
UML refers to Unified Modeling Language. It is a language used to model OO problem spaces and solutions.
UML refers to Unified Modeling Language. It is a language used to model OO problem spaces and solutions.
What is the difference between a shallow copy and a deep copy ?
A shallow copy simply creates a new object and inserts in it references to the members of the original object. A deep copy constructs a new object and then creates in it copies of each of the members of the original object.
Here are some more interesting references on C++ interview preparation: be sure to check them out before your next interview. Good Luck!
The C++ Interview
These 40 questions and answers will help you land the assignment
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Ó 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. <ClassName>::<StaticMemberName>.
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.
What is an object in C++?An object is a package that contains related data and instructions. The data relates to what the object represents, while the instructions define how this object relates to other objects and itself.
What is a message?
A message is a signal from one object to another requesting that a computation take place. It is roughly equivalent to a function call in other languages.
What is a class?
A class defines the characteristics of a certain type of object. It defines what its members will remember, the messages to which they will respond, and what form the response will take.
What is an instance?
An individual object that is a member of some class.
What is a super-class?
Given a class, a super-class is the basis of the class under consideration. The given class is defined as a subset (in some respects) of the super-class. Objects of the given class potentially posses all the characteristics belonging to objects of the super-class.
What is inheritance?
Inheritance is property such that a parent (or super) class passes the characteristics of itself to children (or sub) classes that are derived from it. The sub-class has the option of modifying these characteristics in order to make a different but fundamentally related class from the super-class.
To what does message protocol refer?
An object’s message protocol is the exact form of the set of messages to which the object can respond.
What is polymorphism?
Polymorphism refers to the ability of an object to respond in a logically identical fashion to messages of the same protocol, containing differing types of objects. Consider 1 + 5 and 1 + 5.1. In the former, the message “+ 5″ is sent to an object of class integer (1). In the later, the message “+ 5.1″ is sent to the same integer object. The form of the message (its protocol) is identical in both cases. What differs is the type of object on the right-hand side of these messages. The former is an integer object (5) while the later is a floating point object (5.1). The receiver (1) appears (to other objects) to respond in the same way to both messages. Internally, however, it knows that it must treat the two types of objects differently in order to obtain the same overall response.
What are instance variables?
These represent an object’s private memory. They are defined in an object’s class.
What are class variables?
These represent a class’s memory which it shares with each of its instances.
What is a method?
A method is a class’s procedural response to a given message protocol. It is like the definition of a procedure in other languages.
In C++ what is a constructor? A destructor?
A constructors and destructors are methods defined in a class that are invoked automatically when an object is created or destroyed. They are used to initialize a newly allocated object and to cleanup behind an object about to be removed.
Compare and contrast C and C++.
Comparison: C++ is an extension to the C language. When C++ is used as a procedural language, there are only minor syntactical differences between them.
Contrast: When used as a procedural language, C++ is a better C because:
It vigorously enforces data typing conventions.
It allows variables to be defined where they are used.
It allows the definition of real (semantically significant) constants.
It allows for automatic pointer dereferencing.
It supports call-by-reference in addition to call-by-value in functions.
It supports tentative variable declarations (when the type and location of a variable cannot be known before hand.
As an object oriented language, C++ introduces much of the OOP paradigm while allowing a mixture of OOP and procedural styles.
What is operator overloading?
It is the process of, and ability to redefine the way an object responds to a C++ operator symbol. This would be done in the object’s class definition.
What is cin and cout?
They are objects corresponding to a program’s default input and output files.
What are the differences between a C++ struct and C++ class?
The default member and base class access specifiers are different.
This is one of the commonly misunderstood aspects of C++. Believe it or not, many programmers think that a C++ struct is just like a C struct, while a C++ class has inheritance, access specifiers, member functions, overloaded operators, and so on. Some of them have even written books about C++. Actually, the C++ struct has all the features of the class. The only differences are that a struct defaults to public member access and public base class inheritance, and a class defaults to the private access specifier and private base class inheritance. Getting this question wrong does not necessarily disqualify you because you will be in plenty of good company. Getting it right is a definite plus.
What is a default constructor?
A constructor that has no arguments or one where all the arguments have default argument values.
If you don’t code a default constructor, the compiler provides one if there are no other constructors. If you are going to instantiate an array of objects of the class, the class must have a default constructor.
What is a conversion constructor?
A constructor that accepts one argument of a different type.
The compiler uses this idiom as one way to infer conversion rules for a class. A constructor with more than one argument and with default argument values can be interpreted by the compiler as a conversion constructor when the compiler is looking for an object of the type and sees an object of the type of the constructor’s first argument.
What is the difference between a copy constructor and an overloaded assignment operator?
A copy constructor constructs a new object by using the content of the argument object. An overloaded assignment operator assigns the contents of an existing object to another existing object of the same class.
First, you must know that a copy constructor is one that has only one argument, which is a reference to the same type as the constructor. The compiler invokes a copy constructor wherever it needs to make a copy of the object, for example to pass an argument by value. If you do not provide a copy constructor, the compiler creates a member-by-member copy constructor for you.
You can write overloaded assignment operators that take arguments of other classes, but that behavior is usually implemented with implicit conversion constructors. If you do not provide an overloaded assignment operator for the class, the compiler creates a default member-by-member assignment operator.
What is a virtual destructor?
The simple answer is that a virtual destructor is one that is declared with the virtual attribute.
The behavior of a virtual destructor is what is important. If you destroy an object through a pointer or reference to a base class, and the base-class destructor is not virtual, the derived-class destructors are not executed, and the destruction might not be complete.
When is a template a better solution than a base class?
When you are designing a generic class to contain or otherwise manage objects of other types, when the format and behavior of those other types are unimportant to their containment or management, and particularly when those other types are unknown (thus the genericity) to the designer of the container or manager class.
Prior to templates, you had to use inheritance; your design might include a generic List container class and an application-specific Employee class. To put employees in a list, a ListedEmployee class is multiply derived (contrived) from the Employee and List classes. These solutions were unwieldy and error-prone. Templates solved that problem.
What is the difference between C and C++ ? Would you prefer to use one over the other ?
C is based on structured programming whereas C++ supports the object-oriented programming paradigm.Due to the advantages inherent in object-oriented programs such as modularity and reuse, C++ is preferred. However almost anything that can be built using C++ can also be built using C.
What are the access privileges in C++ ? What is the default access level ?
The access privileges in C++ are private, public and protected. The default access level assigned to members of a class is private. Private members of a class are accessible only within the class and by friends of the class. Protected members are accessible by the class itself and it’s sub-classes. Public members of a class can be accessed by anyone.
What is data encapsulation ?
Data Encapsulation is also known as data hiding. The most important advantage of encapsulation is that it lets the programmer create an object and then provide an interface to the object that other objects can use to call the methods provided by the object. The programmer can change the internal workings of an object but this transparent to other interfacing programs as long as the interface remains unchanged.
What is inheritance ?
Inheritance is the process of deriving classes from other classes. In such a case, the sub-class has an ‘is-a’ relationship with the super class. For e.g. vehicle can be a super-class and car can be a sub-class derived from vehicle. In this case a car is a vehicle. The super class ‘is not a’ sub-class as the sub- class is more specialized and may contain additional members as compared to the super class. The greatest advantage of inheritance is that it promotes generic design and code reuse.
What is multiple inheritance ? What are it’s advantages and disadvantages ?
Multiple Inheritance is the process whereby a sub-class can be derived from more than one super class. The advantage of multiple inheritance is that it allows a class to inherit the functionality of more than one base class thus allowing for modeling of complex relationships. The disadvantage of multiple inheritance is that it can lead to a lot of confusion when two base classes implement a method with the same name.
What is polymorphism?
Polymorphism refers to the ability to have more than one method with the same signature in an inheritance hierarchy. The correct method is invoked at run-time based on the context (object) on which the method is invoked. Polymorphism allows for a generic use of method names while providing specialized implementations for them.
What do the keyword static and const signify ?
When a class member is declared to be of a static type, it means that the member is not an instance variable but a class variable. Such a member is accessed using Classname.Membername (as opposed to Object.Membername). Const is a keyword used in C++ to specify that an object’s value cannot be changed.
How is memory allocated/deallocated in C ? How about C++ ?
Memory is allocated in C using malloc() and freed using free(). In C++ the new() operator is used to allocate memory to an object and the delete() operator is used to free the memory taken up by an object.
What is an explicit constructor?
A conversion constructor declared with the explicit keyword. The compiler does not use an explicit constructor to implement an implied conversion of types. Its purpose is reserved explicitly for construction.
What is the Standard Template Library?
A library of container templates approved by the ANSI committee for inclusion in the standard C++ specification.
An applicant who then launches into a discussion of the generic programming model, iterators, allocators, algorithms, and such, has a higher than average understanding of the new technology that STL brings to C++ programming.