C++ Interview questions
What is the purpose of 'delete' operator?
'delete' operator is used to release the dynamic memory which was created using 'new' operator
Which operator can be used in C++ to allocate dynamic memory
'new' is the operator can be used for the same
When a class member is definied outside the class, which operator can be used to associate the function definition to a particular class
(::)
What are/is the operator/operators used to access the class members?
. and ->
Can we implement all the concepts of OOPS using keyword struct?
Yes
Can I use malloc() function of C language to allocate dynamic memory in C++?
Yes, as C is the subset of C++, we can all the functions of C in C++ too
Mention the storage classes names in C++
the following are storage classes supported in C++ auto, static, extern, register and mutable
What are VTABLE and VPRT?
vtable is a table of function pointers. It is maintained per class. vptr is a pointer to vtable. It is maintained per object. Compiler adds additional code at two places to maintain and use vtable and vptr. 1) 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. 2) Code with polymorphic function fall. Wherever a polymorphic call is made, compiler inserts code to first look for vptr using base class pointer or reference (in the ablve example since pointed reference object is of derived type, vptr of derived class accessed). Once vptr is fetched, vtable of derived class can be accessed. Using vtable, address of derived class function show() is accessed and called
Name the data type which can be used to store wide characters in C++
wchar_t
What are the differences between C and C++
1) C++ is a kind of superset of C, most of C programs except few exceptions work in C++ as well 2) C is a procedural programming language, but C++ supports both procedural and Object Oriented Programming 3) since C++ supports object oriented programming. It supports features like function overlaoding, templates, inheritance, virtual functions, friend functions. These features are absent in C 4) C++ supports exception handling at language level, in C exception handling is done in traditional if-else style. 5) C++ Supports references, C does not 6) In C, scanf() and printf() are mainly used input/output. C++ mainly uses streams to perform input and output operations. cin is standard input stream and cout is standard output stream
What is an abstract class in C++
A class with at least one pure virtual function. We cannot instantiate an abstract class
What is the role of mutable storage specifier?
A constant class object's member variable can be altered by declaring it mutable storage specifier. Applicable only for non-static and non-constant member variable of the class
What is a constructor?
A constructor is the member function of the class which is having the same as the class name and gets executed automatically as soon as the object for the respective class is created
What is a copy constructor?
A copy constructor is the constructor which takes the same class object reference as the parameter. It gets automatically invoked as soon as the object is initialized with another object of the same class at the time of its creation.
What is a destructor? Can it be overloaded?
A destructor is the member function of the class which is having the same name as the class name and prefixed with tilde (~) symbol. It gets executed automatically w.r.t the object as soon as the object loses its scope. It cannot be overloaded and the only form is without the parameters.
What is an inline function?
A function prefixed with the keyword inline before the function definition is called an inline function. The inline functions are faster in execution when compared to normal functions as the compiler treats inline functions as macros
What is a friend function?
A function which is not a member function of the class but still can access all the members of the class. To make it happen we need to declare within the required class following the keyword 'friend'
What is a namespace?
A namespace is the logical division of the code which can be used to resolve the name conflict of the identifiers by placing them under difference namespace
What is a reference variable in C++?
A reference variable is an alias name for the existing variable. Which mean both the variable name and reference variable point to the same memory location. Therefore updation on the original variable can be achieved using reference variable too.
What is a static member function?
A static member function can be invoked using the class name as it exists before class objects come into existence. It can access only static members of the class
What is a class template?
A template class is a generic class. The keyword template can be used to define a class template.
What is the block scope variable in C++?
A variable whose scope is applicable only within a block is said so. Also a variable in C++ can be declared anywhere within the block
What is a pure virtual function?
A virtual function with no function body and assigned with a value zero
What is abstraction?
Abstraction refers to hiding the internal implementation and exhibiting only the necessary details
What is an object
An instance of the class is called an object
What are the differences between references and pointers
Both references and pointers can be used to change 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 above similarities, there are following differences between references and pointers References are less powerful than pointers 1) Onces a reference is created, it cannot be later made to reference another object; it cannot be reseated. This is often done with pointers 2) References cannot be NUL. Pointers are often made NULL to indicate that they are not pointing to any valid thing. 3) A reference must be initialized when declared. There is no such restriction with pointers Due to the above limitation, references in C++ cannot be used for implementing data structures like Linked Lists, Tree, etc. In Java, references don't have 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: 1) safer: since references must be intialized, wild references like wild pointers are unlikely to exist. It is still possible to have references that don't refer to valid location. 2) 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, inlike pointers where arrow operator (->) is needed to access members
What is function overloading?
Defining several functions with the same name with unique list of parameters is called function overloading
What is the difference between the keywords struct and class in C++
By default the members of struct are public and by default the members of the class are private
Explain the purpose of the keyword volitile
Declaring a variable volatile directs the compiler that the variable cant be changed externally. Hence avoiding compiler optimization on the variable reference.
Does C++ support exception handling? If so what are the keywords involved in achieving it?
C++ does support exception handling. try, catch & throw are keywords used.
What is a class?
Class is a blue print which reflects the entities attributes and actions. Techinically defining a class is designing an user defined data type
What is operator overloading?
Defining a new job for the existing operator w.r.t the class objects is called operator overloading
What is a default constructor?
Every class does have a constructor provided by the compilor if the programmer doesn't provide one and known as default constructor. A programmer provided constructor with no parameters is called a default constructor. In such case compiler doesn't provide the constructor.
explain the pointer - this
This, is the pointer variable of the compiler which always holds the current active object's address
What is the role of protected access specifier
If a class member is protected then it is accessible in the inherited class. However, outside both the private and protected members they are not accessible
What is inheritance?
Inheritance is the process of acquiring the properties of the existing class into the new class. The exisiting class is called as base/parent class and the inherited class is called as derived/child class
can we initialize a class/structure member variable as soon as the same is defined.
No, defining the class/structure is just a type definition and not allocated memory for the same
Do we have a String primitive type in C++?
No, its a class from STL
Can I use 'delete' operator to release the memory which was allocated using malloc() function of C language?
No, we need to use free() of C language for the same
Distinguish between shallow copy and deep copy
Shallow copy does memory dumping bit-by-bit from one object to another. Deep copy is copy field by field from object to another. Deep copy is achieved using copy constructor and or overloading assignment operator
List the types of inheritance supported in C++
Single, Multilevel, Multiple, Hierarchical and Hybrid
What is the role of static keyword on class member variable?
Static member variables share a common memory across all the objects created for the respective class. A static member variable can be referred using the class name itself
What is a storage class?
Storage class specifies the life or scope of symbols such as variable or functions
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 is static member functions as static member functions can be called without any object (with class name).
how can we catch all kinds of exceptions in a single catch block?
The catch block with ellipses as follows catch(...) {}
What is encapsulation?
The process of binding the data and the functions acting on the data together in an entity (class) called an encapsulation
What is the scope resolution operator
The scope resolution operator is used to - resolve the scope of global variables - To associate function definition to a class if the function is defined outside the class
What are virtual functions - Write an example
Virtual functions are used with inheritance, they are called acoording to the type of object pointed or referred, not according to the type or pointer or reference. In other words, virtual functions are resolved late, at runtime. 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) 1) a base class and a derived class 2) A function with same name in base class and derived class 3) A pointer or reference of base class type pointing or referring to an object for derived class #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"; } }; int main(void) { Base *bp = new Derived; bp->show(); // RUN-TIME POLYMORPHISM return 0; } output: In Derived
What is the data type to store the Boolean Value?
bool, is the primitive data type introduced in C++ language
Name the default standard streams in C++
cin, cout, cerr, clog
What is the role of the file opening ios::trunk?
if the file already exists, its contents will be truncated before opening the file