Best C++ Interview Questions & Answers [Updated in 2021]

Best C++ Interview Questions & Answers in 2021
Best C++ Interview Questions & Answers in 2021

C++ is one of the predominant general-purpose, object-oriented programming (OOP) languages used to develop a technical and commercial software. Bjarne Stroustrup developed it in 1979 at Bell Labs.

Introduction

C++ is an extension of the C programming language. It is a high-level programming language that combines the features of both low-level and high-level languages. 

Even today, several companies offer jobs to candidates knowing C++. It is among the foremost challenging programming languages since when writing programs with C++, one has to do extensive thinking. But don’t worry, you can learn it easily, and to know more about the best C++ Interview Questions and Answers in 2021, just go through our blog post.

General Questions For The Basic/First Round Of Interview:

Q1: What is Object-Oriented Programming?

Object-Oriented Programming is a programming paradigm that is related to the real world. It is based on the concepts of objects. Objects contain data and information.

Q2: Name the basic components of OOP language.

The essential components of an object-oriented programming language are:

  • Objects
  • Classes
  • Inheritance
  • Polymorphism
  • Message passing
  • Dynamic Binding
  • Data abstraction and encapsulation

Q3: How is C++ different from Python?

The primary differences between C++ and Python are:

  • C++ is a compiled language and Python is an interpreted language. C++ is much faster than Python. 
  • C++ is more popular for embedded or enterprise applications whereas Python is more famous for machine learning and web design.  
  • C++ is more predictable because it is a strongly typed language. Python is a dynamically typed language, so; it is less predictable.

Q4. What is the importance of Inheritance in Object Oriented Languages?

The idea of classes leads to the idea of Inheritance in Object-Oriented languages. The inheritance relationship of real-world models is expressed by the concept of Inheritance in C++. For instance, cars, trucks inherit from vehicles. In Object-Oriented Languages, the concept of Inheritance provides a crucial extension to the concept of reusability of code.

Q5. What are virtual functions?

Virtual functions are integrated with Inheritance to ensure that the correct function has been deployed to point to a particular object. Instead of naming them according to the type of reference or pointer, virtual functions are named according to the kind of object being referred to. The functions are named with a virtual keyword in the base class.

Questions Asked In The Coding Round of the Interview [Updated in 2021]:

Q6. Suppose you have a two-dimensional array, write a program to check if any element of the array is zero. Then make its whole row and column zero. 

Solution

void rep_to_zeroes(vector<vector<int>>& matrix) {
  if (matrix.empty()) {
    return;
  }
 
  unordered_set<size_t> zero_rows;
  unordered_set<size_t> zero_cols;
 
  size_t rows = matrix.size();
  size_t cols = matrix[0].size();
 
  for (size_t p = 0; p < rows; ++p) {
    for (size_t q = 0; q < cols; ++q) {
      if (matrix[p][q] == 0) {
 
        if (zero_rows.find(p) == zero_rows.end()) {
          zero_rows.insert(p);  
        }
  if (zero_cols.find(q) == zero_cols.end()) {
          zero_cols.insert(q);
        }
      }
    }
  }
 
  for (size_t r : zero_rows) {
    for (size_t c = 0; c < cols; ++c) {
      matrix[r][c] = 0;
    }
  }
 
  for (size_t c : zero_cols) {
    for (size_t r = 0; r < rows; ++r) {
      matrix[r][c] = 0;
    }
  }
}
 
bool is_row_or_col_zero(vector<vector<int>>& matrix, int r, int c) {
  size_t rows = matrix.size();
  size_t cols = 0;
  if (rows > 0) {
    cols = matrix[0].size();
  }
 
  for (int p = 0; p < cols; ++p) {
    if (matrix[r][p] == 0) {
      return true;
    }
  }
 
  for(int p = 0; p < rows; ++p) {
    if (matrix[p][c] == 0) {
      return true;
    }
  }
 
  return false;
}
 
void verify(vector<vector<int>>& matrix) {
  auto mat_copy = matrix;
 
  rep_to_zeroes(matrix);
  size_t rows = matrix.size();
  size_t cols = 0;
  if (rows > 0) {
    cols = matrix[0].size();
  }
 
  for (int p = 0; p < rows; ++p) {
    for (int q = 0; q < cols; ++q) {
      if (is_row_or_col_zero(mat_copy, p, q)) {
        assert(matrix[p][q] == 0);
      }
    }
   }
}
 
int main(int argc, char const *argv[])
{
  vector<vector<int>> matrix = {
    {1, 5, 45, 0, 81},
    {6, 7, 2, 82, 8},
    {20, 22, 49, 5, 5},
    {0, 23, 50, 0, 92}
  };
  
  print_matrix(matrix);
  verify(matrix);
  print_matrix(matrix);
 
  matrix = create_random_matrix(5, 5);
  print_matrix(matrix);
  verify(matrix);
  print_matrix(matrix);
 
  for (int p = 0; p < 25; p++) {
    for (int q = 0; q < 25; q++) {
      matrix = create_random_matrix(p, q);
      verify(matrix);
    }
  }
 
  return 0;
}

Q7. If you are given a string, write a program to remove all the given string duplicates.

Solution

#include <bits/stdc++.h> 
using namespace std; 
  
char *remove_duplicate(char str[], int n) 
{ 
   // Used as index in the modified string 
   int index = 0;    
     
   // Traverse through all characters 
   for (int p=0; p<n; p++) { 
         
     // Check if str[p] is present before it   
     int q;   
     for (q=0; q<p; q++)  
        if (str[p] == str[q]) 
           break; 
       
     // If not present, then add it to 
     // result. 
     if (q == p) 
        str[index++] = str[p]; 
   } 
     
   return str; 
} 
  
// Driver code 
int main() 
{ 
   char str[]= "jeepsforjeeps"; 
   int n = sizeof(str) / sizeof(str[0]); 
   cout << remove_duplicate(str, n); 
   return 0; 
} 

Want to solve Interview Questions asked at Amazon, Facebook, Microsoft and more, visit CodeStudio.

Have you solved the Baby Names Problem on Code Studio yet? If not, then do it now.

Q8. Rewrite the code after removing errors from the following code:

Solution:

int sum, diff, prod, quotient, mod;
int a, b;
cout<<"Enter the value of a =";
cin>>a;
cout<<"Enter the value of b =”;
cin>>b;
sum=a+b;
diff=a-b;
prod=a*b;
quotient=a/b;
mod=a%b;

Q9. Write the output of the following C ++ program code.

Assume all required header files are already being included in the programme.

Solution:

using namespace std;
typedef char STRING[80];
void MIXITNOW(STRING S)
{
int Size=strlen(S);
for (int I=0;I<Size;I+=2)
{
char WS=S[I];
S[I]=S[I+1];
S[I+1]=WS;
}
for (int I=1;I<Size;I+=2)
if (S[I]>='M' && S[I]<='U')
S[I]='@';
}
int main()
{
STRING Word="CRACKAJACK";
MIXITNOW(Word);
cout<<Word<<endl;
return 0;
}
 RCCAAKAJKC 

Q10. What is the difference between linear and non-linear data structures?

A linear data structure is a structure whose elements form a sequence or a linear list. Examples of linear data structures are arrays and linked lists. Non-linear data structures are mainly used to represent data containing a hierarchical relationship between elements.

Examples of non-linear data structures are records, trees, graphs.  

Q11. What is an iterator class?

Iterator class allows you to access classes inside containers that hold data structures, classes, and abstract data types.

Iterators are crucial to understanding the functioning of the C++ Standard Template Library (STL) since it offers a way to access data stored in the container classes, for example, maps, list, vector and so on.

Frequently Asked Questions

What are the C++ language interview questions?

C++ language interview questions are frequently asked in various examinations and technical interviews for getting a job or an internship where the eligibility and requirements include being proficient in C++.

What are the basics of C++?

Basics of C++ include pointers, recursion, dynamic allocation, data structures, and searching and sorting algorithms.

How do I prepare for C++?

To prepare for a C++ interview, you need to strengthen pointers’ basic concepts, dynamic allocation, data structures and basic algorithms.

What are the basic C++ interview questions?

Basic C++ interview questions include questions on basic algorithms, implementation of data structures, error handling and analysis, and output questions. You can read the blog thoroughly and explore multiple-choice questions online.

Conclusion

In this blog, we have covered some C++ interview questions and their answers. Want to get an in-depth understanding of basic C++ interview questions and their underlying concepts?

Join Coding Ninjas today! Our trained and dedicated team of instructors will ensure that by the time you finish the course, you’ve mastered the art of C++.

Moreover, if you are interested in reading books, here is a list of some books for C++.