Programming in Modern C++ | Week 10

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


Quiz

Q1. Consider the code segment (in C++11) given below.
Identify the appropriate option/s to fill in the blank at LINE-1 such that output becomes 10 10 11
a) auto
b) auto&
c) decltype(rn)
d) decltype((n))

Answer: a) auto


Q2. Consider the code segment (in C++11) given below.
Identify the appropriate option(s) to fill in the blank at LINE-1 such that the output of the program is NOT: 2 4 6 8 10

a) auto i
b) auto& i
c) decltype(*arr) i
d) decltype(arr[0]) i

Answer: a) auto i


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

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


Q3. Consider the following code segment.
Which of the following line/s generate/s compiler error?

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

Answer: b), c)


Q4. Consider the program (in C++11) given below.
Choose the appropriate option to fill in the blanks at LINE-1, LINE-2, and LINE-3 so that the program runs without any compilation error and produces the output: 314 314 346.185

a) LINE-1: circle
LINE-2: circle
LINE-3: circle
b) LINE-1: v1_0::circle
LINE-2: v2_0:: circle
LINE-3: circle
c) LINE-1: v1_0:: circle
LINE-2: circle
LINE-3: circle
d) LINE-1: v2_0:: circle
LINE-2: v2_0:: circle
LINE-3: v2_0:: circle

Answer: b), c)


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

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


Q5. Consider the following code segment (in C++11).
Choose the appropriate option to fill in the blank at LINE-1 such that the output is 125IN.

a) 5.0FT, 10.0IN
b) (FT)5, (IN) 10
c) 5.0 FT, 10.0_IN
d) 5 FT, 11_IN

Answer: c) 5.0 FT, 10.0_IN


Q6. Consider the following program (in C++11).
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: b) #1 #4 #2 #5 #2 #6


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

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


Q7. Consider the code segment (C++11) given below.
What will be the output?

a) cont-3 cont-3 cont-3 cont-3 cont-3
b) cont-2 cont-2 cont-3 cont-2 cont-4
c) cont-2 cont-3 cont-3 cont-3 cont-4
d) cont-2 cont-2 cont-3 cont-3 cont-3

Answer: c) cont-2 cont-3 cont-3 cont-3 cont-4


Q8. Consider the following code segment.
Which of the following lines generate/s compiler error?

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

Answer: b), c)


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

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


Q9. Consider the code segment (in C++14) given below.
Identify the appropriate option/s to fill in the blank at LINE-1 such that output becomes lval 20 rval 20.

a) decltype (op()) wrapper (T& op )
b) auto wrapper (T& op) -> decltype (op())
c) auto wrapper (T& op)
d) decltype(auto) wrapper (T& op)

See also  Programming in Modern C++ | Week 7

Answer: b), d)


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

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


Programming

Question 1

Consider the following program (in C++14).
• Fill in the blank at LINE-1 with an appropriate template definition.
• Fill in the blank at LINE-2 with an appropriate header for function divide.
The program must satisfy the sample input and output.

Solution:

#include <iostream>
 
int convert(double d){
    return int(d);
}
double convert(int i){
    return double(i);
}
 
template<typename T, typename U>
 
auto divide(T n1, U n2) -> decltype(convert(n1) / convert(n2)) {
    return convert(n1) / convert(n2);
}

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

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


Question 2

Consider the following program (in C++11).
• Fill in the blank at LINE-1 with appropriate header and initialization list for copy con structor.
• Fill in the blank at LINE-2 with appropriate header for overloading copy assignment operator.
• Fill in the blank at LINE-3 with appropriate header and initialization list for move constructor.
• Fill in the blank at LINE-4 with appropriate header for overloading move assignment operator.
The program must satisfy the sample input and output.

Solution:

#include <iostream>
 
class ctype { 
    public: 
        ctype() : cp_(nullptr) {  }
        ctype(char c) : cp_(new char(c)) { }
        ctype(const ctype& ob) : cp_(new char(*(ob.cp_))) {}
        ctype& operator=(const ctype& ob) {
            if (this != &ob) { 
                delete cp_;  
                cp_ = new char(*(ob.cp_) + 1); 
            } 
            return *this;
        }
        ctype(ctype&& ob) noexcept : cp_(ob.cp_) {
            ob.cp_ = nullptr; 
        }  
        ctype& operator=(ctype&& ob) noexcept {
            if (this != &ob) { 
                cp_ = ob.cp_; 
                ob.cp_ = nullptr; 
            } 
            return *this;
        }

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

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 add.
• Fill in the blank at LINE-3 to define the new type Tmp.
The program must satisfy the sample input and output.

Solution:

template<typename T, typename U>
 
decltype(auto) add(T n1, U n2) {
 
    typedef decltype(n1 + n2) Tmp;
    Tmp sum = n1 + n2;
    
    std::cout << sum << std::endl;
}

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

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


Programming

Question 1
Consider the following program in C++11/14 to convert between feet and inch. Fill in the blanks as per the instructions given below:
   • at LINE-1 with appropriate header to function convert,
   • at LINE-2 with appropriate return statement convert,
such that it will satisfy the given test cases.

See also  Programming in Modern C++ | Week 6

Solution:

template <typename T>

decltype(auto) convert(T val){   // LINE-1

    return val.getValue();;      // LINE-2
}

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


Question 2
Consider the following program in C++11/14. Fill in the blanks as per the instructions given below:
   • at LINE-1 with appropriate header and initialization list for the copy constructor,
   • at LINE-2 with appropriate header for copy assignment operator overload,
   • at LINE-3 with appropriate header and initialization list for the move constructor,
   • at LINE-4 with appropriate header for move assignment operator overload,
such that it will satisfy the given test cases.

Solution:

#include <iostream>
#include <vector>
class number { 
    public:
        number(){}
        number(int i) : ip_(new int(i)) { } 
        number(const number& n) : ip_(new int(*(n.ip_) * 10)) { }
        number& operator=(const number& n) {
            if (this != &n) { 
                delete ip_;  
                ip_ = new int(*(n.ip_) * 10); 
            } 
            return *this;
        }
        ~number() { delete ip_; } 
        number(number&& n) : ip_(n.ip_) { n.ip_ = nullptr; }
        number& operator=(number&& d) {
            if (this != &d) { 
                ip_ = d.ip_; 
                d.ip_ = nullptr; 
            } 
            return *this;
        }

These are Nptel Programming in Modern C++ Assignment 10 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 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: b) LINE-1 : namespace ver1_0
LINE-2 : inline 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, d


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: a) LINE-1


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

See also  Programming in Modern C++ | Week 4

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: b) #1 #4 #2 #5 #2 #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

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