1) What is the output of the sizeof operator for the variable ‘t’ in the following code snippet? (Assume sizeof(int) = 4).class Test {    int var;     int arr[9];    void display() { int a;}};int main() {    Test t;    cout << sizeof(t) << ” “;}

a) 40
b) 36
c) 44
d) Default size: 0

Answer(s) : 
a) 40

2) What will be the output / error of the  following program?#includeusing namespace std;class Sample {    int data_;    Sample(): data_(2){}};int main() {    Sample s;    s.data_ = 1;    cout << Sample.data_;}

a) 1
b) 2
c) Compilation Error: Sample() is private
d) 0

Answer(s) : 
c) Compilation Error: Sample() is private

3) What is the output of the following program ? #includeusing namespace std;class Test {  private:      int x_;    int y_;public:    void func() {         x_ = y_ = 1;         cout << x_ << ” ” << y_;    }};int main() {    Test t;    t.func();}

a) 1  1
b) Compilation error: Constructor not defined
c) Compilation error: Cannot access private member ‘x’ and ‘y’
d) Compilation error: Illegal access of func()

Answer(s) : 
a) 1 1

4) Consider Object S of class Sample. What is the type of this pointer ? 

a) S const * const this
b) S * const this
c) S * this
d) const S const * this

Answer(s) : 
b) S * const this

5) What is the o/p of the following code snippet ?class Sample {      string name;public:     Sample(string s): name(s) {     cout << name << ” Created” << ” “;}     ~Sample() {          cout << name << ” Destroyed” << ” “;      }};int main() {     Sample s1(“s1”), s2(“s2”);     return 0;}

a) S1 Created S2 Created S2 Destroyed S1 Destroyed
b) S1 Created S2 Created S1 Destroyed S2 Destroyed
c) S2 Created S1 Created S2 Destroyed S1 Destroyed
d) S1 Created S1 Destroyed S2 Created S2 Destroyed

Answer(s) : 
a) S1 Created S2 Created S2 Destroyed S1 Destroyed
Categories: NPTEL solution

5 Comments

ritwick · August 15, 2017 at 3:01 PM

thanks for this and please upload the solutions to 5 programming assignments of week 3 in the course “Programming in C++” by tomorrow..

raj · August 16, 2017 at 3:13 AM

programming assignments plz for week 3.

John Gracias · August 22, 2017 at 6:21 AM

We’re a group of volunteers and opening a new programming community. Your website offered us with valuable info to work on. You’ve done an impressive job and our entire community will be grateful to you.

Leave a Reply

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