Programming in Modern C++ Week 6 Assignment Answers
Are you looking for the NPTEL Programming in Modern C++ Week 6 Assignment Answers? You’ve come to the right place! This resource provides comprehensive solutions to all the questions from the Week 6 assignment, helping you navigate through the complexities of modern C++ programming.
Course Link: Click Here
Table of Contents

Programming in Modern C++ Week 6 Assignment Answers (Jan-Apr 2025)
Que. 1 Consider the following code segment.
#include<iostream>
using namespace std;
class classA{
public:
void funi() { cout << "1" ; }
virtual void fun2() { cout << "3" ; }
};
class classB : public classA{
public:
void funi() { cout << "2" ; }
void fun2() { cout << "4" ; }
};
int main(){
classA *t = new classB();
t->funi();
t->fun2();
return 0;
}
What will be the output?
a) 13
b) 14
c) 23
d) 24
Answer: B
Que. 2 Consider the following code segment.
#include<iostream>
using namespace std;
class A{
public:
virtual void fun() { cout << "class1" << endl; }
};
class B : public A{
public:
void fun() { cout << "class2" << endl; }
};
int main(){
B t;
A *t2 = new B();
A &t3 = t;
t2->fun();
t3.fun();
return 0;
}
What will be the output/error?
a) class1 class1
b) class1 class2
c) class2 class1
d) class2 class2
Answer: D
Que. 3 Consider the following code segment.
#include<iostream>
using namespace std;
int x = 0;
class Base{
public:
Base(){ x = x+3; }
~Base() { x = x-5; }
};
class Derived : public Base{
public:
Derived(){ x = x+7; }
~Derived(){ x = x-1; }
};
void fun(){
Derived t;
Base *t1 = new Derived();
cout << x << " ";
delete t1;
}
int main(){
fun();
cout << x;
return 0;
}
What will be the output?
a) 208
b) 209
c) 177
d) 176
Que. 4 Consider the following code segment.
#include<iostream>
using namespace std;
class classA{
public:
classA() { cout<<"A "; }
~classA() { cout<<"A "; }
};
class classB : public classA{
public:
classB() { cout<<"B "; }
~classB() { cout<<"B "; }
};
class classC : public classB{
public:
classC() { cout<<"C "; }
virtual ~classC() { cout<<"C "; }
};
int main(){
classA *t1 = new classC;
delete t1;
return 0;
}
What will be the output?
a) ABC AC AB AA
b) ABCC B
c) ABC ABA
d) ABCA
Que. 5 Consider the following code segment.
#include<iostream>
using namespace std;
class base{
int a;
public:
base(int i) : a(i) {}
virtual void test(base *) { cout << a << endl; }
};
class derived : public base{
int b;
public:
derived(int i=0, int j=0) : base(i), b(j) {}
void test(derived *) { cout << b << endl; }
};
int main(){
base *t1 = new derived(5,9);
t1->test(new derived); //Line-1
return 0;
}
What will be the output?
a) 0
b) 5
c) 9
d) garbage
Que. 6 Consider the following code segment.
#include<iostream>
using namespace std;
class A{
public:
void print(){ cout << "A"; }
};
class B : public A{
public:
void print (double d) { cout << "B"; }
};
int main(){
B t1;
t1.print(); //LINE-2
t1.print(9.81);
return 0;
}
Fill in the blank at LINE-1 such that the output is AB.
a) void print(){ cout << “A” ; }
b) using A::print
c) void print (int i){ }
d) void print({ }
Que. 7 Consider the following code segment.
#include<iostream>
using namespace std;
class A{
public:
int a;
};
class B {
public:
int b=9;
};
class C {
public:
double c=3.14;
};
int main(){
A p;
B t1;
C t2;
p = (A)t1;
cout << p.a <<" ";
p = (A)t2;
cout << p.a <<" ";
return 0;
}
What will be the output?
a) 55
b) 93
c) 93.14
d) 9
Que. 8 Consider the following code segment.
class Vehicle{
public:
virtual void run() = 0;
virtual void stop() = 0;
};
class Car : public Vehicle{};
class MotorCycle : public Vehicle{
public:
void run(){}
};
class Truck : public Car{
public:
void run(){}
void stop(){}
};
class SportsCar : public Car{
public:
void run(){}
virtual void runTurbo() = 0;
void stop(){}
};
class SUV : public Car{
public:
void run(){}
void stop(){}
};
Identify the abstract classes.
a) Vehicle, Car, MotorCycle
b) Vehicle, Car, SUV
c) Vehicle, Car
d) Vehicle, Car, MotorCycle, SportsCar
Que. 9 Consider the following code segment.
#include<iostream>
using namespace std;
class A{
public:
virtual void print(){ cout << "A "; }
};
class B : public A{
public:
void print(){ cout << "B"; }
};
class C : public B{
public:
void print(){ cout << "C "; }
};
int main(){
A *cb = _________; //LINE-1
cb->print();
return 0;
}
Fill in the blank at LINE-1 such that the program will print B.
a) new A
b) new B
c) new C
d) It cannot be printed
Programming in Modern C++ Week 6 Assignment Answers (July-Dec 2024)
Q1.Which line/s will give error?
a) AC
b) AD
c) BC
a) BD
Answer: b) AD
Q2 Consider the following code segment.
What will be the output /error?
a) Mammal Labrador
b) Mammal 0
c) Compilation error at LINE-1: class Dog has no member named ’displayType’
d) Compilation error at LINE-2: class Animal has no member named ’displayBreed’
Answer: d) Compilation error at LINE-2: class Animal has no member named ’displayBreed’
For answers or latest updates join our telegram channel: Click here to join
Q3.Consider the following code segment.
What will be the output?
a) XYZ ~Z~Y ~X
b) XYZ~ Z ~Y
c) XYZ ~Y ~X
d) XYZ ~X
Answer: d) XYZ ~X
Q4.Consider the following code segment.
Fill in the blank at LINE-1 such that the program runs successfully and prints display ()
a) Base: :display();
b) using Base: :display;
c) Base.display();
d) void display();
Answer: b) using Base: :display;
For answers or latest updates join our telegram channel: Click here to join
Q5.Consider the following code segment.
‘What will be the output?
a) B1 B2 B1 F D1 D2 D1
b) B1 B2 F D1 D2 D1
c) B1 B2 F D2 D1
d) BL B2 F D2
Answer: b) B1 B2 F D1 D2 D1
Q6. Consider the following code segment.
Fill in the blank at LINE-1 such that the program gives output as
Circle: :draw
a) void draw() = 0
b) virtual void draw()
c) virtual void draw() = 0
d) void draw()
Answer: c) virtual void draw() = 0
For answers or latest updates join our telegram channel: Click here to join
Q7. Consider the following code segment.
‘What will be the output?
a) 0
b) 3
c) 4
d) Garbage
Answer: a) 0
Q8. Consider the following code segment.
What will be the output?
a) X::display()
b) Y::display()
c) Z::display()
d) Compilation error: cannot call member function of class ‘Y’
Answer: b) Y::display()
For answers or latest updates join our telegram channel: Click here to join
Q9.Consider the following code segment.
What will be the output /error?
a) Base2::show()Base2::display()Basei::printBase2: :show()Base2: :display()Basei::print
b) Base2::show()Basei::display()Base2: :printBase2: :show()Base2: :display()Basei: :print
c) Base2::show()Base2: :display()Basel::printBase2: :show()Basel::display()Base2: :print
d) compile time error
Answer: a) Base2::show()Base2::display()Basei::printBase2: :show()Base2: :display()Basei::print
For answers or latest updates join our telegram channel: Click here to join
For answers to additional Nptel courses, please refer to this link: NPTEL Assignment
Programming in Modern C++ Week 6 Assignment Answers (JAN-APR 2024)
Course Name: Programming in Modern C++
Course Link: Click Here
For answers or latest updates join our telegram channel: Click here to join
These are Nptel Programming in Modern C++ Assignment 6 Answers
Quiz
Q1. Consider the following code segment.
What will be the output?
a) A1B1
b) A1B2
c) A2B1
d) A2B2
Answer: b) A1B2
Q2. Consider the following code segment.
What will be the output?
a) 8 8 1
b) 8 8 4
c) 1 8 4
d) 0 1 4
Answer: b) 8 8 4
Q3. Consider the following code segment.
What will be the output?
a) A B C ~C ~B ~A
b) A B C ~C ~B
c) A B C ~B ~A
d) A B C ~A
Answer: d) A B C ~A
For answers or latest updates join our telegram channel: Click here to join
These are Nptel Programming in Modern C++ Assignment 6 Answers
Q4. Consider the following code segment.
Which line/s will NOT give you error?
a) LINE-1
b) LINE-2
c) LINE-3
d) LINE-4
Answer: a) LINE-1
Q5. Consider the following code segment.
Which line/s will give you error?
a) LINE-1
b) LINE-2
c) LINE-3
d) LINE-4
Answer: a), c)
Q6. Consider the following code segment.
Fill in the blank at LINE-1 such that the program will print Class2: : fun().
a) new Class1
b) new Class2
c) new Class3
d) It cannot be printed
Answer: b) new Class2
For answers or latest updates join our telegram channel: Click here to join
These are Nptel Programming in Modern C++ Assignment 6 Answers
Q7. Consider the following code segment.
What will be the output?
a) 0
b) 1
c) 2
d) Garbage values
Answer: a) 0
Q8. Consider the following code segment.
Fill in the blank at LINE-1 so that the program will print 1.
a) cb-›f()
b) classA: : cb->f (
c) cb->classA: :f ()
d) classA: :f ()
Answer: c) cb->classA: :f ()
Q9. Identify the abstract class/es from the following code snippet.
a) Flower, FlowerWSmell, FlowerWOSmell
b) Flower, FlowerWOSmell, Dahlia
c) Flower, FlowerWSmell, FlowerWOSmell, Sunflower
d) Flower
Answer: b) Flower, FlowerWOSmell, Dahlia
For answers or latest updates join our telegram channel: Click here to join
These are Nptel Programming in Modern C++ Assignment 6 Answers
Programming Assignment
Question 1
Complete the program with the following instructions.
• Fill in the blank at LINE-1 with appropriate keyword,
• Fill in the blanks at LINE-2 to declare fun () as a pure virtual function,
• fill in the blank at LINE-3 with approriate object instantiation,
The program must satisfy the given test cases.
Solution:
#include<iostream>
using namespace std;
class Base{
protected:
int s;
public:
Base(int i=0) : s(i){}
virtual ~Base(){ }
virtual int fun() = 0;
};
class Derived : public Base{
int x;
public:
Derived(int i, int j) : Base(i), x(j) {}
~Derived();
int fun(){
return s*x;
}
};
class Wrapper{
public:
void fun(int a, int b){
Base *t = new Derived(a, b);
int i = t->fun();
cout << i << " ";
delete t;
}
};
For answers or latest updates join our telegram channel: Click here to join
These are Nptel Programming in Modern C++ Assignment 6 Answers
Question 2
Consider the following program with the following instructions.
• Fill in the blank at LINE-1 and LINE-2 with appropriate return statements
The program must satisfy the sample input and output.
Solution:
double Customer::getMaturity(){
return principal + getInterest();
}
double Employee::getMaturity(){
return principal + getInterest() + getExtraAmt();
}
For answers or latest updates join our telegram channel: Click here to join
These are Nptel Programming in Modern C++ Assignment 6 Answers
Question 3
Consider the following program. Fill in the blanks as per the instructions given below :
• Fill in the blank at LINE-1 with an appropriate destructor declaration.
• Fill in the blank at LINE-2 with an appropriate constructor statement.
such that it will satisfy the given test cases.
Solution:
#include<iostream>
using namespace std;
class A{
public:
A(){ cout << "11 "; }
A(int n){ cout << n + 2 << " "; }
virtual ~A();
};
class B : public A{
public:
B(int n) : A(n)
{
cout << n + 5 << " ";
}
B(){ cout << "21 "; }
virtual ~B(){ cout << "22 "; }
};
For answers or latest updates join our telegram channel: Click here to join
These are Nptel Programming in Modern C++ Assignment 6 Answers
More Weeks of Programming in Modern C++: Click here
More Nptel Courses: Click here