Programming in Modern C++ | Week 10

Course Name: Programming in Modern C++

Course Link: Click Here

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


Quiz

Q1. Consider the program given below.
Choose the appropriate option to fill in the blanks at LINE-1 and LINE-2 so that the output becomes
5.25 7 7.35
a) LINE-1 : namespace ver1_0
LINE-2 : namespace ver2_0
b) LINE-1 : namespace ver1_0
LINE-2 : inline namespace ver2_0
c) LINE-1 : inline namespace ver1_0
LINE-2 : namespace ver2_0
d) LINE-1 : inline namespace ver1_0
LINE-2 : inline namespace ver2_0

Answer: a) LINE-1 : namespace ver1_0
LINE-2 : namespace ver2_0


Q2. Consider the program given below.
What will be the output/error?

a) Compiler error at LINE-1 : auto connot deduce to cv-qualifier
b) Compiler error at LINE-2 : read-only x2 cannot be modified
c) 11 11 11 11
d) 11 11 10 10

Answer: d) 11 11 10 10


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


Q3. Choose the appropriate option/s to fill in the blank at LINE-1 such that the output is 20, 30, 40, 50,
a) auto i
b) decltype (j) i
c) decltype ((j)) i
d) decltype (iv[j]) i

Answer: c, d


Q4. Consider the following code segment.
Choose the appropriate option/s to fill in the blank at LINE-1 such that the output is 10 20.

a) auto caller (T& rf)
b) auto caller (T& rf) -> decltype (rf())
c) int& caller (T& rf)
d) decltype (auto) caller ( T& rf )

Answer: b, c


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


Q5. Consider the following code segment.
Identify the function call/s that will compile without generating any error.

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

Answer: b, c, d


Q6. Consider the following code segment.
Choose the appropriate option to fill in the blank at LINE-1 such that the output is 5011M.

a) 5.0KM, 11.0M
b) 5.0 KM, 11.0_M
c) (KM)5.0, (M)11.0
d) 5_KM, 11_M

Answer: b) 5.0 KM, 11.0_M


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


Q7. Consider the following program.
What will be the output?

a) #1 #4 #2 #5 #3 #6
b) #1 #4 #2 #5 #2 #6
c) #1 #4 #2 #5 #3 #5
d) #1 #4 #2 #6 #2 #6

Answer: a) #1 #4 #2 #5 #3 #6


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

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

Answer: d) #1 #2 #2 #2 #2 #3


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


Q9. Consider the following code segment.
Choose the call/s to wrapper function that will result in compiler error/s.

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

Answer: b, c


Programming Assignment of Programming in Modern C++

Question 1

Consider the following program (in C++11).
• Fill in the blank at LINE-1 with appropriate header for overloading copy assignment operator.
• Fill in the blank at LINE-2 with appropriate header and initialization list for move constructor.
• Fill in the blank at LINE-3 with appropriate header for overloading move assignment operator.

Solution:

#include <iostream>
#include <vector>

class item {
public:
    item() : cp_(nullptr) {  }
    item(char c) : cp_(new char(c)) { }
    item& operator=(const item& ob) {
        if (this != &ob) {
            delete cp_;
            cp_ = new char(*(ob.cp_) + 1);
        }
        return *this;
    }
    item(item&& ob) : cp_(nullptr) {
        std::swap(cp_, ob.cp_);
    }
    item& operator=(item&& ob) {
        if (this != &ob) {
            delete cp_;
            cp_ = ob.cp_;
            ob.cp_ = nullptr;
        }
        return *this;
    }

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


Question 2

Consider the following program (in C++11). Fill in the blanks as per the instructions given below:
• Fill in the blanks at LINE-1 and LINE-2 with appropriate headers for the definition of function getValue() belongs to class Gram and class KiloGram.
• Fill in the blank at LINE-3 with appropriate template definition.
• Fill in the blank at LINE-4 with appropriate header for function convert_weight.

Solution:

KiloGram Gram::getValue(){
    return KiloGram(w_ /1000);
}
Gram KiloGram::getValue(){
     return Gram(w_ * 1000);
}
KiloGram convert_weight (Gram w){
     return w.getValue();
}
Gram convert_weight (KiloGram w){
     return w.getValue();
}

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


Question 3

Consider the following program (in C++11). Fill in the blanks as per the instructions given below:
• Fill in the blank at LINE-1 with an appropriate template definition.
• Fill in the blank at LINE-2 to complete the header for function inner_product.
• Fill in the blank at LINE-3 to define the new type Tmp.
The program must satisfy the sample input and output.

Solution:

#include <iostream>
#include <vector>

template<typename T1, typename T2>
void inner_product(const std::vector<T1>& v1, const std::vector<T2>& v2) {

    typedef typename std::common_type<T1, T2>::type Tmp;

    Tmp sum = 0;

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



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

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

More NPTEL Solutions: https://progiez.com/nptel/


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