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.

SIKSHAPATH Latest Articles

Programming in Modern C++ NPTEL Assignment Week 4 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 4 Assignment Answers? So, here in this article, we have provided Programming in Modern C++ week 4 Assignment Answer’s hint.

Programming in Modern C++ NPTEL Assignment Week 4 Answers

Q1. Consider the following code segment.

#include<iostream>
using namespace std;
class myClass{
static int s;
public:
myClass (ints): s(s) { }
 void incr(){ s = s+10; }
 void print(){ cout << s; }
};
int myClass::s = 10;
int main(){
myClass 01(5), 02(10);
01.incr();02. incr();
02.print();
return 0;
}

What will be the output/error?

a. 15
b. 20
c. 30
d. Compilation Error: myClass::s is a static data member; it can only be initialized at its definition

Answer: d. Compilation Error: myClass::s is a static data member; it can only be initialized at its definition

750+ Students getting advantage of instant notification on telegram.


Q2. Consider the following code segment.

#include<iostream> 
using namespace std; 
class Point{
int x, y;
public:
Point (int r=0, int i=0): x(r), y(i) {} 
Point& operator<< (const Point& c){ //Line-1 
 cout << x+c.x << "," << y+c.y << endl;
 return *this;
}
friend Point& operator<<(ostream& os, Point& c);
};
Point& operator<<(ostream& os, Point& c) { //Line-2 cout << c.x <<<","<<< c.y << endl;
return c;
}
int main(){ Point c1 (2,5), c2(4,6); cout << c1 <<< c2; return 0;
}

What will be the output?

a. 2,5

4,6

b. 6,5

2, 11

c. 6, 11

2,5

d. 2,5

6,5

Answer: Option D


Q3. Consider the following code segment.

#include <iostream>
using namespace std;
int var= 0;
namespace name {
int var= 2;
}

int main() {
int var= 1;
{
using namespace name;
cout << ::var << " " << var << " " << name::var; // LINE-1}
return 0;
}

What will be the output/error?

a. 0 1 2

b. 0 2 2

c. 2 0 1

d. Compilation Error: reference to ‘var’ is ambiguous

Answer: a. 0 1 2


Q4. Consider the following code segment.

#include <iostream>
using namespace std;
class myClass {
static int X; 
static int Y;
 public:
   _______ void print() {   //LINE-1 
            cout << x << " " << Y;
         }
void setX(int a){
X=a;
}
void setY(int a){
Y=a;
}
};
int myClass::X = 10;
int myClass::Y = 10;
int main() {
myClass t1, t2;
t1.setX(4);
t2.setY (5);
 myClass::print();
return 0;
}

Fill in the blank at LINE-1 such that the output will be 4 5.

a. mutable

b. static

c. const

d. friend

Answer: b. static


Q5. Consider the following code segment.

#include<iostream>
using namespace std;
int x=10;
namespace e{
namespace e1{
 int x=15;
}
int x=5;
}
int main(){
  ________________; //LINE-1 
cout << x;
return 0;
}

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

a. using e::e1::x

b. using e::e1

c. using namespace e::e1

d. using namespace e

Answer: a. using e::e1::x


Q6. Consider the following code segment.

#include<iostream>
using namespace std;
class constTest{
     ______________x; //LINE-1 
    public:
     constTest(int x) = x(x) {}
 void set (int a) const{
   x = a;
}
void print() const{
cout << x <<< endl;
   }
};

int main(){
const constTest m(5);
 m.set(10);
m.print();
return 0;
}

Fill in the blank at LINE-1 such that the output is 10.

a. int mutable

b. mutable int

c. volatile int

d. const int

Answer: a. int mutable

b. mutable int


Q7. Consider the following code segment.

#include<iostream>
using namespace std;
 namespace name{
class Student{
 int roll;
public:
Student (int x): roll(x) {}
 void print(){ cout << roll; }
};
}

int main(){
   _______________; //LINE-1 
s.print();
return 0;
}

Fill in the blank at LINE-1 such that the output is 5.

a. name::Student s(5)

b. Student s(5)

c. name. Student s(5)

d.  using name::Student s(5)

Answer: a. name::Student s(5)


Q8. Consider the following code segment.

#include<iostream>
using namespace std;
class Point{
int x, y;
public:
Point (int x, int y) x(x), y(y) {}
__________________; //LINE-1 
};
Point& operator<< (ostream& os, Point& p){
 cout<<"("<<< p.x << "," << p.y << ")" << endl;
 return p;
}
int main(){
Point pt (2,5);
cout << pt;
return 0;
}

Fill in the blank at LINE-1 such that the program will print (2,5).

a. Point& operator<<< (ostream&, Point&)

b. friend Point& operator<< (ostream&, Point&)

c. Point& friend operator<<< (ostream&, Point&)

d. const Point& operator<< (ostream&, Point&)

Answer: b. friend Point& operator<< (ostream&, Point&)


Q9. Consider the following code segment.

#include<iostream>
using namespace std;
class A{
static int a;
public:
int get(){ return a; }
     _________________; //LINE-1 
};
int A::a = 0;
class B{
int b;
public:
B(int y) b(y) {}
void print(){
A::a = 10;
cout << A::a-b << " " << A::a+b;
}
};
int main(){
B t2(5);
t2.print();
return 0;
}

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

a. static class B

b. friend class B

c. class friend B

d. using class B

Answer: b. friend class B


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 3 Answers

Programming in Modern C++ NPTEL Assignment Week 5 Answers

Related Posts