DEV Community

Komal Singh
Komal Singh

Posted on

C++ Common Interview Questions

Alt Text

What is this keyword and its use?

A very common concept used frequently in C++ Code. Let's see what are the use and what it is.
Each object gets its own copy of data members and all objects share a single copy of member functions.
Then now question is that if only one copy of each member function exists and is used by multiple objects, how are the proper data members are accessed and updated?
The compiler supplies an implicit pointer along with the names of the functions as ‘this’.
The ‘this’ pointer is passed as a hidden argument to all nonstatic member function calls and is available as a local variable within the body of all nonstatic functions. ‘this’ pointer is not available in static member functions as static member functions can be called without any object (with class name).
The following are the uses of This Pointer:
1) When local variable’s name is same as member’s name
2) To return a reference to the calling object

(Answer Credits: GFG)

What are Serialization and Deserialization
Serialization is a mechanism to convert an object into a sequence of bytes so that it can be stored in memory. The byte stream, once created, also can be streamed across a communication link to a remote receiving end. The reverse of serialization is called deserialization, where the data in the byte stream is used to reconstruct it to its original object form. Apart from object persistence, this mechanism is particularly useful in transmitting object information in serialized form, say, to the server which, on receiving it, can deserialize and create the object format which is its original form.

What are virtual functions – Write an example?

Virtual functions are used with inheritance, they are called according to the type of the object pointed or referred, not according to the type of pointer or reference. In other words, virtual functions are resolved at runtime. Virtual keyword is used to make a function virtual.
Suppose we have the same function name in both the base and derive class and we want to access the derived class method, we can achieve this by making the base class method virtual.

For example, in the following program, bp is a pointer of type Base, but a call to bp->show() calls show() function of Derived class because bp points to an object of Derived class.

#include<iostream> 
using namespace std; 

class Base { 
public: 
    virtual void show() { cout<<" In Base \n"; } 
}; 

class Derived: public Base { 
public: 
    void show() { cout<<"In Derived \n"; }  
}; 

int main(void) {    
    Base *bp = new Derived;      
    bp->show();  // RUN-TIME POLYMORPHISM 
    return 0; 
} 

O/P: In Derived

*What is Pure Virtual function? *

A pure virtual function (or abstract function) in C++ is a virtual function for which we don’t have an implementation, we only declare it. A pure virtual function is declared by assigning 0 in its declaration. See the following example.

class virtualexample 
{    
    // Data members of class 
public: 
    // Pure Virtual Function 
    virtual void show() = 0; 

   /* Other members */
};

Will be adding some more C++ based interview questions. Do suggest if you have come across some interesting C++ questions in interviews.

Top comments (2)

Collapse
 
jeikabu profile image
jeikabu • Edited

I've taken (or written) C++ interview tests on and off for nigh 20 years and this feels like it's from 2000-ish. Actually, I just took one last month. The problems you'll run into vary widely depending on the level of the position and the target industry, but I usually run into most of the following:

  • Basics: sort a thing, reverse a string, write some trivial function, etc.
  • Design a class: either something generic like a string/stack or specific to a particular problem.
  • Template "shenanigans" (meta-programming): compile-time evaluation of something, or type-specific implementations.
  • Performance: looking for more in-depth knowledge about compilers and optimisation, SIMD, STL container big-O behaviour, or the like.
  • Memory: solve the memory leak or use smart pointers (including weak to avoid a cycle).
  • Virtual functions: specifically virtual destructor, or how vtables are commonly used.

In general, I'd stick to C++11/14 as employers I've seen have yet to widely embrace 17/20.

As someone whose written tests my personal favourite questions:

  • Show mastery of pointers
  • Trick candidates into returning a reference to a local
  • Memory leaks, memory leaks, memory leaks
Collapse
 
komalsingh1 profile image
Komal Singh

Thanks @jeikabu These are a great set of questions. I will cover these in the next blog. Thanks a lot!