Programming in Modern C++ | Week 6
Session: 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
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
Session: JULY-DEC 2023
Course Name: Programming in Modern C++
Course Link: Click Here
These are Nptel Programming in Modern C++ Assignment 6 Answers
Programming
Question 1
Consider the following program. Fill in the blanks as per the instructions given below:
• Complete the destructor statement,
• Complete the constructor statement,
such that it will satisfy the given test cases
Solution:
#include<iostream>
using namespace std;
class B{
public:
B(){ cout << "1 "; }
B(double n){ cout << n << " "; }
virtual ~B();
};
class D : public B{
public:
D(double n) : B(n)
These are Nptel Programming in Modern C++ Assignment 6 Answers
Question 2
Consider the following program. Fill in the blanks as per the instructions given below.
• at LINE-1, define fun as pure abstract function,
• at LINE-2, complete constructor definition,
• at LINE-3, complete constructor definition,
such that it will satisfy the given test cases.
Solution:
#include<iostream>
using namespace std;
class Test{
public:
virtual void fun()=0; //Line-1
};
class ReTest1 : public Test{
int d1;
public:
ReTest1(int n) : d1(n*2){ } //Line-2
void fun();
};
class ReTest2 : public Test{
int d2;
public:
ReTest2(int n) : d2(n*3){ } //Line-3
void fun(){
cout << d2 << " ";
}
};
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.
• at LINE-1, declare the function show() as pure virtual function,
• at LINE-2, with appropriate function call,
• at LINE-3, with appropriate function header,
such that it will satisfy the given test cases.
Solution:
#include <iostream>
using namespace std;
class shape{
protected:
int a,b;
shape(int x, int y) : a(x), b(y) {}
public:
virtual void show() = 0; //LINE-1
};
class triangle : public shape{
public:
triangle(int x, int y) : shape(x,y){}
void Area();
void show(){
cout << a + b << " "; //LINE-2
Area();
}
};
void shape::show(){ //LINE-3
cout << a+b << " ";
}
These are Nptel Programming in Modern C++ Assignment 6 Answers
More Weeks of Programming in Modern C++: Click here
More Nptel Courses: Click here
Course Name: Programming in Modern C++
Course Link: Click Here
These are Nptel Programming in Modern C++ Assignment 6 Answers
Quiz
Q1. Consider the following code segment.
What will be the output?
a) 13
b) 14
c) 23
d) 24
Answer: b) 14
Q2. Consider the following code segment.
What will be the output/error?
a) 10 6
b) 10 4
c) 8 6
d) 8 4
Answer: a) 10 6
These are Nptel Programming in Modern C++ Assignment 6 Answers
Q3. Consider the following code segment.
What will be the output?
a) ABC ~C ~B ~A
b) ABC ~C ~B
c) ABC~B~A
d) ABC ~A
Answer: d) ABC ~A
Q4. 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: b, c
These are Nptel Programming in Modern C++ Assignment 6 Answers
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, d
Q6. Consider the following code segment.
What will be the output?
a) A::f(0) B::g() C::h()
b) C::f() C::g() B::h()
c) C::f() B::g() B::h()
d) C::f() C::g() C::h()
Answer: b) C::f() C::g() B::h()
These are Nptel Programming in Modern C++ Assignment 6 Answers
Q7. Consider the following code segment.
Fill in the blank at LINE-1 so that the program will print 2.
a) cb->B::fun()
b) B::fun()
c) B::cb->fun()
d) cb->fun()
Answer: a) cb->B::fun()
Q8. Consider the following code segment.
Identify the abstract classes.
a) Vehicle, Car, MotorCycle
b) Vehicle, Car, SUV
c) Vehicle, Car
d) Vehicle, Car, SportsCar, SUV
Answer: d) Vehicle, Car, SportsCar, SUV
These are Nptel Programming in Modern C++ Assignment 6 Answers
Q9. Consider the following code segment.
What will be the output?
a) 0
b) 1
c) 2
d) Garbage
Answer: a) 0
Programming Assignment of Programming in Modern C++
Question 1
Complete the program with the following instructions.
• Fill in the blank at LINE-1 with proper access specifier.
• Fill in the blanks at LINE-2 to declare area() as a pure virtual function.
The program must satisfy the given test cases.
Solution:
#include<iostream>
using namespace std;
class Shape
{
protected:
double ar;
public:
virtual void area()=0;
void show()
{
cout << ar << " ";
}
};
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 with appropriate destructor declaration.
• Fill in the blank at LINE-2 with appropriate initialization list of derived class constructor.
The program must satisfy the sample input and output.
Solution:
#include<iostream>
using namespace std;
class Base
{
public:
Base()
{
cout << "1 ";
}
Base(int n)
{
cout << n << " ";
}
virtual ~Base()
{
cout << "2 ";
};
};
class Derived : public Base
{
int num;
public:
Derived()
{
cout << "3 ";
}
Derived(int n) : Base(n), num(n*3)
{
cout << num << " ";
}
virtual ~Derived()
{
cout << "4 ";
}
};
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 blanks at LINE-1 with an appropriate keyword.
• Fill in the blank at LINE-2 with an appropriate function declaration.
• Fill in the blank at LINE-3 with an appropriate statement.
such that it will satisfy the given test cases.
Solution:
#include<iostream>
using namespace std;
class B
{
protected:
int s;
public:
B(int i=0) : s(i)
{}
virtual ~B()
{}
virtual int fun(int x)=0;
};
class D : public B
{
public:
D(int i) : B(i)
{}
~D();
int fun(int x)
{
return s+x;
}
};
class Z
{
public:
void fun(int a, int b)
{
B *t = new D(a);
int i = t->fun(b);
cout << i << " ";
delete t;
}
};
These are Nptel Programming in Modern C++ Assignment 6 Answers
These are Nptel Programming in Modern C++ Assignment 6 Answers
More Solutions of Programming in Modern C++: Click Here
More NPTEL Solutions: https://progiez.com/nptel-assignment-answers/
The content uploaded on this website is for reference purposes only. Please do it yourself first. |