You are on page 1of 9

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.
 

You might also like