Sign Up

Sign In

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Sorry, you do not have permission to ask a question, You must login to ask a question.

Programming in Modern C++ Assignment Week 3 Answers 2022

A blog banner of programming in modern c++ nptel course answer

Are you looking for help in Programming in Modern C++ NPTEL Week 3 Assignment Answers? So, here in this article, we have provided Programming in Modern C++ week 3 Assignment Answer’s hint.

Programming in Modern C++ NPTEL Assignment Week 3 Answers

Q1. Consider the following code segment.

class Complex{
private:
int re, im;
public:
void setRE(int r){ re=r_; }
void setIM(int i_){ im = i_; }
void print(){ cout << re <<< ", i" << im; } 
void incr(){ re++, im++; }
int incrRE(){ return re + 1; }
int incrIM(){ return ++im; }
};

Identify set of all methods that change the state of Complex class objects?

a. setRE(), setIM(), print()
b. setRE(), setIM(), incrRE(), incrIM()
c. incr(), incrRE(), incrIM()
d. setRE(), setIM(), incr(), incrIM()

Answer: d. setRE(), setIM(), incr(), incrIM()

For instant notification of any updates, Join us on telegram.


Q2. Consider the following code segment.

#include <iostream>
using namespace std;
class Number {
int n;
public:Number () {
cout << 0 <<< " ";
}
Number(int i): n(i) {
cout << n << " ";
}
};
int main() {
int i = 1;
Number n1(); //LINE-1
Number *n2 = new 
Number (i++);
Number *n3;
new Number (i++);
return 0;
}

What will be the output?

a. 0 1 0 2

b. 0 1 2

c. 0 2 3

d. 1 2

Answer: d. 1 2


Q3. Consider the following code segment.

#include <iostream>
#include <cstring> 
using namespace std;
class MyClass {
const char ----------; // LINE-1: declare the data members
public:
MyClass (const char* s1, const char* s2, const char* _s3) : 
s1 (setS1 (s1)), s2 (setS2 (s2)), 
s3(setS3(_s3)){}
const char* setS1 (const char* s) { 
cout << s <<< " ";
return strdup(s);
}
const char* setS2 (const char* s) { 
cout << s <<< " "; 
return strdup(s);
}
const char* setS3 (const char* s) { 
cout << s <<< " ";
return strdup(s);
}
};
int main() {
MyClass obj("programming", "in", "C++");
return 0;
}

Fill in the blank at LINE-1 such that the program will print in C++ programming?

a. *s2, *s3, *s1

b. *s1, *s2, *s3

c. *s1, *s3, *s2

d. *s2, *s1, *s3

Answer: a. *s2, *s3, *s1


Q4. Consider the following code segment.

#include <iostream>
#include <string>
using namespace std;
class Test{
int t;
public:
int set t(int t) const {
_t = t;
}
int get_t() const {
return _t;
}
};
int main(){
Test obj; 
obj.set_t (5); 
cout<<obj.get_t();
return 0;
}

What will be the output/error?

a. 0

b. 5

c. Compiler error: assignment of data-member Test::_t is read-only object

d. Compiler error: cannot have const function for non-const object

Answer: c. Compiler error: assignment of data-member Test::_t is read-only object


Q5. Consider the following code segment.

#include <iostream> 
using namespace std;
class Complex {
int re, im;
public: 
Complex(int re, int im): re(_re), im(_im) { } 
void change (Complex *new_C) { this = new_C; } 
void show() { cout << re << " + i" << im <<< endl; 
}
};
int main() {
Complex c1 (10, 20); 
Complex c2(20, 50);
c1.change(&c2);
c1.show();
return 0;
}

What will be the output/error?

a. 10 + i20

b. 20 + i50

c. Compiler Error: lvalue required as left operand of assignment

d. Compiler Error: private x, y are inaccessible

Answer: c. Compiler Error: lvalue required as left operand of assignment


Q6. Consider the following code segment.

class myClass { 
// code...
};
int main() { 
const myClass m; // LINE-1 
return 0;
}

What is the type of this pointer associated with the object m?

a. const myClass* this;

b. myClass* const this;

c. myClass const* const this;

d. const myClass* const this;

Answer: c. myClass const* const this;

d. const myClass* const this;


Q7. Consider the following code segment.

#include<iostream>
using namespace std;
class Data {
public:
Data() { cout << "0"; }        //LINE-1Data 
(Data *t) { cout << "K"; }      //LINE-2
Data(const Data &t) { cout << "Z"; }   //LINE-3
}; int main() { Data *t1, t2; t1= new Data(); t2 = new Data(t1); Data t3 = *t1; Data t4 = t3; return 0; }

What will be the output?

a. OKKK

b. OKZZ

c. OKKZ

d.  OZZZ

Answer: b. OKZZ


Q8. Consider the following code segment.

#include<iostream>
using namespace std;
class String {
char x;
public:
String (char x): x(x) { }
void display() { cout << _______<< " "; } //LINE-1
};
int main() {
String c('C');
c.display();
return 0;
}

Fill in the blank at LINE-1 such that the program will print D.

a. ++this->x

b. ++this.x

c. ++x

d. x++

Answer: a. ++this->x

c. ++x


Q9. Consider the following code segment.

#include<iostream> 
using namespace std; 
static int i = 5; 
class myClass { 
public:
myClass() { cout <<< ++; } 
myClass() { cout << i--; }
};
void check (myClass c){
//Some Code} 
int main() { 
myClass c1; 
check (c1); 
return 0;
}

What will be the output?

a. 5665

b. 555

c. 665

d. 6565

Answer: c. 665


TELEGRAM FOR NOTIFICATIONClick Here
Follow on Google News (in one click)Click Here

Disclaimer: These answers are provided only for the purpose to help students to take references. This website does not claim any surety of 100% correct answers. So, this website urges you to complete your assignment yourself.


Also Available:

Programming in Modern C++ NPTEL Assignment Week 2 Answers

Programming in Modern C++ NPTEL Assignment Week 4 Answers

Related Posts

Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.