C++ Interview Questions
By
www.axter.com
Describe the three
main parts of OO.
What is an abstract
class?
Why use virtual
destructors?
Abstract class should always have a virtual destructor, unless absolutely sure that the descendant class will not be deleted via base class pointer.
This is true even if the descendant class has no objects to destroy. Otherwise according to the C++ standard, it will result in undefined behavior.
Section 5.3.5 in the Standard:
"3. In the first alternative (delete object), if the static type of the operand is different from its dynamic type, the static type shall be a base class of the operand's dynamic type and the static type shall have a virtual destructor or the behavior is undefined."
What is the difference between putting the
initialization of an object in the initialization part of the constructor
versus in the body of the constructor?
What is the
difference between creating an object using new and creating an object using malloc?
New calls the object’s constructor and malloc does not.
Any object created with new must be freed using delete.
What’s the difference
between a copy constructor and an assignment operator?
A copy constructor initializes un-initialized variables, where-as an assignment operator must deal with well constructed objects.
An assignment operator should protect itself against self assignment. (check if equal to this)
A copy constructor can copy an object that has constant types and/or reference types. In general, an assignment operator can not correctly copy an object that has reference and/or constant types.
What is coupling?
Coupling is the degree to which software components depend on each other.
What is abstract
coupling?
Given a class X that maintains a reference to an abstract class Y, class X is said to be abstractly coupled to Y. It’s called abstract coupling because X refers to a type of object, and not a concrete object.
What is "cohesion and coupling"?
Have any new keywords
been added to C++? What about types?
What is the
difference between a pointer and a reference variable?
When should a
function be declared with a pointer argument over a reference argument?
Explain what is the difference between dynamic_cast to a pointer vs. dynamic_cast to a reference.
What possible things can happen when operator new fails to
allocate memory?
Unless an allocation function is declared with an empty exceptionspecification (15.4), throw(), it indicates failure to allocate storage by throwing a bad_alloc exception (clause 15, 18.4.2.1); it returns a non-null pointer otherwise.
What is polymorphism?
Why multiple inheritance?
What are mixins?
A class that provides some, but not all of the implementation for a virtual base class is often called a mixin.
What is an adapter?
A component that modifies the interface of another component is called an adaptor.
A usage example would be using an adapter to get std::string to work with code that access MFC-CString functions. An adaptor can be used to get the constant char pointer.
What is a bridge?
The Bridge pattern isn't just for establishing/changing implementation
at run-time, it can also be used to help reduce the total number of
classes necessary in the hierarchy. For example if we had 3 classes
derived from IA and 3 derived from A, then we can combine them to form 9
differently behaved objects. Using multiple inheritance, we avoid the
Bridge pattern, but end up needing 9 sub-classes...
What is a Pimpl design?
A Pimpl allows for hiding an objects declaration and implementation behind a forward declared class pointer. It’s used to reduce compile time dependencies.
What is layering?
It’s the process of building one class on top of another class by having the layering class contain an object of the layered class as a data member.
Layering is also known as composition, containment, and embedding.
What might I look for
to consider an association between objects as an aggregation?
The lifetime of the aggregate object and its owner are identical. An aggregate object avoids exposure of its implementation.
What are the different forms of aggregation?
How are the lifetimes
of containers and contained objects related in the various forms of
aggregation?
The lifetime of the aggregate object and its owner are identical
What does
"mutable" mean?
An object that can be modified inside a constant type member function.
Explain the Liskov Substition Principle.
The is-a rule for public base class.
What is a virtual
base class?
A virtual base class is a class that has one or more virtual functions.
How is a pure virtual
member function defined?
Make the function equaled to zero in the class declaration, and use the virtual keyword.
What is bit slicing
or object slicing?
When a derived class has parts sliced off. This occurs when a derived class is passed by value via the base type.
What is the dominance
rule?
For virtual functions the dominance rule is what guarantees that the same function is called independently of the static type of the pointer, reference, or name of the object for which it is called
Suppose I have a class B that derives from class A and I wanted to reuse some, but not all of the methods of B. How would I restrict access to the superclass' methods selectively?
Differentiate between
subtyping and subclassing.
How can subtyping and subclassing be
approximated or implemented in C++?
Are you familiar with any design methodology? Booch? OMT? Use cases?
What is an Abstract
Factory and why would I want to use one?
An interface that allows for the creation of objects without specifying the concrete class.
See following links for more C++ Interview Questions:
http://www.techinterviews.com/index.php?cat=2
http://www.handheldfriendly.net/blb/resume/quest2.html
http://www.vninformatics.com/forum/?action=msg&msg=1023409348#1023409348
http://sure.org.ru/immigr/interview/faq_cpp.html
http://www.cprogramming.com/cgi-bin/quiz.cgi