Commonly Asked C++ Interview Questions | Set 1
Last Updated :
16 Jun, 2024
Refer to the article C++ Interview Questions and Answers for the latest data.
1. What are the differences between C and C++?
- C++ is a kind of superset of C, most C programs except for a few exceptions (See this and this) work in C++ as well.
- C is a procedural programming language, but C++ supports both procedural and Object Oriented programming.
- Since C++ supports object-oriented programming, it supports features like function overloading, templates, inheritance, virtual functions, and friend functions. These features are absent in C.
- C++ supports exception handling at a language level, in C exception handling, is done in the traditional if-else style.
- C++ supports references, C doesn't.
- In C, scanf() and printf() are mainly used input/output. C++ mainly uses streams to perform input and output operations. cin is the standard input stream and cout is the standard output stream.
There are many more differences, above is a list of the main differences.
2. What are the differences between references and pointers?
Both references and pointers can be used to change the local variables of one function inside another function. Both of them can also be used to save copying of big objects when passed as arguments to functions or returned from functions, to get efficiency gain. Despite the above similarities, there are the following differences between references and pointers.
References are less powerful than pointers as:
- Once a reference is created, it cannot be later made to reference another object, it cannot be reseated. This is often done with pointers.
- References cannot be NULL. Pointers are often made NULL to indicate that they are not pointing to any valid thing
- A reference must be initialized when declared. There is no such restriction with pointers
Due to the above limitations, references in C++ cannot be used for implementing data structures like Linked List, Tree, etc. In Java, references don’t have the above restrictions and can be used to implement all data structures. References being more powerful in Java is the main reason Java doesn’t need pointers.
References are safer and easier to use:
Safer: Since references must be initialized, wild references like wild pointers are unlikely to exist. It is still possible to have references that don’t refer to a valid location (See questions 5 and 6 in the below exercise)
Easier to use: References don’t need dereferencing operator to access the value. They can be used like normal variables. ‘&’ operator is needed only at the time of declaration. Also, members of an object reference can be accessed with dot operator (‘.’), unlike pointers where arrow operator (->) is needed to access members.
3. 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 to, not according to the type of pointer or reference. In other words, virtual functions are resolved late, at runtime. The virtual keyword is used to make a function virtual.
Following things are necessary to write a C++ program with runtime polymorphism (use of virtual functions)
- A base class and a derived class.
- A function with the same name in base class and derived class.
- A pointer or reference of base class type pointing or referring to an object of a derived class.
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.
C++
// C++ program for the above approach
#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"; }
};
// Driver's code
int main(void)
{
Base* bp = new Derived;
// Function call
bp->show(); // RUN-TIME POLYMORPHISM
return 0;
}
Time Complexity: O(1)
Auxiliary Space: O(1)
4. What is this pointer?
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 a constant pointer that holds the memory address of the current object. ‘this’ pointer is not available in static member functions as static member functions can be called without any object (with class name).
5. Can we perform "delete this" operation?
See https://www.geeksforgeeks.org/delete-this-in-c/
6. What are VTABLE and VPTR?
The vtable is a table of function pointers. It is maintained per class. vptr is a pointer to vtable. It is maintained per object (See this for an example). The compiler adds additional code at two places to maintain and use vtable and vptr.
- Code in every constructor. This code sets the vptr of the object being created. This code sets vptr to point to vtable of the class.
- Code with polymorphic function call (e.g. bp->show() in above code). Wherever a polymorphic call is made, the compiler inserts code to first look for vptr using a base class pointer or reference (In the above example, since the pointed or referred object is of the derived type, vptr of a derived class is accessed). Once vptr is fetched, vtable of derived class can be accessed. Using vtable, the address of the derived class function show() is accessed and called.
You may also like:
We will soon be covering more C++.
Similar Reads
Commonly Asked Data Structure Interview Questions
To excel in a Data Structure interview, a strong grasp of fundamental concepts is crucial. Data structures provide efficient ways to store, organize, and manipulate data, making them essential for solving complex problems in software development.Interviewers often test candidates on various data str
6 min read
Top C++ STL Interview Questions and Answers
The Standard Template Library (STL) is a set of C++ template classes that are used to implement widely popular algorithms and data structures such as vectors, lists, stacks, and queues. It is part of the C++ Language ISO standard. STL is a popular topic among interviewers, so it is useful for both f
15+ min read
C++ Interview Questions and Answers (2025)
C++ - the must-known and all-time favourite programming language of coders. It is still relevant as it was in the mid-80s. As a general-purpose and object-oriented programming language is extensively employed mostly every time during coding. As a result, some job roles demand individuals be fluent i
15+ min read
Top 25 Frequently Asked Interview Questions in Technical Rounds
Here is the collection of the TOP 25 frequently asked questions based on the experience of interviews in multiple companies. 1Lowest common Ancestor2An unsorted array of integers is given; you must find the max product formed by multiplying three numbers. (You cannot sort the array, watch out when t
1 min read
Microsoft's most asked interview questions
Like other product-based companies, Microsoft also asks data structures and algorithms as part of their technical interview. Below is a list of questions prepared from different Microsoft interview experiences. Most Asked QuestionsCheck if a Binary Tree is BST or not - Practice hereRemove duplicates
2 min read
SAP Labs Interview Questions | Set 9 (Fresher)
SAP Labs visited our campus for a recruitment drive, providing an exciting opportunity for students to showcase their skills and secure positions in their esteemed organization.The 1st round was an Online Test. It consisted of 107 questions (60- Psychometric Questions) and the overall time limit of
6 min read
C++ Interview Questions on Virtual Function and Abstract Class
Diving into the depths of C++ programming requires a solid grasp of some of its most important topics, such as virtual functions and abstract classes. These concepts are not only fundamental to mastering the language but are also frequent subjects in technical interviews.In this interview preparatio
5 min read
C Programming Interview Questions (2025)
At Bell Labs, Dennis Ritchie developed the C programming language between 1971 and 1973. C is a mid-level structured-oriented programming and general-purpose programming. It is one of the oldest and most popular programming languages. There are many applications in which C programming language is us
15+ min read
Most asked Computer Science Subjects Interview Questions in Amazon, Microsoft, Flipkart
Apart from coding questions, top tech companies like Amazon, Microsoft and Flipkart also asks question from core computer science subjects. Majorly question are asked from subjects like Object-Oriented Programming, Operating System (OS), Database Management System (DBMS) and Computer Networks. Knowl
3 min read
JP Morgan Interview Questions and Answers for Technical Profiles
JP Morgan, a big player in finance worldwide, is well-known for being very careful when choosing new employees, especially for technical jobs. To help you do well in your upcoming technical interview at JPMorgan, we've put together a detailed list of questions that are often asked, along with their
10 min read