[1] C++ Interview Questions
C/C++ Complete Program Preparation
Memory Layout of C Programs
Question 9. Can You Name A Situation Where Joining Threads Should Be Avoided?
Answer :A call to join() blocks the caller thread. This is really bad in situations where the caller thread is a main UI thread – because if the UI thread blocks, the application will stop responding to user inputs which will make it seem hanged.
Another place where calling join() is not advisable is inside a main game loop. Calling join() can block update and rendering of the game scene and severly impact the user experience (it'll be like watching a You tube video on a dial up internet connection !).
https://www.wisdomjobs.com/e-university/c-multithreading-developer-interview-questions.htmlhttps://www.wisdomjobs.com/e-university/c-multithreading-developer-interview-questions.html
https://www.wisdomjobs.com/e-university/advanced-c-plus-plus-interview-questions.html
C++ Preparation
Virtual Functions
1. Virtual Functions Quiz1
2. Virtual Function Quiz - Set2
Virtual functions in derived classes
Virtual Destructor
Advanced C++ | Virtual Constructor
const_cast in C++ | Type Casting operators
Default Assignment Operator and References
Can virtual functions be private in C++?
Polymorphism in C++
Encapsulation in C++
Difference between C structures and C++ structures
C/C++ Tricky Programs
Some interesting facts about static member functions in C++
Copy Constructor in C++
When do we use Initializer List in C++?
How many virtual table will be created in C++
Number of Virtual tables and Virtual Pointers in a C++ Program
How to print “GeeksforGeeks” with empty main() in C, C++ and Java?
Print 1 to 100 in C++, without loop and recursion
Print substring of a given string without using any string function and loop in C
How to restrict dynamic allocation of objects in C++?
Operator Overloading
- Operator Overloading
- Copy constructor vs assignment operator
- When should we write our
own assignment operator? - Operators that cannot be overloaded
- Conversion Operators
- Is assignment operator inherited?
- Default Assignment Operator and References
- Overloading stream insertion (<<)
and extraction (>>) operators - Overloading array index operator []
Very Important Topics:-
1. Program for Sum of the digits of a given number
[2] Commonly Asked C++ Interview Questions | Set 1
[3] Commonly Asked OOP Interview Questions | Set 1
Design Pattern Collections
[1] Composite Design Pattern in C++
[2]
1. Overload new and delete operator
#include <iostream>
#include<malloc.h>
using namespace std;
void *operator new(size_t s, int i)
{
int *q = (int*)malloc(s);
*q = i;
return q;
}
void operator delete(void* ptr) throw()
{
cerr << "deallocating at " << ptr << endl;
free(ptr);
}
int main()
{
cout << "Overloading new and delete operator" << endl;
int *p = new (25)int;
cout<<*p;
delete p;
return 0;
}
2. Overload Cin and Cout
#include <iostream>
#include<malloc.h>
using namespace std;
class sample
{
private:
char name[20];
int age;
int sal;
public:
friend ostream &operator<<(ostream & o, sample & s);
friend istream &operator>>(istream & i, sample & s);
};
ostream &operator<<(ostream &out,sample &s)
{
out<<s.name<<endl;
out<<s.age<<endl;
out<<s.sal<<endl;
return(out);
}
istream &operator>>(istream &in,sample &s)
{
in>>s.name;
in>>s.age;
in>>s.sal;
return(in);
}
int main()
{
sample s;
cout<<"Enter name, age and salary of the employee";
cin>>s;
cout<<endl;
cout<<s;
}
3. Write an example for smart pointer.#include <iostream>
#include<malloc.h>
using namespace std;
class test
{
public:
void fun()
{
cout<<"fun of smart pointer"<<endl;
}
};
class sample
{
private:
test tt;
public:
test *operator->()
{
return (&tt);
}
};
int main()
{
sample s;
s->fun();
}
4. How can we create a pointer to a member function ?#include <iostream>
using namespace std;
class sample
{
public:
int a;
void f(int b) {
cout << "The value of b is "<< b << endl;
}
};
int main() {
// declare pointer to data member
int sample::*ptiptr = &sample::a;
// declare a pointer to member function
void (sample::* ptfptr) (int) = &sample::f;
// create an object of class type sample
sample s;
// initialize data member
s.*ptiptr = 10;
cout << "The value of a is " << s.*ptiptr << endl;
// call member function
(s.*ptfptr) (20);
}
The output for this example is:
The value of a is 10
The value of b is 20
5. How would you define a pointer to data member of the type pointer to pointer ?
#include <iostream>
using namespace std;
class sample
{
public:
sample(int **pp)
{
p = pp;
}
int **p;
};
int **sample::*ptr = &sample::p;
int main()
{
int i =9;
int *p = &i;
sample s(&p);
cout<<**(s.*ptr);
}
6. Write a program that will convert an integer pointer into an integer and vice-versa.#include <iostream>
using namespace std;
int main()
{
int i = 6500;
int *iptr = reinterpret_cast<int*>(i);
cout<<endl<<iptr;
iptr++;
cout<<endl<<iptr;
i = reinterpret_cast<int>(iptr);
cout<<endl<<i;
i++;
cout<<endl<<i;
}
7. How can we initialize an array of objects?
#include <iostream>
using namespace std;
class sample
{
private:
float f;
int i;
public:
sample(int ii,float ff)
{
f = ff;
i = ii;
}
void fun()
{
cout<<i<< " "<<f<<endl;
}
};
int main()
{
sample p[5] = {
sample (1,2.5f),
sample (3,4.5f),
sample (13,5.5f),
sample (24,6.7f),
sample(34,9.7f)
};
int k;
for(k=0;k<5;k++)
{
p[k].fun();
}
return 0;
}
8. How will you overload the following binary operators
+, +=,<=,>=, !=, ==
#include <iostream>
using namespace std;
class sample
{
private:
int i;
public:
sample(int ii)
{
i = ii;
}
sample operator+(sample s)
{
return sample(i+s.i);
}
sample operator +=(sample s)
{
return sample(i+=s.i);
}
bool operator !=(sample s)
{
if(i!=s.i)
{
return true;
}
else
return false;
}
bool operator <=(sample s)
{
if(i<=s.i)
{
return true;
}
else
return false;
}
bool operator >=(sample s)
{
if(i>=s.i)
{
return true;
}
else
return false;
}
bool operator ==(sample s)
{
if(i==s.i)
{
return true;
}
else
return false;
}
};
int main()
{
sample a(1),b(2),c(0);
c = a+b;
if (a<=b)
{
cout<<"Less than a"<<endl;
}
if (a>=b)
{
cout<<"Greater than a"<<endl;
}
if (a==b)
{
cout<<"a is equal to b"<<endl;
}
if (a!=b)
{
cout<<"a is not equal to b"<<endl;
}
}
8. Is it necessary that a constructor in a class should always be public.
No, When we want that a user of the class should not be able to create an object of a class but the member function of the class should be able to create it then constructor should be made private.
#include <iostream>
using namespace std;
class sample
{
private:
int i;
sample(int i)
{
}
public:
sample()
{
}
void fun()
{
sample x(10);
}
};
int main ()
{
sample s1; // It will work
sample s2(2); // It will throw an error as sample(int i) is private.
}
9. How will you initialize the static data in class ?
#include <iostream>
using namespace std;
class sample
{
static int i;
public:
sample(int m =0)
{
i = m;
}
void show()
{
cout<<"The value of i = "<<i<<endl;
}
};
int sample::i = 8;
int main()
{
sample s(25);
s.show();
}
10. Which all operator can not be overloaded ?
•Scope Resolution operator (::)
•Member Selection Operator (.)
•Member selection through pointer to function (.*)
•Member Selection Operator (.)
•Member selection through pointer to function (.*)
11. What is the wrong with the below code snippet ?
#include <iostream>
using namespace std;
void print();
class base1
{
protected:
void print()
{
cout<<"Hello";
}
};
class base2
{
public:
void print()
{
cout<<"Hi"<<endl;
::print();
}
};
void print()
{
cout<<"Error"<<endl;
}
int main()
{
base1 b1;
base2 b2;
b1.print();
b2.print();
}
11. A virtual destructor ensures a proper calling order for the destructor in the class hierarchy.
#include<iostream>
using namespace std;
class base
{
public:
virtual ~base()
{
}
};
class derived:public base
{
int *p;public:derived(){p =new int;}~derived(){delete p;}};
int main(){
base *b = new derived;delete b;}
12. What are virtual base classes ? When should they be used?
#include<iostream>
using namespace std;class base{
protected:int data;public:base(){data = 500;}};
class derived1:virtual public base{
};
class derived2:virtual public base{};
class derived3:public derived1,public derived2{
public:int getdata(){return data;}};
int main(){
derived3 ch;int a;a = ch.getdata();cout<<a;}
Suppose you have two derived classes B and C that have a common base class A, and you also have another class D that
inherits from B and C. You can declare the base class A as virtual to ensure that B and C share the same subobject of A.
In the following example, an object of class D has two distinct subobjects of class L, one through class B1 and another
through class B2. You can use the keyword virtual in front of the base class specifiers in the base lists of classes B1
and B2 to indicate that only one subobject of type L, shared by class B1 and class B2, exists.
For example:
class L { /* ... */ }; // indirect base class class B1 : virtual public L { /* ... */ }; class B2 : virtual public L { /* ... */ }; class D : public B1, public B2 { /* ... */ }; // valid
Using the keyword virtual in this example ensures that an object of class D inherits only one subobject of class L.
A derived class can have both virtual and nonvirtual base classes. For example:
class V { /* ... */ }; class B1 : virtual public V { /* ... */ }; class B2 : virtual public V { /* ... */ }; class B3 : public V { /* ... */ }; class X : public B1, public B2, public B3 { /* ... */ };
In the above example, class X has two subobjects of class V, one that is shared by classes B1 and B2 and one through class B3
Comments
Post a Comment