C++ 50 Interview Sheet
C++ 50 Interview Sheet
C++
50+ interview questions/answers
3. Define ‘std’?
‘std’ is also known as Standard or it can be interpreted as a namespace.
The command “using namespace std” informs the compiler to add
everything under the std namespace and inculcate them in the global
namespace. This all inculcation of global namespace benefits us to use
“cout” and “cin” without using “std::_operator_”.
For more information, refer to namespace and std.
Syntax:
int RUN = 10;
// reference variable
int& ref = RUN;
For more information, refer to references in C++
The changes made in the function The changes made in the functions
are never reflected outside the can be seen outside the function on
function on the variable. In short, the passed function. In short, the
the original value is never altered original value is altered in Call by
in Call by Value. reference.
For information, refer to the difference between call by value and call by
reference
For more information, refer to the Difference between struct and class.
It can never hold a null value as It can hold or point at a null value
it needs an existing value to and be termed as a nullptr or null
become an alias of pointer
Example of Function
Example of Operator Overloading:
Overloading:
int RUN() = X() + Y();
int RUN(int X, int Y);
int RUN() = X() – Y();
int RUN(char X, char Y);
For more information, refer to the Difference between while and do-while
loop
For more information, refer to the Difference between prefix and postfix
17.What is the difference between new and malloc()?
malloc()
new
The base class has a virtual A base class having pure virtual function
function that can be becomes abstract that cannot be
Pure Virtual Function
Virtual Function
For more information, refer to the Difference between virtual functions and
pure virtual functions
22.Explain inheritance
The capability or ability of a class to derive properties and characteristics
from another class is known as inheritance. In simple terms, it is a system or
technique of reusing and extending existing classes without modifying
them.
For more information, refer to Inheritance
Multiple Inheritances
For more information, refer to Multiple Inheritance.
- Function Overloading: When there are multiple functions with the same
name but different parameters then this is known as function
overloading.
C++
// same name different arguments
int RUN() {}
int RUN(int a) {}
float RUN(double a) {}
int RUN(int a, double b) {}
int main()
{
derived_RUN rn;
rn.display();
return 0;
}
Output:
Function of derived class
For more information, refer to Different types of Polymorphism
28.It is also termed static binding It is also termed Dynamic binding and
and early binding. Late binding.
It is achieved by function
It is achieved by virtual functions and
overloading and operator
function overriding.
overloading.
public:
// Parameterized Constructor
RUN(int x1, int y1)
{
x = x1;
y = y1;
}
int getX() { return x; }
int getY() { return y; }
};
int main()
{
// Constructor called
RUN R(10, 15);
// Access values assigned by constructor
cout << "R.x = " << R.getX() << ", R.y = " << R.getY();
return 0;
}
Output
R.x = 10, R.y = 15
Example:
C++
Sample(Sample& t) { id = t.id; }
Syntax:
~constructor_name(); // tilde sign signifies that it is a destructor
For more information, refer to Destructor.
32.Is destructor overloading possible? If yes then explain and if no then why?
The simple answer is NO we cannot overload a destructor. It is mandatory
to only destructor per class in C++. Also to mention, Destructor neither take
arguments nor they have a parameter that might help to overload.
delete[] delete
It is used for deleting the objects It is used for deleting the objects
of new[]; By this, we can say of new; By this, we can say
that delete[] is used to delete an array that delete is used to delete a
of objects single object
For more information, refer to the friend function and friend class
C++
// C++ program to demonstrate the
// a program without main()
#include
<stdio.h>
#define fun main
int fun(void)
{
printf("ItsRunTym");
return 0;
}
For more information, refer to Can you compile a program without the main
function
41.What is STL?
STL is known as Standard Template Library, it is a library that provides 4
components like container, algorithms, and iterators.
C++ STL
For more information, refer to STL in C++
44.What are the static data members and static member functions?
The static data member of a class is a normal data member but preceded
with a static keyword. It executes before main() in a program and is
initialized to 0 when the first object of the class is created. It is only visible
to a defined class but its scope is of a lifetime.
Syntax:
static Data_Type Data_Member;
The static member function is the member function that is used to access
other static data members or other static member functions. It is also
defined with a static keyword. We can access the static member function
using the class name or class objects.
Syntax:
classname::function name(parameter);
Syntax:
storage_class var_data_type var_name;
Some types of storage classes:
Examples of storage class
For more information, refer to Storage Class
In Shallow copy, a copy of the original In Deep copy, the copy of the
object is stored and only the original object and the repetitive
reference address is finally copied. In copies both are stored. In simple
simple terms, Shallow copy duplicates terms, Deep copy duplicates
as little as possible everything