Hide menu

TDDD38 Advanced Programming in C++

Theory Questions


The theory questions below are examples of questions given in the past, and possibly examples of questions to be given in the future...

When answering theory questions, strive to be as accurate as possible and to use correct concepts and terminology.

Some questions may actually belong to several of the given categories.

1. Syntax



1.1  Explain the two different uses of the keyword typename.
1.2  What is the difference, if any, using keyword class or typename when declaring a template type parameter?
1.3  Give two different, non-deprecated, uses of the keyword static and describe the effect of static in each case.
1.4  Explain the keyword volatile. Also, give a reasonable example of when volatile may be used.
1.5  Explain the keyword mutable, and describe a situation when mutable can be used.
1.6  Where and why can the keyword explicit be used?
1.7  

Explain the semantics of the keyword const in the declaration of the member function get() in the code fragment below. Also, how does it affect the usability of get().

   struct S { int get() const; };
1.8  

Explain why one uses the character '&' in the declarations of the arguments to the following function. T is some type.

   istream& operator>>(istream&, T&);
1.9  

Explain the semantics and typical purpose when declaring a function parameter 'const T& x', where T is some type.

2. Style



2.1  

Study the following for loop:

   for (vector<int>::iterator it = v.begin(); it < v.end(); it++) {
      cout << *it << endl; 
   } 

There is no outright error, but which stylistic problems can you think of? How would you rewrite the code to make it, possibly, better?

2.2  Explain why to prefer prefix operator++ to postfix operator++ in situations where the context allows us to choose either one.
2.3  What is a policy class? What is, in general, its purpose?
2.4  What is the Non-Virtual Interface (NVI) design pattern? What is its purpose?
2.5  

There is a coding guideline stating: A base class destructor should either be public and virtual, or protected and nonvirtual.

There is also a corollary: Always write a destructor for a base class, because... complete why a base class destructor always should be written!

3. Basics



3.1  What is an aggregate?
3.2  What is list initialization?

4. Namespaces



4.1  In which different ways can names declared within a namespace be made accessible outside the namespace?
4.2  What is a using directive? What is a using declaration?
4.3  What is a ADL (argument dependent lookup)? What relation does ADL have to namespaces

5. Parameter passing



5.1  

Suppose we have a type T. When passing an object of type T to a function, how would you declare the parameter type in the following two cases? Motivate!

— T is a simple type, such as e.g. int, and the parameter is used as an "in" parameter.

— T is a complicated type, e.g. a class type, and the parameter is used as an "in" parameter.

5.2  

Explain how the calls of the function fun() below differ. 't' is a value, or object, of type T.

   fun(t);   // if the declaration of fun is: fun(T);
   fun(t);   // if the declaration of fun is: fun(T&);
   fun(t),   // if the declaration of fun is: fun(const T&);
   fun(&t);  // if the declaration of fun is: fun(T*);

Discuss semantics and consequences, e.g. regarding performance, in these four cases.

5.3  

C++ have two fundamental parameter passing modes, which?

What is ment by "passing by reference to const"? Which is the purpose of that?

5.4  

Below are four overloadings of a function fun.

   fun(T); 
   fun(T&);
   fun(T&&);
   fun(const T&);

Which of these can co-exist, without creating overload resloution ambiguity?

Which of those that can to co-exist will be chosen for different kind of arguments, such as lvalue, rvalue (xvalue, prvalue), const, non-const, etc.?

6. Exception handling



6.1  

In C++ exceptions should be "thrown by value and caught by reference". Explain and motivate that citation.

Hint: The alternative to "throw by value" would be to "throw by pointer".

6.2  How is strong exception safety defined?
6.3  What is meant by exception neutral?
6.4  What is a function try-block?
6.5  Give a key advantage and a key disadvantage of using an ellipsis in a catch clause, i.e. catch (...).
6.6  What happens if no catch handler in a try block matches the type of a thrown object?
6.7  What happens if several catch handlers of a try block matches the type of a thrown object?
6.8  

Which is the earliest possible resumption point in a program, after an exception has been thrown?

Which is the latest resumption point, if the program is to at all survive?

6.9  

What happens to automatic objects that have been constructed in a try block when that block throws an exception? Assume that more than one such object has been constructed.

6.10  What happens if an exception is thrown in a constructor and not handled there?
6.11  

Suppose an exception may be thrown in a constructor. Discuss possibilities to handle such an exception within the constructor, or not, and how this affect the object in question and the context where the object is to be created.

6.12   What destructors are called during exception handling.
6.13  

A function-try-block could be used in a constructor. What would usually be the intended purpose of that?

Why does a function-try-block in a constructor often show not to be especially useful?

6.14  

Suppose we have a function fun(), which may throw an exception of type std::bad_alloc, of type std::exception, or an exception not belonging to the std::exception hierarchy at all. The calling program shall take different actions for each of these three categories of exceptions. Show how a try block and handlers is declared to allow for this.

7. Types and type conversions



7.1  

When and how does up-cast and down-cast, respectively, occur in C++?

What possible problems, if any, are associated with these kind of casts?

7.2  Give a context where cross-cast can occur and give an example on how cross-cast could occur in that context.
7.3  Explain the concepts static type and dynamic type.
7.4  In which ways can type conversion for a class type be defined?
7.5  

How can implicit conversions be provided for a class type?

How can the use of implicit conversion be eliminated?

7.6  

What is required for using dynamic_cast?

For what purposes can dynamic_cast be used?

7.7  

Compare and contrast the "new" type conversions, such as static_cast, dynamic_cast, etc., with the traditional type conversions, i.e. C cast (syntax: '(type) expr') and function call form (syntax: 'type(expr)').

7.8  

Compare and contrast dynamic_cast and typeid expressions, when used in the context of a polymorphic class hierarchy.

8. Overloading and related



8.1  Explain why there is an extra argument when declaring the postfix version of operator++.
8.2  Explain why it is appropriate to declare operator+= in a class that declares operator= and operator+.
8.3  Compare and contrast overloading of a function and definition of a template for the function, e.g. for which types they work, etc.?
8.4  

When declaring an operator for a class we have, in most cases, several options, e.g. concerning membership and overloading in several versions.

Give one good reason for choosing membership over non-membership, when this is an option.

What is usually the purpose of overloading an operator for a class type in several versions?

8.5  

A function template can be overloaded with both other function templates and with ordinary, non-template functions. There are basically four steps (at most) a compiler performs to find a match for a function call, where the last step is to conclude that there were no match at all for the call, or there were ambiguity because several functions matched, but none of them was a unique best match.

Which are, in order, the three first steps in function overload resolution?

8.6  

Some operators cannot be overloaded at all, some operators must be overloaded as member functions, but for most overloadable operators there is a choice between member and non-member.

What you would chose, and why, if you were to overload operator<< to print objects of some type to an output stream.

8.7  

Explain what is "wrong" with the following declaration of operator<<.

   class C {
   public:
      ostream& operator<<(ostream& os) {
         return os << i;
      }
   private:
      int i;
   };
8.8  

Operator new is related to memory allocation. It has actually three forms, a plain form, a no-throw form, and a placement form. Describe each of these three forms.

9. Classes



9.1  

Which are the, so called, special member functions, and what is their respective purpose/use?

9.2  

The copy constructor is a, so called, special member function of a class. Elaborate on what's special about it and what it (usually) do.

9.3  

The copy assignment operator is a, so called, special member function of a class. Elaborate on what's special about it.

9.4  

Explain why it is appropriate to declare the copy constructor and the copy assignment operator in classes with dynamic memory allocation. What is the alternative?

9.5  Why should one always declare a member function const whenever possible?
9.6  Some operators can only be declared as non-static member functions. Which are they? Also, give a good reason for this rule.
9.7  Give two good reasons for a class normally to have a default constructor.
9.8  

Which operators are implicitly declared for class types? Describe shortly each operators purpose/use.

Note: There are quite a lot of operators defined, you may restrict to those which are more specifically related to class types.

9.9  Which type has the this pointer in a non-const member function, expressed in plain English or Swedish? What is the concrete implication of this?
9.10   Give at least two different cases when a class member must be initialized in the constructor initializer list.
9.11  

When initializing a data member of a class in a constructor one can either use a member initializer, or use assignment within the constructor body.

Discuss differences, restrictions, guidelines, etc., for these two ways of initializing class data members.

9.12   Which requirements must hold for a member function call to be bound dynamically?
9.13  

Below is a fragmentary definition of a class C given.

   class C {
   public:
      explicit C(const C&);
   };

Explain what the declarator explicit mean.

What are the consequences of declaring this specific member function explicit?

9.14  

Given the fragmentary class definition below, which of the class special member functions can be expected to have to be declared by user? Motivate! (T is some type.)

   class C {
      ... 
      private:
         T* v; 
   };

10. Derived classes, inheritance and polymorphism



10.1  Explain public inheritance and the effects of using it.
10.2  Explain private inheritance and the effects of using it.
10.3  Explain protected inheritance and the effects of using it.
10.4  

Given a base class Base, give a scenario for showing the difference(s) between protected inheritance and private inheritance, and point out the difference(s).

10.5  

What is an abstract class? How is an abstract class defined?

Also, give at least one important feature of an abstract class.

10.6  

What is a polymorphic class?

What specific features are related to polymorphic classes/objects?

10.7  What is a virtual base class and what is the context and purpose of such classes?
10.8  Explain the concept of object slicing with words and by an example.
10.9  What is a virtual destructor? What is its purpose?
10.10  Why should one not call a virtual function in a constructor or in a destructor?
10.11  

Given three classes A, B and C, where B inherits from A and C inherits from B. Describe which code have access to the data member 'number' in class Test below.

   class Test {
   private:
      int number;
      friend class A;
   };
10.12  

Explain the implications and probable purpose of the follwing code fragment:

   struct X {
      virtual ~X() = 0;
   };

   X::~X(){}
10.13  

Suppose we use objects of some derived type in a program and we have, in some way, noticed that the destruction of such objects does not always work properly.

Give one explanation for such an error, and also a scenario where the error would occur?

10.14  

Suppose we have a base class Base and a derived class Derived, each having a member function with the same signature. Also suppose that we have a base class pointer (Base*) to a derived class object and we call this member function through that pointer. Discuss what determines which function is actually called, the base class member function or the derived class member function.

10.15  

There are many well-known pitfalls one might fall into when programming in C++. Explain what is wrong, and why, with the class C below.

   class C {
   public:
      A();
      ~A();
      virtual void memfun() = 0;
   };
10.16  

Suppose multiple inheritance is involved, but no virtual base classes. Describe the initialization order for the sub-objects of an object of derived type (ordinary data members are also sub-objects) and also when in the process constructors are invoked.

10.17  

How does the presence of virtual base classes affect the initialization order of the sub-objects of an object of derived type?

11. Templates and related



11.1  

What different main kind of template parameters are there? Give an example of each kind, shown in a template declaration.

11.2  

Give four examples of how (ordinary) function parameters and template parameters, either differ or have alike properties.

11.3  

Explain why it is appropriate to define a specialization for char*, given the following function:

   template<typename T>
   T min(const T& a, const T& b) {
      return (a < b) ? a : b;
   }
11.4  

Explain the difference between having a size argument for a data structur as a template parameter, as in

   template<typename T, int size> class Vector { ... };

compared to having it as a constructor argument, as in

   template<typename T> class Vector {
   public: 
      Vector(int size);
   };
11.5  

std::max is a function template with one template type parameter, specifying the arguments and return value type. Explain why the following call is not allowed, and describe workarounds for making the call possible.

   int i = max(a,j);  // a is a double, j is an int.
11.6  

Given the template declaration below, which requirements must an instantiation type for 'T' fulfill?

   template<class T> T fun(const T t);
11.7  

Explain what the declaration of 'p' in the following code fragment means. Also, why is the keyword typename used?

   template<typename T> class C { 
      typename T::M* p; 
   };
11.8  Which are the (two) most common reasons for explicitly specializing a template class or a template function?
11.9  Why should one avoid to specialize a function template? What is the often better alternative?
11.10  What is a primary class template? What is a partial specialization?
11.11  What is a template alias?
11.12  What is a dependent name?
11.13  What is a move semantics?
11.14  What is a perfect forwarding?
11.15  What is the purpose of the utility function std::move?
11.16  What is the purpose of the utility function std::forward?
11.17  What is the name rule?
11.18  Explain the reference collapsing rules, and when and how they are applied.
11.19  What is SFINAE?
11.20  What is a variadic template?
11.21  What is the meaning and purpose of emplacement in C++?
11.22  Which categories of type traits are there?

12. Standard Library



12.1  

What is the purpose of the standard library function adaptors?

Which are they?

Give examples of typical use, related to the standard library.

12.2  

The standard library algorithms have iterators as parameters, categorized by five iterator categories.

Which are these five iterator categories, which are the leading features of each category, and what do they actually specify in practice?

12.3  

The standard library algorithms have iterators as parameters, categorized by five iterator categories.

Relate the container iterators to the five iterator categories and elaborate some on the implications when attempting to apply a certain algorithm to a certain container type.

13. Function objects and lambda expressions



13.1  What is a function object?
13.2  To maximize the usability of a function object, what should one think of in general when defining it?
13.3   

Some components of the standard library accept either a function object, a lambda expression, or an ordinary function as argument, while some components accept only function objects. What is the reason for that?

13.4  What is a callable object? Give som examples.
13.5  Which function adaptors are there in the standard library?

Page responsible: Christoffer Holm
Last updated: 2012-12-17