Programming in Modern C++ | NPTEL 2023 | Week 5 Assignment Solutions

This set of MCQ(multiple choice questions) focuses on the Programming in Modern C++ NPTEL Week 5 Assignment Solutions.

Programming in C++ is so fundamental that all companies dealing with systems as well as application development (including web, IoT, embedded systems) have a need for the same. These include – Microsoft, Samsung, Xerox, Yahoo, Oracle, Google, IBM, TCS, Infosys, Amazon, Flipkart, etc. This course would help industry developers to be up-to-date with the advances in C++ so that they can remain at the state-of-the-art.

Course layout (Answers link)

Answers COMING SOON! Kindly Wait!

Week 0: Assignment answers
Week 1: Programming in C++ is Fun
Week 2: C++ as Better C.
Week 3: OOP in C++.
Week 4: OOP in C++. 
Week 5: Inheritance.
Week 6: Polymorphism. 
Week 7: Type Casting.
Week 8: Exceptions and Templates.
Week 9: Streams and STL 
Week 10: Modern C++
Week 11: Lambda and Concurrency.
Week 12: Move, Rvalue and STL Containers.

NOTE: You can check your answer immediately by clicking show answer button. This set ofProgramming in Modern C++ NPTEL Week 5 Assignment Solution” contains 09 questions.

Now, start attempting the quiz.

Programming in Modern C++ NPTEL Week 5 Assignment Solutions

Q1.

Answer: d)

Programming in Modern C++ NPTEL Week 5 Assignment Solutions

Q2.

Answer: a), b)

Q3.

Answer: c)

Programming in Modern C++ NPTEL Week 5 Assignment Solutions

Q4.

Answer: a), b)

Programming in Modern C++ NPTEL Week 5 Assignment Solutions

Q5.

Answer: b), c)

Programming in Modern C++ NPTEL Week 5 Assignment Solutions

Q6.

Answer: d)

Q7.

Answer: a), d)

Programming in Modern C++ NPTEL Week 5 Assignment Solutions

Q8.

Answer: c)

Programming in Modern C++ NPTEL Week 5 Assignment Solutions

Q9. Consider the code segment given below.

#include<iostream>
using namespace std;
class B {
         public:
                void print()  {  cout << "B" << " "; }
};
class D: public B {
         public: 
               void print() { cout << "D" << " "; }
};
int main() {  
            B *a1 = new D(); 
            D *b1 = new D(); 
            a1 -> print(); 
            b1-> print(); 
            return 0;
}

What will be the output?

a) B D
b) D B
c) B B
d) D D

Answer: a)

Previous – Programming in Modern C++ NPTEL Week 5 Assignment Solutions

Q1. Consider the below code segment.

#include<iostream>
using namespace std;

class A{
    protected:
          int id;
          static int count;
    public:
          A(){ id = ++count; cout << id << "-" << count << ", "; }
};
class B : public A {
     public:
           B(){ ++count; cout << count << ", "; }
};
class C : public B {
      public:
           C(){ ++count; cout << count << ", "; }
};
int A::count = 0;

int main() {
     C cObj[2];
     return 0;
}

What will be the output?

a) 1, 2, 3
b) 1, 2, 3-3, 1, 2, 3-3,
c) 1, 2, 3-3, 4, 5, 6-6,
d) 1-1, 2, 3, 4-4, 5, 6,

Answer: d)

Programming in Modern C++ NPTEL Week 5 Assignment Solutions

Q2. Consider the following code segment.

#include<iostream>
using namespace std;

class animal{
    public:
         animal(){ cout << "animal called" << ", "; }
         `animal(){ cout << "animal ends" << ", "; }
};

class tiger : public animal{
    public:
         tiger(){ cout << "tiger called" << ", "; }
         `tiger(){ cout << "tiger ends" << ", "; }
};

class cat : public animal{
    public:
         cat(){ cout << "cat called" << ", "; }
         `cat(){ cout << "cat ends" << ", "; }
};

class royal_bengal_tiger : public tiger{
    cat c;
    public:
         royal_bengal_tiger(){ cout << "royal bengal tiger called" << ", "; }
         `royal_bengal_tiger(){ cout << "royal bengal tiger ends" << ", "; }
};

int main(){
     royal_bengal_tiger rbt;
     return 0;
}

What will be the output?

a) animal called, tiger called, animal called, cat called, royal bengal tiger called, royal bengal tiger ends, cat ends, animal ends, tiger ends, animal ends,
b) animal called, tiger called, royal bengal tiger called, animal called, cat called, cat ends, animal ends, royal bengal tiger ends, tiger ends, animal ends,
c) animal called, tiger called, royal bengal tiger called, cat called, cat ends, royal bengal tiger ends, tiger ends, animal ends,
d) animal called, tiger called, royal bengal tiger called, cat called, animal ends, tiger ends, royal bengal tiger ends, cat ends,

Answer: a)

Q3. Consider the following code segment.

#include<iostream>
using namespace std;

class bird{
   public: 
       void fly(){ cout << "bird::fly()"; }
       void fly(int height){ cout << "bird::fly(int)"; }
};

class crow : public bird{
   public:
       void fly(){ cout << "crow::fly()"; }
       void fly(int height){ cout << "crow::fly(int)"; }
};

int main(){
    crow c;
    _ _ _ _ _ _ _ _;    // LINE-1
    return 0;
}

Choose the appropriate option to fill in the blank at LINE-1 such that the output of the code becomes bird::fly().

a) c.fly()
b) bird::fly()
c) c.bird::fly()
d) bird::c.fly()

Answer: c)

Programming in Modern C++ NPTEL Week 5 Assignment Solutions

Q4. Consider the following code segment.

#include<iostream>
using namespace std;

int data = 10;

class base{
    protected:
        int data;
    public:
         base() : data(20) {}
         `base() {}
};

class derived : base{
    protected:
        int data;
    public:
        derived() : data(3) {}
        `derived(){}
        void print() { cout << _ _ _ _ _ _ _; }  //LINE-1
};

int main() {
    derived d;
    d.print();
    return 0;
}

Choose the appropriate option(s) to fill in the blank at LINE-1 such that the output becomes
30 20 10

a) this->data << ” ” << base::data << ” ” << data
b) derived::data << ” ” << base::data << ” ” << data
c) data << ” ” << base::data << ” ” << ::data
d) derived::data << ” ” << base::data << ” ” << ::data

Answer: c), d)

Q5. Consider the class definition given below.

class base{
   public:
       void fun1(int);
       void fun2(char*);
       void fun3();
};

class derived : public base{
   public:
       void fun1();
       void fun1(int);
       void fun3();
       void fun4(char*);
};

Which of the following statement/s is/are true?

a) derived::fun1() and derived::fun1(int) override base::fun1()
b) derived::fun1() overloads and derived::fun1(int) overrides base::fun1(int)
c) derived::fun4(char*) overrides derived::fun2(char*) and derived::fun3() overrides derived::fun3()
d) derived::fun3() overrides base::fun3() and derived::fun1(int) overrides base::fun1(int)

Answer: b), d)

Programming in Modern C++ NPTEL Week 5 Assignment Solutions

Q6. Consider the code segment below.

#include<iostream>
using namespace std;

int gdata = 10;
class gmClass{
   int mdata;
   public:
       gmClass(int mdata_ = 0) : mdata(++mdata_) { ++gdata; }    // LINE-1
       `gmClass(){ madata = 0; gdata = 0; }
       void print(){ cout << "mdata = " << mdata << ", gdata = " << gdata << endl; }
};

void fun(){
    gmClass ob;
    ob.print();
}

int main(){
    gmClass ob;
    fun();
    ob.print();
    return 0;
}

What will be the output?

a) mdata = 9, gdata = 11
mdata = 1, gdata = 12
b) mdata = 9, gdata = 12
mdata = 1, gdata = 0
c) mdata = 1, gdata = 11
mdata = 2, gdata = 12
d) mdata = 1, gdata = 12
mdata = 1, gdata = 0

Answer: d)

Q7. Consider the code segment below.

#include<iostream>
using namespace std;

class Account{
    private:
       int acc_no;
       string acc_type;
       double acc_balance;
    public:
       Account(int acc_no_, string acc_type_, double acc_balance_) : acc_no(acc_no_), acc_type(acc_type_), acc_balance(acc_balance_){}
       void printStatus(){
            cout << acc_no << ":" << acc_type << ":" << acc_balance << ":";
       }
};

class Customer : private Account{
    private:
        int cust_id;
        string cust_name;
    public:
        Customer(int cust_id_, string cust_name_, int acc_no_, string acc_type_, double acc_balance_) : cust_id(cust_id_), cust_name(cust_name_), Account(acc_no_, acc_type_, acc_balance_){}
        void printStatus(){
             Account::printStatus();    //LINE-1
             cout << cust_id << ":" << cust_name;
        }
};

void print(Account* ac) {
     ac->printStatus();
}

int main(){
    Customer* cObj = new Customer(101, "Rajiv", 2002023, "current", 20000.00);
    print(cObj);     //LINE-2
    return 0;
}

What will be the output/error?

a) 2002023:current:20000:
b) 2002023:current:20000:101:Rajiv
c) compiler error at LINE-1: Account is an inaccessible base of Customer
d) compiler error at LINE-2: Account is an inaccessible base of Customer

Answer: d)

Programming in Modern C++ NPTEL Week 5 Assignment Solutions

Q8. Consider the code segment below.

#include<iostream>
using namespace std;

class PCP{
    public:
        PCP(){}
        `PCP(){}
    private:
        PCP(const PCP& obj){}
        PCP& operator=(const PCP&){ return *this; }
};

class IntData : public PCP{
    int i;
    public:
        IntData(){}
        IntData(const int& i_) : i(i_){}
        void print(){ cout << i << " "; }
};

int main(){
    IntData iO1(10);
    IntData iO2(20);
    iO1 = iO2;
    iO1.print();
    iO2.print();
    return 0;
}

What will be the output/error?

a) 20 10
b) 20 20
c) Compiler error: ‘PCP& PCP::operator=(const PCP&)’ is private
d) Compiler error: ‘PCP::PCP(const PCP& obj)’ is private

Answer: c)

Programming in Modern C++ NPTEL Week 5 Assignment Solutions

Q9. Consider the code segment below.

#include<iostream>
using namespace std;

class A{
   public:
       int a;
};

class B : protected A{
   public:
       int b;
};

class C : public B{
   public:
       int c;
       C(int a_, int b_, int c_) {
           a = a_;    //LINE-1
           b = b_;
           c = c_;
       }
};

int main(){
    C cObj(10, 20, 30);
    cout << cObj.a << " ";   //LINE-2
    cout << cObj.b << " ";   //LINE-3
    cout << cObj.c;
    return 0;
}

What will be the output/error(s)?

a) 10 20 30
b) compiler error at LINE-1: A::a is not accessible
c) compiler error at LINE-2: A::a is not accessible
d) compiler error at LINE-3: B::b is not accessible

Answer: c)

Programming in Modern C++ NPTEL Week 5 Assignment Solutions

<< Prev- Programming in Modern C++ Week 4 Assignment Solutions

>> Next- Programming in Modern C++ Week 6 Assignment Solutions


DISCLAIMER: Use these answers only for the reference purpose. Quizermania doesn't claim these answers to be 100% correct. So, make sure you submit your assignments on the basis of your knowledge.

NPTEL answers: Principles of Management

NPTEL answers: Problem solving through programming in C

Programming in Java NPTEL week 1 quiz answers

NPTEL – Python for Data Science assignment solutions

Nptel – Deep Learning assignment solutions

The above question set contains all the correct answers. But in any case, you find any typographical, grammatical or any other error in our site then kindly inform us. Don’t forget to provide the appropriate URL along with error description. So that we can easily correct it.

Thanks in advance.

For discussion about any question, join the below comment section. And get the solution of your query. Also, try to share your thoughts about the topics covered in this particular quiz.