1) Which of the following operators can use friend functions for overloading?

a) ==
b) [ ]
c) –>
d) ( )

Answer(s) : 
a) ==

[quads id=1]

2) What will be the O/P of the following program ?

#include
using namespace std;

class GlobalClass {
int m_value;
static GlobalClass *s_instance;
GlobalClass(int v = 0) {
m_value = v;
}
public:
int get_value() {
return m_value;
}
void set_value(int v) {
m_value = v;
}
static GlobalClass *instance() {
if (!s_instance)
s_instance = new GlobalClass;
return s_instance;
}
};

GlobalClass *GlobalClass::s_instance = 0;

void Func1(void) {
GlobalClass::instance()->set_value(1);
cout << GlobalClass::instance()->get_value() << ‘\n’; } void Func2(void){ GlobalClass::instance()->set_value(2);
cout << GlobalClass::instance()->get_value() << ‘\n’;
}

int main() {
cout << GlobalClass::instance()->get_value() << ‘\n’;
Func1();
Func2();
}

a)

0 1 1

b)

0 0 0

c)

0 1 2

d)

1 2 3
Answer(s) : 
c) 0 1 2

3) What is the output/Error of the following code ?

#include
using namespace std;
struct emp {
int a;
emp ( int b): a(b){cout << ” Constructor ” ;}
~emp(){ cout << ” Destructor ” ;}
void disp(){ cout << ” In Display ” ; }
};

int main(){
emp *e = new emp(20);
cout << e->a ;
e->disp();
}

a) a and disp cannot be accessed in main as it is private
b) a cannot be accessed in main as it is private
c) Constructor 20 In Display
d) Constructor 20 In Display Destructor

Answer(s) : 
c) Constructor 20 In Display

[quads id=1]

4) What is the output of the following code?

#include
using namespace std ;
namespace Ex { int x = 10; }
namespace Ex { int y = 10; }
int x = 5;
int main(){
using namespace Ex ;
x = y = 50;
cout << x << ” ” << y;
}

a) 10  10
b) 50  50
c) 5  50
d)

Compilation error: ambiguous reference to variable 'x'
Answer(s) : 
d) Compilation error: ambiguous reference to variable 'x'

5) Fill in the blank in the following program.

#include
using namespace std;
class Test { static int x;
public:
void get() { x = 15; }
void print() {
x = x + 20;
cout << “x =” << x << endl;
}
};
____________; // Define static variable ‘x’
int main() {
Test o1, o2;
o1.get(); o2.get();
o1.print(); o2.print();
return 0;
}

a)

int Test t.x = 0;

b)

Test t; t.x = 0;

c)

int Test::x = 0;

d)

t; t::x = 0;
Answer(s) : 
c) int Test::x = 0;

[quads id=1]

6) What will be the output of the following program ?

#include
using namespace std;
class Test { int x;
public:
Test(int i) : x(i) {}
friend void print(const Test& a);
};
void print(const Test& a) {
cout << “x = ” << a.x;
}
int main(){
Test t(10);
print(t);
return 0;
}

a)

x = 10

b)

Compilation Error: print cannot access x as it is private

c)

Compilation Error: illegal parameter passing in print

d)

Compilation Error: Const parameter cannot be passed in friend function
Answer(s) : 
a) x = 10

7) What will be the output of the following program ?

#include
using namespace std;
class sample {
public:
int x, y;
sample() {};
sample(int, int);
sample operator + (sample);
};
sample::sample (int a, int b) {
x = a;
y = b;
}
sample sample::operator+ (sample param) {
sample temp;
temp.x = x + param.x;
temp.y = y + param.y;
return (temp);
}
int main () {
sample a (4,1);
sample b (3,2);
sample c;
c = a + b;
cout << c.x << ” ” << c.y;
return 0;
}

a) 5  5
b) 7  3
c) 3  7
d) 4  6

Answer(s) : 
b) 7 3

[quads id=1]

8) What will be the output of the following program ?

#include
using namespace std;
class Test {
int i;
public:
Test(int ii) : i(ii) {}
const Test operator*(const Test& rv) const {
cout << “Executes *” << endl;
return Test(i * rv.i);
}
Test& operator+=(const Test& rv) {
cout << “Executes +=” << endl;
i += rv.i;
return *this;
}
};
int main() {
int i = 1, j = 2, k = 3;
k += i * j;
Test ii(1), jj(2), kk(3);
kk += ii * jj;
}

a)

Executes *
Executes +=

b)

Executes *
Executes +

c)

Executes +=
Executes *

d) Compilation Error: Ambiguous declaration

Answer(s) : 
a) Executes *Executes +=