100+ Latest C++ Interview Questions and Answers
Question: Briefly explain the concept of Inheritance in C++.
Question: Define C++?
Question: Can we call C++ as OOPS? and Why?
Question: Define Class in C++?
Question: Define Object in C++?
Question: Define Encapsulation in C++?
Question: What is an abstraction in C++?
Question: What is the function of the keyword “Volatile”?
Question: Define storage class in C++? Name some?
Question: Can we have a recursive inline function in C++?
Question: Define an Inline Function in C++? Write its syntax. Is it possible for the C++ compiler to ignore inlining?
The syntax of a typical inline function is:
Inline return-type function-name(parameters)
{
// Function code goes here
}
As the inlining is a request, not a command, the compiler can ignore it.
Question: Explain ‘this’ pointer?
Question: Why do we need the Friend class and function?
Some important points about friend class and friend function:
- Friendship is not inherited
- Friendship isn’t mutual i.e. if some class called Friend is a friend of some other class called NotAFriend then it doesn’t automatically become a friend of the Friend class
- The total number of friend classes and friend functions should be limited in a program as the overabundance of the same might lead to a depreciation of the concept of encapsulation of separate classes, which is an inherent and desirable quality of object-oriented programming
Question: Explain the significance of vTable and vptr in C++ and how the compiler deals with them
- In every constructor – This code sets vptr:
- Code with the polymorphic functional call – At every location where a polymorphic call is made, the compiler inserts code in order to first look for vptr using the base class pointer or reference. The vTable of a derived class can be accessed once the vptr is successfully fetched. Address of derived class function show() is accessed and called using the vTable.
Question: How is function overloading different from operator overloading?
Question: Is it possible for a C++ program to be compiled without the main() function?
Question: Draw a comparison between C++ and Java
- C++ has destructors, which are invoked automatically when an object is destroyed. Java has something called automatic garbage collection
- C++ supports multiple inheritance, operator overloading, pointers, structures, templates, and unions. Java doesn’t have any of them
- Java has a Thread class that is inherited in order to create a new thread. C++ has no inbuilt support for threads
- In C++, a goto statement offers a way to jump from a location to some labeled statement in the same function. There is no goto statement in Java
- C++ run and compile using the compiler, which converts the source code into machine level language. Hence, it is platform-dependent. Java compiler, on the other hand, converts the source code into JVM bytecode, which is platform-independent.
Question: Take a look at the following C++ program:
#include
using namespace std;
int main()
{
int numbers[5], sum = 0;
cout << "Enter 5 numbers: ";
for (int i = 0; i < 5; ++i)
{
cin >> numbers[i];
sum += numbers[i];
}
cout << "Sum = " << sum << endl;
return 0;
}
Question: What will be the output?
Question: What are the most important differences between C and C++?
- C++ supports references while C doesn’t
- Features like friend functions, function overloading, inheritance, templates, and virtual functions are inherent to C++. These are not available in C programming language
- In C, exception handling is taken care of in the traditional if-else style. On the other hand, C++ offers support for exception handling at the language level
- Mainly used input and output in C are scanf() and printf(), respectively. In C++, cin is the standard input stream while cout serves as the standard output stream
- While C is a procedural programming language, C++ provides support for both procedural and object-oriented programming approaches
Question: Explain Virtual Functions and the concept of Runtime Polymorphism in C++ with a code example.
- A base class
- A derived class
- A function with the same name in both the classes i.e. the base class and the derived class
- A pointer or reference of base class type that points or refers, respectively to an object of the derived class
An example demonstrating the use of virtual functions (or runtime polymorphism at play) is:
#include
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(); // <- Runtime Polymorphism in Action
return 0;
}
Question: What differences separate structure from a class in C++?
Answer: There are two important distinctions between a class and a structure in C++. These are:
- When deriving a structure from a class or some other structure, the default access specifier for the base class or structure is public. On the contrary, default access specifier is private when deriving a class.
- While the members of a structure are public by default, the members of a class are private by default
Question: What does a Static member in C++ mean?
- Any static member function can’t be virtual
- Static member functions don’t have ‘this’ pointer
- The const, const volatile, and volatile declaration aren’t available for static member functions
Question: Define access specifier and its various types in C++
Question: Define the Copy Constructor used in C++ along with its general function prototype. Also, explain the various scenarios in which it is called.
- The compiler generates a temporary object
- An object is constructed or based on some another object of the same class
- An object of the class is returned by value
- An object of the class is passed (i.e. to a function) by value as an argument
The general function prototype for the Copy Constructor is:
Question: Observe the following code snippet:
After execution, what will be the value of i and j? Explain your answer.
Question: Take a look at the following two code examples for printing a vector:
Sample Code 1:
vector vec;
/* ... .. ... */
for (auto itr = vec.begin(); itr != vec.end(); itr++) {
itr->print();
}
Sample Code 2:
vector vec;
/* ... .. ... */
for (auto itr = vec.begin(); itr != vec.end(); ++itr) {
itr->print();
}
Is there any advantage of using one over the other?
Question: Suppose you have the GPA (Grade Point Average) of n number of students and you need to store and display it using C++. Can you write a program that accomplishes this?
#include
#include
using namespace std;
int main()
{
int num;
cout << "Enter the total number of students: ";
cin >> num;
float* ptr;
ptr = new float[num];
cout << "Enter the GPA of students." << endl;
for (int i = 0; i < num; ++i)
{
cout << "Student" << i + 1 << ": ";
cin >> *(ptr + i);
}
cout << "\nDisplaying GPA of students." << endl;
for (int i = 0; i < num; ++i) {
cout << "Student" << i + 1 << " :" << *(ptr + i) << endl;
}
delete [] ptr;
return 0;
Question: What is a mutable storage class specifier? How can they be used?
Question: What are the differences between a shallow copy and a deep copy?
Answer: The differences between a shallow copy and a deep copy can be stated as under.
Question: Define an Abstract class in C++?
// An abstract class
class Test
{
// Data members of class
public:
// Pure Virtual Function
virtual void show() = 0;
/* Other members */
};
Question: Define the Reference variable?
#include<iostream>
using namespace std;
int main()
{
int x = 10;
// ref is a reference to x.
int& ref = x;
// Value of x is now changed to 20
ref = 20;
cout << "x = " << x << endl ;
// Value of x is now changed to 30
x = 30;
cout << "ref = " << ref << endl ;
return 0;
}
Question; Can we have a String primitive data type in C++?
Question: Can we use access specifiers to achieve data hiding in C++?
Question: What is a destructor?
Question: Can we overload a destructor?
Answer: No, a destructor cannot be overloaded, and it has the only form without the parameters.
Question: What is the default constructor?
// Cpp program to illustrate the
// concept of Constructors
#include <iostream>
using namespace std;
class construct {
public:
int a, b;
// Default Constructor
construct()
{
a = 10;
b = 20;
}
};
int main()
{
// Default constructor called automatically
// when the object is created
construct c;
cout << "a: " << c.a << endl
<< "b: " << c.b;
return 1;
}
Question: Can we provide one default constructor for our class?
Question: What is the main difference between the keyword struct and class?
Question: Define Block scope variable?
Question: What are the functions of the scope resolution operator?
Answer: The functions of the scope resolution operator include the following.
- It helps in resolving the scope of various global variables.
- It helps in associating the function with the class when it is defined outside the class.
The code of the scope resolution operator can be displayed as follows.
#include <iostream>
using namespace std;
int my_var = 0;
int main(void) {
int my_var = 0;
::my_var = 1; // set global my_var to 1
my_var = 2; // set local my_var to 2
cout << ::my_var << ", " << my_var;
return 0;
}
Question: Define a namespace?
Question: Define a class template?
Question: What is the function of the keyword "Auto"?
Question: Define a token in C++? Give examples?
asm bool catch class
const_cast delete dynamic_cast explicit
export false friend inline
mutable namespace new operator
private protected public reinterpret_cast
static_cast template this throw
true try typeid typename
using virtual wchar_t
Question: What is the ‘diamond problem’ that occurs with multiple inheritance in C++? Explain using an example.
The problem generates an inheritance diagram resembling a diamond, hence the name, diamond problem.
0 comments:
Post a Comment