top of page
altaybrusan

C++ Questions! (Part 1)

Updated: Aug 9, 2022




In this series, a set of tips and tricks with regard to C++ are reviewed. These point are good for job interviews and in real world you may not face them so much often. Hope you enjoy it.






Q1.What is the output and why?




#include <iostream>
int main(int argc, char **argv)
 {
    std::cout << 25u - 50;
    return 0;
 }

Ans.

The answer is not -25. Rather, the answer (which will surprise many) is 4294967271, assuming 32 bit integers. Why?

In C++, if the types of two operands differ from one another, then the operand with the “lower type” will be promoted to the type of the “higher type” operand, using the following type hierarchy (listed here from highest type to lowest type):

  1. long double,

  2. double,

  3. float,

  4. unsigned long int,

  5. long int,

  6. unsigned int,

  7. int (lowest).

So, when operands are, as in our example, 25u (unsigned int) and 50 (int), the 50 is promoted to also being an unsigned integer (i.e., 50u).

Moreover, the result of the operation will be of the type of the operands. Therefore, the result of 25u - 50u will itself be an unsigned integer as well. So the result of -25 converts to 4294967271 when promoted to being an unsigned integer.

Q2. What is the error in the code below and how should it be corrected?




 my_struct_t *bar;
/* ... do stuff, including setting bar to point to a defined my_struct_t object ... */
memset(bar, 0, sizeof(bar));
 

Ans.

The last argument to memset should be sizeof(*bar), not sizeof(bar). sizeof(bar) calculates the size of bar (i.e., the pointer itself) rather than the size of the structure pointed to by bar.

The code can therefore be corrected by using sizeof(*bar) as the last argument in the call to memset.

A sharp candidate might point out that using *bar will cause a dereferencing error if bar has not been assigned. Therefore an even safer solution would be to use sizeof(my_struct_t). However, an even sharper candidate must know that in this case using *bar is absolutely safe within the call to sizeof, even if bar has not been initialized yet, since sizeof is a compile time construct.

Q3. What will i and j equal after the code below is executed? Explain your answer.



int i = 5;
int j = i++;

Ans.


After the above code executes, i will equal 6, but j will equal 5. Understanding the reason for this is fundamental to understanding how the unary increment (++) and decrement (--) operators work in C++. When these operators precede a variable, the value of the variable is modified first and then the modified value is used. For example, if we modified the above code snippet to instead say int j = ++i;, i would be incremented to 6 and then j would be set to that modified value, so both would end up being equal to 6. However, when these operators follow a variable, the unmodified value of the variable is used and then it is incremented or decremented. That’s why, in the statement int j = i++; in the above code snippet, j is first set to the unmodified value of i (i.e., 5) and then i is incremented to 6.

Q4. Assuming 'buf' is a valid pointer, what is the problem in the code below? What would be an alternate way of implementing this that would avoid the problem?


size_t sz = buf->size();
while ( --sz >= 0 )
 {
         /* do something */
 }
         

Ans.

The problem in the above code is that --sz >= 0 will always be true so you’ll never exit the while loop (so you’ll probably end up corrupting memory or causing some sort of memory violation or having some other program failure, depending on what you’re doing inside the loop).

The reasons that --sz >= 0 will always be true is that the type of sz is size_t. size_t is really just an alias to one of the fundamental unsigned integer type. Therefore, since sz is unsigned, it can never be less than zero (so the condition can never be true).

One example of an alternative implementation that would avoid this problem would be to instead use a for loop as follows:




for (size_t i = 0; i < sz; i++)
 {
         /* do something */
 }
         

Q5. Consider the two code snippets below for printing a vector. Is there any advantage of one vs. the other? Explain.


Option 1:


vector vec;
/* ... .. ... */
for (auto itr = vec.begin(); itr != vec.end(); itr++) {
         itr->print();
 }

Option 2:



vector vec;
/* ... .. ... */
for (auto itr = vec.begin(); itr != vec.end(); ++itr) {
         itr->print();
 }

Although both options will accomplish precisely the same thing, the second option is better from a performance standpoint. This is because the post-increment operator (i.e., itr++) is more expensive than pre-increment operator (i.e., ++itr). The underlying implementation of the post-increment operator makes a copy of the element before incrementing it and then returns the copy. That said, many compilers will automatically optimize the first option by converting it into the second.

Q6. Implement a template function IsDerivedFrom() that takes class C and class P as template parameters. It should return true when class C is derived from class P and false otherwise.

This question tests understanding of C++ templates. An experienced developer will know that this is already a part of the C++11 std library (std::is_base_of) or part of the boost library for C++ (boost::is_base_of). Even an interviewee with only passing knowledge should write something similar to this, mostly likely involving a helper class:




template<typename D, typename B>
class IsDerivedFromHelper
 {
    class No { };
    class Yes { No no[3]; };
    
    static Yes Test( B* );
    static No Test( ... );
public:
    enum { Is = sizeof(Test(static_cast<D*>(0))) == sizeof(Yes) };
    
 };
    
template <class C, class P> 
bool IsDerivedFrom() {
    return IsDerivedFromHelper<C, P>::Is;
 }

class No { }; - defines a class, which will have an undefined size, which won't be zero (mandated by the standard)

class Yes { No no[3]; }; - defines another class, Yes which will be at least 3 times as big as a No. So they are guaranteed to be different sizes.

static Yes Test( B* ); - declare a function which returns a Yes, but don't give it a definition (we don't need one). It will match any pointer argument which is pointing to an object derived from B.

static No Test( ... ); - declare a function which returns a No (smaller, remember?). It's an overload which will be selected if the more specific one (above) cannot be satisfied. It accepts any argument (but the other version will be selected by overload resolution in preference if the argument is derived from B).

sizeof(Test(static_cast<D*>(0))) deduces the size of the object returned by Test when passes a pointer to a D. If D is derived from B, it will be a Yes, otherwise it will be a No.

Because the 'call' is made in a non-deduced context, it is not evaluated (called), just selected and return type deduced.

enum { Is = sizeof(Test(static_cast<D*>(0))) == sizeof(Yes) };

What this is doing in a nutshell, is saying:

"declare a constant called Is, which will be true if the result of calling Test with a D* is a type that happens to be the same size as a Yes. And it will be false if the result of the call happens to be a type which is a different size. Because of the two overloads, above, when D is a B, or anything derived from it, Test(D*) will select the Yes version, which will return a Yes, which is obviously the same size as a Yes. If this overload is not selected, the more permissive (but lower priority) one will be. That one returns a No, which obviously won't be the same size as a Yes (by definition)."

Why is Test(...) 'lower priority' than Test(B*) (in terms of overload resolution)? Because it just is. The standard defines it to be.

Finally:

template <class C, class P> bool IsDerivedFrom() { return IsDerivedFromHelper<C,P>::Is; }

This creates a function which will return the result of the above test for any two class types, C and P. This it will return true if P is derived from a C and false otherwise.

Q7. Implement a template boolean IsSameClass() that takes class A and B as template parameters. It should compare class A and B and return false when they are different classes and true if they are the same class.

Ans.


template <typename T, typename U>
struct is_same
{
    static const bool value = false;
};
template <typename T>
struct is_same<T, T>
{
    static const bool value = true;
};
template <class A, class B>
bool IsSameClass() {
    return is_same<A, B>::value;
}

Q8. Is it possible to have a recursive inline function?

Ans.

Although you can call an inline function from within itself, the compiler may not generate inline code since the compiler cannot determine the depth of recursion at compile time. A compiler with a good optimizer can inline recursive calls till some depth fixed at compile-time (say three or five recursive calls), and insert non-recursive calls at compile time for cases when the actual depth gets exceeded at run time.

Q9. What is the output of the following code:



#include <iostream>
class A {
public:
    A() {}
    ~A() {
        throw 42;
    }
 };
int main(int argc, const char * argv[]) {
    try {
        A a;
        throw 32;
    } catch(int a) {
        std::cout << a;
    }
 }

Ans.

This program will terminate abnormally. throw 32 will start unwinding the stack and destroy class A (this is because the object itself is defined inside the try block). The class A destructor will throw another exception during the exception handling, which will cause program to crash.

Q10. You are given library class Something as follows.




class Something {
public:
    Something() {
        topSecretValue = 42;
    }
    bool somePublicBool;
    int somePublicInt;
    std::string somePublicString;
private:
    int topSecretValue;
 };
    

Implement a method to get topSecretValue for any given Something* object. The method should be cross-platform compatible and not depend on sizeof (int, bool, string).

Ans.

Create another class which has all the members of Something in the same order, but has additional public method which returns the value. Your replica Something class should look like:


class SomethingReplica {
public:
    int getTopSecretValue() { return topSecretValue; }
    bool somePublicBool;
    int somePublicInt;
    std::string somePublicString;
private:
    int topSecretValue;
 };

Then, to get the value:


int main(int argc, char argv[])
{
    Something a;
    SomethingReplica* b = reinterpret_cast<SomethingReplica*>(&a);
    std::cout << b->getTopSecretValue();
}


It’s important to avoid code like this in a final product, but it’s nevertheless a good technique when dealing with legacy code, as it can be used to extract intermediate calculation values from a library class.

Q11. When you should use virtual inheritance?

Ans.


While it’s ideal to avoid virtual inheritance altogether (you should know how your class is going to be used) having a solid understanding of how virtual inheritance works is still important:

So when you have a class (class A) which inherits from 2 parents (B and C), both of which share a parent (class D), as demonstrated below:


 #include <iostream>
class D {
public:
    void foo() 
    {
        std::cout << "Foooooo" << std::endl;
    }
 };
class C:  public D {};
class B:  public D {};
class A: public B, public C {};

int main(int argc, const char * argv[]) {
    A a;
    a.foo();
 } 

If you don’t use virtual inheritance in this case, you will get two copies of D in class A: one from B and one from C. To fix this you need to change the declarations of classes C and B to be virtual, as follows:



class C:  virtual public D { };
class B:  virtual public D { };

Q12. Is there a difference between class and struct?


Ans.

The only difference between a class and struct are the access modifiers. Struct members are public by default; class members are private. It is good practice to use classes when you need an object that has methods and structs when you have a simple data object.

Q13. What is the output of the following code?




#include <iostream>
int main(int argc, const char * argv[]) {
    int a[] = {1, 2, 3, 4, 5, 6};
    std::cout << (1 + 3)[a] - a[0] + (a + 1)[2];
 }
    

Ans.


The above will output 8, since:

(1+3)[a] is the same as a[1+3] is equal to 5, a[0] is qual to 1, (a + 1)[2] is the same as a[3] is equal to 4.This question is testing pointer arithmetic knowledge, and the magic behind square brackets with pointers.

While some might argue that this isn’t a valuable question as it appears to only test the capability of reading C constructs, it’s still important for a candidate to be able to work through it mentally; it’s not an answer they’re expected to know off the top of their head, but one where they talk about what conclusion they reach and how.


Q14. What is the output of the following code:




#include <iostream>
class Base {
    virtual void method() {std::cout << "from Base" << std::endl;}
public:
    virtual ~Base() {method();}
    void baseMethod() {method();}
 };
class A : public Base {
    void method() {std::cout << "from A" << std::endl;}
public:
    ~A() {method();}
 };
int main(void) {
    Base* base = new A;
    base->baseMethod();
    delete base;
    return 0;
 }

Ans.

The above will output:


from A
from A
from Base

The important thing to note here is the order of destruction of classes and how Base’s method reverts back to its own implementation once A has been destroyed.

Q15. Explain the volatile and mutable keywords?

Ans.

The volatile keyword informs the compiler that a variable may change without the compiler knowing it. Variables that are declared as volatile will not be cached by the compiler, and will thus always be read from memory.

The mutable keyword can be used for class member variables. Mutable variables are allowed to change from within const member functions of the class.

Q16. How many times will this loop execute? Explain your answer.




unsigned char half_limit = 150;
for (unsigned char i = 0; i < 2 * half_limit; ++i)
 {
    // do something;
 }

Ans.


If you said 300, you would have been correct if i had been declared as an int. However, since i was declared as an unsigned char, the correct answer is that this code will result in an infinite loop.

Here’s why:

The expression 2 * half_limit will get promoted to an int (based on C++ conversion rules) and will have a value of 300. However, since i is an unsigned char, it is represented by an 8-bit value which, after reaching 255, will overflow (so it will go back to 0) and the loop will therefore go on forever.

Q17. How can you make sure a C++ function can be called as e.g. void foo(int, int) but not as any other type like void foo(long, long)?


Ans.

Implement foo(int, int)


 void foo(int a, int b) 
 {
// whatever
 }

…and delete all others through a template:



 template <typename T1, typename T2> 
 void foo(T1 a, T2 b) = delete;
 

or without the delete keyword:



template <class T, class U> 
void f(T arg1, U arg2);
template <>
void f(int arg1, int arg2)
 {
    //...    
 } 

Q18. What is the problem with the following code?



class A
{
    public:
     A() {}
     ~A(){}
 };
class B: public A
 {
    public:
     B():A(){}
     ~B(){}
 };
int main(void)
{
  A* a = new B();
  delete a;
}
  

Ans.

The behavior is undefined because A’s destructor is not virtual. From the spec:

( C++11 §5.3.5/3 ) if the static type of the object to be deleted is different from its dynamic type, the static type shall be a base class of the dynamic type of the object to be deleted and the static type shall have a virtual destructor or the behavior is undefined.

Q19. Are you allowed to have a static const member function? Explain your answer.

Ans.

A const member function is one which isn’t allowed to modify the members of the object for which it is called. A static member function is one which can’t be called for a specific object.

Thus, the const modifier for a static member function is meaningless, because there is no object associated with the call.

A more detailed explanation of this reason comes from the C programming language. In C, there were no classes and member functions, so all functions were global. A member function call is translated to a global function call. Consider a member function like this:

void foo(int i);

A call like this:

obj.foo(10);

…is translated to this:

foo(&obj, 10);

This means that the member function foo has a hidden first argument of type T*:

void foo(T* const this, int i);

If a member function is const, this is of type const T* const this:

void foo(const T* const this, int i);

Static member functions don’t have such a hidden argument, so there is no this pointer to be const or not.

Q20. What is a storage class?

Ans.

A class that specifies the life and scope of its variables and functions is called a storage class.

In C++ following the storage classes are supported: auto, static, register, extern, and mutable.

Note, however, that the keyword register was deprecated in C++11. In C++17, it was removed and reserved for future use.

Q21. How can a C function be called in a C++ program?

Using an extern "C" declaration:



 //C code
void func(int i)
 {
//code
 }
void print(int i)
 {
//code
 }
//C++ code
extern "C"{
void func(int i);
void print(int i);
 }
void myfunc(int i)
 {
   func(i);
   print(i);
 } 

Q22. What will be the output of the following program?



 #include <iostream>
struct A
 {
    int data[2];
    A(int x, int y) : data{x, y} {}
    virtual void f() {}
 };
int main(int argc, char **argv)
 {
    A a(22, 33);
    int *arr = (int *) &a;
    std::cout << arr[2] << std::endl;
    return 0;
 }
     

Ans.

In the main function the instance of struct A is treated as an array of integer values. On 32-bit architectures the output will be 33, and on 64-bit architectures it will be 22. This is because there is virtual method f() in the struct which makes compiler insert a vptr pointer that points to vtable (a table of pointers to virtual functions of class or struct). On 32-bit architectures the vptr takes 4 bytes of the struct instance and the rest is the data array, so arr[2] represents access to second element of the data array, which holds value 33. On 64-bit architectures the vptr takes 8 bytes so arr[2] represents access to the first element of the data array, which holds 22.

This question is testing knowledge of virtual functions internals, and knowledge of C++11-specific syntax as well, because the constructor of A uses the extended initializer list of the C++11 standard.



2 views0 comments

Recent Posts

See All

smart pointers

The code we just described is fully functional, but, it can be strengthened, specifically with the function, AlbumDao::albums(). In this...

Comments


bottom of page