Question 1
When a copy constructor may be called?
When an object of the class is returned by value.
When an object of the class is passed (to a function) by value as an argument.
When an object is constructed based on another object of the same class
When compiler generates a temporary object.
All of the above
Question 2
#include
using namespace std;
class Point
{
int x, y;
public:
Point(const Point &p) { x = p.x; y = p.y; }
int getX() { return x; }
int getY() { return y; }
};
int main()
{
Point p1;
Point p2 = p1;
cout << "x = " << p2.getX() << " y = " << p2.getY();
return 0;
}
x = garbage value y = garbage value
x = 0 y = 0
Compiler Error
Question 3
Predict the output of the following program?
#include
using namespace std;
class GfG {
GfG() {
cout << "Constructor Called" << endl;
}
};
int main() {
cout << "Main Started" << endl;
return 0;
}
Compiler Error
Main Started
Constructor Called
Constructor Called
Main Started
Main Started
Question 4
#include
using namespace std;
int i;
class A
{
public:
~A()
{
i=10;
}
};
int foo()
{
i=3;
A ob;
return i;
}
int main()
{
cout << foo() << endl;
return 0;
}
Question 5
Question 6
#include
using namespace std;
class A
{
int id;
static int count;
public:
A() {
count++;
id = count;
cout << "constructor for id " << id << endl;
}
~A() {
cout << "destructor for id " << id << endl;
}
};
int A::count = 0;
int main() {
A a[3];
return 0;
}
constructor for id 1 constructor for id 2 constructor for id 3 destructor for id 3 destructor for id 2 destructor for id 1
constructor for id 1 constructor for id 2 constructor for id 3 destructor for id 1 destructor for id 2 destructor for id 3
Compiler Dependent.
constructor for id 1 destructor for id 1
Question 7
Question 8
What is the use of this pointer?
When local variable’s name is same as member’s name, we can access member using this pointer.
To return reference to the calling object
Can be used for chained function calls on an object
All of the above
Question 9
#include
using namespace std;
class Test
{
private:
int x;
int y;
public:
Test(int x = 0, int y = 0) { this->x = x; this->y = y; }
static void fun1() { cout << "Inside fun1()"; }
static void fun2() { cout << "Inside fun2()"; this->fun1(); }
};
int main()
{
Test obj;
obj.fun2();
return 0;
}
Question 10
There are 12 questions to complete.