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

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

Answer(s) : 
a) ==

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

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;

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

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 +=
Categories: NPTEL solution

11 Comments

Shreya Purohit · August 22, 2017 at 4:58 AM

This site has really fantastic layout and Perfect content.

raj · August 23, 2017 at 5:32 AM

week 4 programming assignments please?? Last date is today(23rd August)

bijay · August 23, 2017 at 5:36 AM

Sir, week 4 programming assignments????

    Shyam Joshi · August 23, 2017 at 11:15 AM

    Soon!

bijay · August 23, 2017 at 4:03 PM

Sir please we are in hurry!!!

Suresh · August 27, 2017 at 4:10 PM

Please post week 5 assignment solution for c++ asap

Ritesh lamba · August 28, 2017 at 3:54 PM

Sir please upload the solution of week 5

Anon · August 31, 2017 at 12:47 PM

What happened you no longer update the site?

    Smit Parmar · August 31, 2017 at 3:21 PM

    Just keep faith, We will provide solution ASAP

Raju · September 7, 2017 at 7:18 AM

Sir, Week 5 and 6? We would really appreciate if it is uploaded soon!

sai · September 17, 2017 at 1:16 PM

sir please post solution for assignment 8 asap
there is Programming test on 21 september ,please help us with it

Leave a Reply

Your email address will not be published. Required fields are marked *