C++ Programming Interview Questions – Take your Career to New Heights
Master C++ with Real-time Projects and Kickstart Your Career Start Now!!
As an extension to the previous C++ interview questions, we have decided to elevate the difficulty in order to test your concepts and take them to a higher level. After having explored the breadth and depth of these C++ programming interview questions, you are now qualified to solve almost any question that is fired by the interviewer.
The harder you work for something, the greater you’ll feel when you achieve it.
1. Latest C++ Programming Interview Questions
Q.1 What is meant by event-driven programming? In what way is it supported in C++?
Ans. Event-driven programming refers to a programming paradigm in which the flow of control is given by events such as user-actions or threads. C++ supports event-driven programming as it consists of the main function that lists the events to be executed sequentially and triggers a callback function as soon as an event is identified, such as looping or a function call.
Q.2 What are the demerits of using inline functions?
Ans. One of the major disadvantages of inline functions in C++ is the immense wastage of computer memory by repetitive occurrences of the same function code. Inline functions involve several function calls. Therefore, for each function call, the entire code is processed, again and again, increasing the time-complexity of our program.
Moreover, there are certain situations where inline functions do not work. They are:
- When looping is done within the function or it involves the use of a switch or goto statement.
- When the function contains static data members.
- The function is recursive in nature.
Q.3 You use the setw() function and get a compilation error. Which header file are you possibly missing?
Ans. In order to use the setw() function, you need to include the header file <iomanip.h> available in the C standard library.
Q.4 How would you access the private members of a class without using any member function of that class?
Ans. C++ gives the programmer the provision to access private as well as protected data members of a class outside the class or to other classes with the help of the friend function or friend class.
Learn the concept of Friend Function in C++ with Example
Q.5 What is ‘this’ pointer?
Ans. ‘this’ pointer is used to hold the address of the current object. In simple words, ‘this pointer’ points to the current object of the class.
Q.6 Write a program in C++ to find and print the sum of all the values ending with 7 in a 2D array.
Ans.
#include <iostream> using namespace std; int main() { int sum = 0, i , j, m, n, remainder; int A[10][10]; cout<<"Enter the number of rows of the 2D array: "<<endl; cin>>m; cout<<"Enter the number of columns of the 2D array: "<<endl; cin>>n; // Input of the array cout<<"Enter the elements of the array: "<<endl; for (i =0; i < m; i++) { for(j = 0; j < n; j++) { cin>>A[i][j]; } } // Logic to find sum of elements ending with 7. for (i =0; i < m; i++) { for(j = 0; j < n; j++) { remainder = A[i][j]%10; if(remainder == 7) sum = sum + A[i][j]; } } cout<<"The sum of elements with unit's digit 7 is: "<< sum<<endl; return 0; }
Get the Samurai Technique to Learn Arrays in C and C++
Q.7 How would you create a self-referential structure? Is it possible to create a self-referential class as well?
Ans. Self-referential structures are basically structures that have one or more than one pointers pointing to the same type of structure as their members. In simple words, structures that point to the same type of structures are called self-referential structures. Yes, it is possible to create a self-referential class as well in order to implement a linked list or a tree.
Note – This is the most popular C++ Programming Interview Question.
Q.8 What is the difference between delete and delete[]?
Ans. While creating and allocating memory to a variable with the help of the “new” keyword, we can deallocate the memory associated with it by the help of the “delete[]” operator whereas, while creating and allocating memory to a non-array object with new, we can deallocate the memory with the help of the “delete” operator.
Q.9 What does this statement indicate?
virtual void display() = 0;
Ans. This statement indicates the declaration of a pure virtual function. It is important to note that the value 0 is not being assigned to the function.
Q.10 What are the reasons for memory leaks?
Ans. The major reasons for memory leaks are as follows:
- When the user forgets to delete data that has been dynamically allocated.
- When the user fails to notice that the code may bypass a delete statement in unforeseen situations.
- And, when the user assigns the result of a new statement to a pointer that was already pointing to an allocated object.
To know more about Memory Leaks, you should learn Dynamic Memory Allocation in C++
2. C++ Programming Interview Questions for Freshers to Experienced
Q.11 What is wrong with this code?
T *p = new T[10];
delete p;
Ans. The statement would not result in a compilation error, rather a logical error. The problem associated with this statement is that it will delete the first element of the array. Even Though the entire array gets deleted, only the destructor of the first element would be called and the memory for the first element gets released.
Q.12 Predict the output of the following C++ code:
#include <iostream> using namespace std; int main() { int a[3]={10,20,30}; int *pointer; pointer = &a[1]; cout<<"*ptr : "<< *pointer << endl; cout<<"*++ptr : "<< *++pointer << endl; return 0; }
Ans. 20
30
Q.13 Predict the output of the following C++ code:
#include <iostream> using namespace std; void Manipulate(int n, int array[], int size) { for(int i = 0; i < size; i++) if (i < n) array[i]+=i; else array[i]*=i; } void print(int array[], int size) { for(int i =0; i < size; i++) (i%2!=0)?cout<<array[i]<<"*":cout<<array[i]<<endl; } int main() { int ARRAY[] = {30,20,40,10,60,50}; Manipulate(3,ARRAY,6); print(ARRAY,6); return 0; }
Ans.
30
21*42
30*240
250*
Q.14 Write a function in C++ to modify the content of the array in such a way that the elements that are multiples of 5 are swapped with its adjacent element.
Ans.
void Swap(int array[], int size) { int i, temp; for(i = 0; i < size-2; i++) if(array[i]%5 == 0) { temp = array[i]; array[i] = array[i+1]; array[i+1] = temp; i++; } cout<<"The swapped array is: "<<endl; for(i = 0; i <= size-1; i++) { cout<<array[i]<<"\t"; } }
Enhance your fundamental skills with Functions in C/C++
Q.15 Write a function in C++ to modify the content of an array in such a way that elements present every even location of the array is swapped with those present in odd locations
Ans.
void Position_Swap(int array[], int size) { int limit, swap; if(size%2 != 0) limit = size - 1; else limit = size; for(int i =0; i < limit - 1; i+=2) { swap = array[i]; array[i] = array[i+1]; array[i+1] = swap; } }
Q.16 How would you find the product of alternate elements of a 2D array?
Ans. We can find the product of the alternate elements of a 2D array with the help of the following function in C++:
void Alter_sum(int A[], int m, int n) { int product =0, temp = 1; for(int i = 0; i < m; i++) for(int j =0; j < n; j++) { if(temp % 2 != 0) product = product*A[i][j]; temp++; } cout<<"The3 product of the alternate elements of the array is: ", product); }
Unveil the Important Concepts of 2D & 3D Arrays in C/C++
Q.17 How would you check if two numbers are equal or not without the use of any relational operator in C++?
Ans. Here is a program that lets you compare two numbers without the use of any or relational operator in C++.
#include <iostream> using namespace std; void check(int n1, int n2) { if ( !(n1 ^ n2) ) // Performing XNOR operation cout<< n1 << " is equal to " << n2 <<endl; else cout<< n1 << " is not equal to " << n2 <<endl; } int main() { check(2,2); check(2,4); return 0; }
Q.18 Predict the output of the following C++ program:
#include <iostream> using namespace std; int main() { int n[] = {2, 4, 6, 8, 10}; int *pointer = n; for(int i =0; i < 3; i++) { cout<<*pointer<<"*"; pointer++; } cout<<endl; for(int i = 0; i < 4; i++ ) { (*pointer) *=2; --pointer; } for(int i = 1; i < 4; i++) cout<<n[i]<<"$"; cout<<endl; }
Ans. The output of the following C++ program is:
2*4*6*
8$12$16$
Q.19 Predict the output of the following code segment:
#include <iostream> #include <ctype.h> #include <string.h> using namespace std; void change(char String[], int &Pos) { char *pointer = String; int length = strlen(String); for(;Pos < length - 2; Pos+=3, pointer++) { *(pointer+Pos) = toupper(*(pointer+Pos)); } } int main() { int text = 0; char Msg[] = "Welcome to DataFlair tutorials! "; change(Msg, text); cout<< Msg <<"\t" << text <<endl; return 0; }
Ans. The output of the following code segment is:
WelcOme To DAtaFLair tutOriaLs! 30
Challenges are what make life interesting and overcoming them is what makes life meaningful. by Joshua J. Marine
Summary
Enjoyed? We compiled an exclusive list of C++ programming interview questions that hold relevance for cracking the toughest of interviews. We hope that these C++ questions, you achieve the highest zenith of your career.
We work very hard to provide you quality material
Could you take 15 seconds and share your happy experience on Google
Do you offer services to provide C++ programming? We need a programmer for a project. Thank you
good sir