
Are you looking for help in Programming in Modern C++ NPTEL Week 5 Assignment Answers? So, here in this article, we have provided Programming in Modern C++ week 5 Assignment Answer’s hint.
Programming in Modern C++ NPTEL Assignment Week 5 Answers
Q1. Consider the following code segment.
#include<iostream>
using namespace std;
class myClass1 {
static int x1;
double x2;
public:
void fun() { cout << "fun" << endl; }
};
class myClass2 : public myClass1 {
double d1;
};
int myClass1::x1 = 0;
int main(){
myClass2 d;
cout << sizeof(d);
return 0;
}
What will be the output? (Assume sizeof(double) = 8 and sizeof(int) = 4)
a. 16
b. 12
c. 20
d. 24
Answer: a. 16
800+ Students get the advantage of instant notification on telegram.
Q2. Consider the following code segment.
#include<iostream>
using namespace std;
class base{
protected:
int n1;
public:
base (int x) : n1(x) { }
void f1() { cout << ++ni << endl; }
};
class derived : public base {
public:
derived (int x): base(x) { }
void f1(int a) { cout << ++n1 <<< endl; }
};
int main(){
derived d(10);
d.f1(); //LINE-1
return 0;
}
What will be the output/error?
a. 10
b. 11
c. 12
d. Compilation Error: no matching function for call to derived::f1()
Answer: d. Compilation Error: no matching function for call to derived::f1()
Q3. Consider the following code segment.
#include <iostream>
using namespace std;
class base {
public:
void print() { cout << "C" << " "; }
};
class derived public base {
public:
void print() { cout << "C++" << " "; }
};
int main(){
base *a1 = new base();
base *b1 = new derived();
a1->print();
b1->print();
return 0;
}
What will be the output?
a. C C
b. C C++
c. C++ C
d. C++ C++
Answer: a. C C
Q4. Consider the following code segment.
#include <iostream>
using namespace std;
class A{
public:
int a;
A(int x) a(x) { }
};
class B protected A{
int b;
public:
B(int x, int y): b(y), A(x) { }
};
int main(){
B t1(1,2);
A t2(5);
cout << t1.a; //LINE-1
cout<< t2.a; //LINE-2
return 0;
}
Which line will generate compilation error in the main() function?
a. LINE-1
b. LINE-2
c. Both LINE-1 and LINE-2
d. No compilation error
Answer: a. LINE-1
Q5. Consider the following code segment.
#include<iostream>
using namespace std;
class A{
public:
A() { cout<<"A"; }
"A() { cout << ""A"; }
};
class B : public A {
public:
B() { cout << "B"; }
B() { cout << "B"; }
};
class C : public B{
B b;
public:
C() { cout << "C"; }
"C() { cout << " "; }
};
int main(){
C obj;
return 0;
}
What will be the output?
a. A B C ~C~B ~A
b. A A B C ~C ~B ~A~A
c. A BABC ~C~B ~A~B ~A
d. A A B C ~C ~A~A
Answer: Option C
Q6. Consider the following code segment.
#include<iostream>
using namespace std;
class base{
public:
static void func() { cout << "C++" << endl; }
};
class derived : private base {
public:
derived() {
________________ ; } //LINE-1
};
int main(){
derived t1;
return 0;
}
Fill in the blank at LINE-1 such that the output is C++.
a. (new base)->func()
b. base::func()
c. base. func ()
d. base::func
Answer: a. (new base)->func()
b. base::func()
Q7. Consider the following code segment.
#include<iostream>
using namespace std;
class A1{
public:
int t1;
};
class A2 : private A1{
public:
int t2;
int sum(){ return t1 + t2; }
};
int main(){
A1 b;
A2 d;
b. t1 = 10;
//LINE-1
d. t1 = 20;
//LINE-2
d.t2 = 30;
//LINE-3
cout<<d.sum(); //LINE-4
return 0;
}
Which line/s in the main function will generate compilation error?
a. LINE-1
b. LINE-2
c. LINE-3
d. LINE-4
Answer: b. LINE-2
Q8. Consider the following code segment.
#include<iostream>
using namespace std;
class B{
int x;
public:
B(int x) x(x){ }
int fun(){ return x; }
};
class D : public B{
int y;
public:
D(int x, int _y): _____________{} //LINE-1
void fun(){ cout << B::fun() <<< y; }
};
int main(){
D=b2 = new D(1,0);
b2->fun();
return 0;
}
Fill in the blank at LINE-1 such that the program will print 10.
a. B(_x), y(_y)
b. B(_y), y(_x)
c. y(_y), B(_x)
d. y(_x), B(_y)
Answer: a. B(_x), y(_y)
c. y(_y), B(_x)
Q9. Consider the following code segment.
#include<iostream>
using namespace std;
class myClass1{
int x;
public:
myClass1(int a) : x(a){}
void fun(){
cout << x;
}
};
class myClass2: public myClass1{
int y;
public:
myClass2 (int a, int b) myClass1(a), y(b) {}
void fun(){
cout << y;
}
};
int main(){
myClass2 m(1,2);
_______________; //LINE-1
return 0;
}
Fill in the blank at LINE-1 such that the program will print 1.
a. myClass1.m::fun()
b. m.myClass1::fun()
c. m.myClass1.fun()
d. myClass1.m.fun()
Answer: b. m.myClass1::fun()
TELEGRAM FOR NOTIFICATION | Click 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: