Programming in Modern C++ | Week 5

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


Quiz

Q1. Consider the following code segment.
What will be the output/error?
a) 6.75
b) 0
c) 675
d) Compilation error: no matching function for call to ‘Derived::calculate()’

Answer: d) Compilation error: no matching function for call to ‘Derived::calculate()’


Q2. Consider the following code segment.
What will be the output?

a) C Programming
C++ Programming
b) C++ Programming
C Programming
c) C Programming
C Programming
d) C++ Programming
C++ Programming

Answer: c) C Programming
C Programming


Q3. Consider the following code segment.
What will be the output?

a) 1 2 3 -3 -2 -1
b) 1 2 1 2 3 -3 -2 -1 -2 -1
c) 1 3 -3 -1
d) 1 1 2 3 -3 -2 -1 -1

Answer: b) 1 2 1 2 3 -3 -2 -1 -2 -1


For answers or latest updates join our telegram channel: Click here to join

These are Nptel Programming in Modern C++ Assignment 5 Answers


Q4. Consider the following code segment.
Fill in the blank at LINE-1 so that the program will print Father.

a) Father::print();
b) Father.print();
c) (new Father)->print();
d) Father->print();

Answer: a), c)


Q5. Consider the following code segment.
Fill in the blank at LINE-1 so that the output is 1.

a) obj.Base->f ()
b) obj. Base::f()
c) obj.Base.f()
d) Base : : f()

Answer: b) obj. Base::f()


Q6. Consider the following code segment.
Fill in the blank at LINE-1 so that the program will print 30 20 10.

a) Class2::x << ” ” << Class1 : : x << ” ” << x
b) Class2::x << ” ” << Class1 : : x << ” ” << : : x
c) x << ” ” << Class1 : : x << ” ” << : : x
d) : : x << ” ” << Class1 : : x << ” ” << : : x

Answer: b), c)


For answers or latest updates join our telegram channel: Click here to join

These are Nptel Programming in Modern C++ Assignment 5 Answers


Q7. Consider the following code segment.
Fill in the blank at LINE-1 so that the program will print 23.

a) Print (_x), y(_y)
b) Print (_y), y(_x)
c) y(_y), Print (_x)
d) y(_x), Print (_y)

Answer: a), c)


Q8. Consider the following code segment.
What will be the output /error?

a) 1 2
b) 1 3
c) 1 1
d) Compilation error: int A::x is inaccessible

Answer: d) Compilation error: int A::x is inaccessible


Q9. Consider the following code segment.
Fill in the blank at LINE-1 such that the program will run successfully without any error.

a) private
b) protected
c) public
d) friend

Answer: c) public


For answers or latest updates join our telegram channel: Click here to join

These are Nptel Programming in Modern C++ Assignment 5 Answers


Programming

Question 1

Complete the program with the following instructions.
• Fill in the blank at LINE-1 with appropriate inheritance statement,
• Fill in the blanks at LINE-2 appropriate initializer list,
The program must satisfy the given test cases.

Solution:

class DD : public B, public D {
    int d;
public:
    DD(int x) : B(x * 2), D(x * 3), d(x * 1) {}

    void show() {
        cout << d << ", " << b1 << ", " << b2;
    }
};

For answers or latest updates join our telegram channel: Click here to join

These are Nptel Programming in Modern C++ Assignment 5 Answers


Question 2

Consider the following program with the following instructions.
• Fill in the blank at LINE-1 with appropriate keyword.
• Fill in the blanks at LINE-2 and LINE-3 to complete the constructor statements.
The program must satisfy the sample input and output.

Solution:

#include<iostream>
using namespace std;
class Student{
    string n;
    string s;
    int m;
    protected:
        Student(string _n, string _s, int _m) : n(_n), s(_s), m(_m) {}
    public:
        friend void studDetails(const Student&);
};
class Bengali : public Student{
    public:
        Bengali(string n, int m) : Student(n, "Bengali", m){}
};
class English : public Student{
    public:
        English(string n, int m) : Student(n, "English", m){}
};

For answers or latest updates join our telegram channel: Click here to join

These are Nptel Programming in Modern C++ Assignment 5 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 appropriate return statement,
• Fill in the blank at LINE-2 with appropriate return statement,
The program must satisfy the given test cases.

Solution:

class Sphere : public Area, public Volume{
    int r;
    public:
        Sphere(int _r) : r(_r){ }
        double getArea(){ return Area::getVal(r); }
        double getPerimeter(){ return Volume::getVal(r); }
};

For answers or latest updates join our telegram channel: Click here to join

These are Nptel Programming in Modern C++ Assignment 5 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 5 Answers


Programming

Question 1
Consider the following program. Fill in the blanks as per the instructions given below:
• Complete the variable declaration at LINE-1,
• Complete the function prototype at LINE-2 and LINE-3 with appropriate keywords
such that it will satisfy the given test cases.

Solution:

class Cube : public Volume, public SurfaceArea { //LINE-1
    int _a;
    public:
        Cube(int a) : _a(a) { }
        double getVolume() { return Volume::getValue(_a); } //LINE-2
        double getSurfaceArea() { return SurfaceArea::getValue(_a); } //LINE-3
};

These are Nptel Programming in Modern C++ Assignment 5 Answers


Question 2
Consider the following program. Fill in the blanks as per the instructions given below.
• at LINE-1 with appropriate forward declaration,
• at LINE-2 with appropriate statement
such that it will satisfy the given test cases

Solution:

#include <iostream>
using namespace std;
class Vehicle{
    string vehicleName;
    int noOfWheels;
    protected:
        Vehicle(string s, int w) : vehicleName(s), noOfWheels(w) { }
    public:
        friend void vehicleDetails(const Vehicle&);
};
class Twowheeler : public Vehicle{
    public:
  Twowheeler(string n) : Vehicle(n, 2) { }
};
class Fourwheeler : public Vehicle{
    public:
        Fourwheeler(string n) : Vehicle(n, 4) { }
};

These are Nptel Programming in Modern C++ Assignment 5 Answers


Question 3
Consider the following program. Fill in the blanks at LINE-1, LINE-2 and LINE-3 with appropriate statements such that it will satisfy the given test cases.

Solution:

class D : public B1, public B2 {
    int d;
    public:
        D(int x) : B1(x+5), B2(x+10), d(x) {}

These are Nptel Programming in Modern C++ Assignment 5 Answers

More Weeks of Programming in Modern C++: Click here

More Nptel Courses: Click here


Session: JULY-DEC 2022

Course Name: Programming in Modern C++

Course Link: Click Here

These are Nptel Programming in Modern C++ Assignment 5 Answers


Quiz

Q1. Consider the following code segment.
What will be the output /error?

a) 6.75
b) 0
c) 675
d) Compilation error: no matching function for call to ’FDInterest::calculate()’

Answer: d) Compilation error: no matching function for call to ’FDInterest::calculate()’


Q2. Consider the following code segment.
What will be the output?

a) C Programming
C++ Programming
b) C++ Programming
C Programming
c) C Programming
C Programming
d) C++ Programming
C++ Programming

Answer: c) C Programming
C Programming


These are Nptel Programming in Modern C++ Assignment 5 Answers


Q3. Consider the following code segment.
What will be the output?

a) 1 2 3 -3 -2 -1
b) 1 1 2 3 -3 -1 -1
c) 1 3 -3 -1
d) 1 1 2 3 -3 -2 -1 -1

Answer: d) 1 1 2 3 -3 -2 -1 -1


Q4. Consider the following code segment.
Fill in the blank at LINE-1 so that the program will print Base.

a) Base::print();
b) Base.print();
c) (new Base)->print();
d) Base->print();

Answer: a, c


These are Nptel Programming in Modern C++ Assignment 5 Answers


Q5. Consider the following code segment.
What will be the output /error?

a) 5 10
b) 0 10
c) Compilation error generated from LINE-1
d) Compilation error generated from LINE-2

Answer: d) Compilation error generated from LINE-2


Q6. Consider the following code segment.
What will be the output?

a) A::2 B::2 C::2 A::3 B::3 C::3 A::1 B::1 C::1 ~C ~B ~A ~C ~B ~A ~C ~B ~A
b) A::2 B::2 C::2 A::3 B::3 C::3 ~C ~B ~A A::1 B::1 C::1 ~C ~B ~A ~C ~B ~A
c) C::2 B::2 A::2 C::3 B::3 A::3 C::1 B::1 A::1 ~C ~B ~A ~A ~B ~A ~C ~B ~A
d) C::2 B::2 A::2 C::3 B::3 A::3 C::1 B::1 A::1 ~A ~B ~C ~A ~B ~C ~A ~B ~C

Answer: b) A::2 B::2 C::2 A::3 B::3 C::3 ~C ~B ~A A::1 B::1 C::1 ~C ~B ~A ~C ~B ~A


These are Nptel Programming in Modern C++ Assignment 5 Answers


Q7. Consider the following code segment.
Fill in the blank at LINE-1 so that the program will print Base::£().

a) Base.obj.f()
b) Base.obj::f()
c) obj.Base::f()
d) Base::0bj.f()

Answer: c) obj.Base::f()


Q8. Consider the following code segment.
What will be the output /error?

a) Partha CSE
b) unknown CSE
c) Compilation error at LINE-1: void ’Staff::print1()’ is inaccessible in this context
d) Compilation error at LINE-2: void ’Staff::print2()’ is inaccessible in this context

Answer: c) Compilation error at LINE-1: void ’Staff::print1()’ is inaccessible in this context


These are Nptel Programming in Modern C++ Assignment 5 Answers


Q9. Consider the following code segment.
Fill in the blank at LINE-1 such that the program will print 10 20.

a) A2(_t1, _t2)
b) A2(_t2, _t1)
c) AL(_t1), A2(_t2)
d) A2(_t1), A2(_t1, _t2)

Answer: a) A2(_t1, _t2)


Programming Assignment of Programming in Modern C++

Question 1

Complete the program with the following instructions.
• Fill in the blank at LINE-1 with appropriate initializer list,
• Fill in the blanks at LINE-2 and LINE-3 to complete the return statements.
The program must satisfy the given test cases.

Solution:

class Sphere:public Volume,public Area{
  public:
  Sphere(int _r):Volume(_r), Area(_r){}
  double getVolume(){
    return Volume::getVal();
  }
  double getArea(){
    return Area::getVal();
  }
};

These are Nptel Programming in Modern C++ Assignment 5 Answers


Question 2

Consider the following program with the following instructions.
• Fill in the blank at LINE-1 with appropriate keyword.
• Fill in the blanks at LINE-2 and LINE-3 to complete the constructor statements.
The program must satisfy the sample input and output.

Solution:

#include <iostream>
using namespace std;
class Vehicle
{
  public:
  string vehicleName;
  int noOfWheels;
  Vehicle(string s,int w):vehicleName(s),noOfWheels(w)
  {}
  void vehicleDetails(const Vehicle&);
};
class Twowheeler:public Vehicle
{
  public:
  Twowheeler(string n):Vehicle(n,2)
  {}
};
class Fourwheeler:public Vehicle
{
  public:
  Fourwheeler(string n):Vehicle(n, 4)
  {}
};

These are Nptel Programming in Modern C++ Assignment 5 Answers


Question 3

Consider the following program. Fill in the blanks as per the instructions given below:
• Fill in the blank at LINE-1 to complete the inheritance statement.
• Fill in the blank at LINE-2 with the appropriate initializer list,
The program must satisfy the given test cases.

Solution:

class ReTest:public Test1,public Test2
{
  int t;
  public:
  ReTest(int x):Test1(x+5),Test2(x+10),t(x)
  {}
  void show()
  {
    cout << t << ", " << t1 << ", " << t2;
  }
};

These are Nptel Programming in Modern C++ Assignment 5 Answers



These are Nptel Programming in Modern C++ Assignment 5 Answers

More Solutions of Programming in Modern C++: Click Here

More NPTEL Solutions: https://progiez.com/nptel-assignment-answers/


These are Nptel Programming in Modern C++ Assignment 5 Answers
The content uploaded on this website is for reference purposes only. Please do it yourself first.