C++ Interview Questions

By David Maisonave

www.axter.com

 

Describe the three main parts of OO.

  1. polymorphism
  2. encapsulation
  3. inheritance

 

What is an abstract class?

  1. A class that has at least one pure virtual function
  2. Can not be instantiated

 

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?

  1. export
  2. mutable
  3. types (wchar_t)

 

What is the difference between a pointer and a reference variable?

  1. A reference must be initialized to point to an object.
  2. A reference can not change it’s pointer
  3. Pointer arithmetic can not be performed on a reference

 

When should a function be declared with a pointer argument over a reference argument?

  1. When the argument needs to change what it’s pointing to.
  2. When the object needs to be deleted or created

 

 


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?

  1. 'Polymorphism is the capability of different objects to react in an individual manner to the same message.'
  2. Polymorphism is the object-oriented buzz-word for dynamic binding, which itself is an OO buzz-word
  3. Polymorphism refers to the capability of an operation to operate on different entities and to exhibit behavior appropriate for the entity operated on. This is not directly related to object oriented programming although object oriented programming supports a certain kind of polymorphism, namely dynamic polymorphism or late binding: An operation accepts objects and sends messages to them (in C++ these are virtual functions, in other language this may be realized differently). The objects respond to the messages according to their specific definition, in C++ according to their class. There are other kinds of polymorphism, eg. static polymorphism which is resolved at compile time: The operation just uses functions on its arguments (these may be methods in C++ but it can be just functions as well) which are defined to do the right thing for the arguments they get passed. In C++ this kind of polymorphism is realized as templates and there is no direct counterpart in object oriented programming. Another kind of polymorphism, neither supported by C++ nor by object oriented programming is the use of higher order functions in functional programming languages: The higher order function operates on other function to achieve a result depending on the functions it gets passed.
  4. polymorphism (pol`e-mor’fiz-?m) noun:   In an object-oriented programming language, the ability to redefine a routine in a derived class (a class that inherited its data structures and routines from another class). Polymorphism allows the programmer to define a base class that includes routines that perform standard operations on groups of related objects, without regard to the exact type of each object. The programmer then redefines the routines in the derived class for each type, taking into account the characteristics of the object.
  5. Parmetric polymorphism is exclusively a compile-time feature.

 


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