Programming in Modern C++ | Week 11
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 11 Answers
Quiz
Q1. Consider the code segment (in C++11) given below.
rom among the following identify the line(s) which use(s) parameter passing with a universal reference.
a) LINE-1
b) LINE-2
c) LINE-3
d) LINE-4
Answer: c) LINE-3
Q2. Consider the program (in C++11) given below.
Choose the appropriate option to fill in the blank at LINE-1 so that the output becomes base1 lvalue: 10, base2 rvalue: 10,
a) basel (n1), base2(n2)
b) base1 (n1), base2(std:: forward(n2))
c) base1 (std::forward(n1)), base2(n2)
d) base1 (std::forward(n1)), base2(std::forward(n2))
Answer: b), d)
For answers or latest updates join our telegram channel: Click here to join
These are Nptel Programming in Modern C++ Assignment 11 Answers
Q3. Consider the following code segment (in C++11).
Identify the statement/s which are true for the above code segment.
a) It generates compilation error at LINE-1
b) It generates compilation error at LINE-2
c) It generates compilation error at LINE-3
d) There is no error in the given code segment
Answer: b, c
Q4. Consider the following code segment (in C++11).
Which of the following line/s generate/s compilation error/s?
a) LINE-1
b) LINE-2
c) LINE-3
d) LINE-4
Answer: b) LINE-2
For answers or latest updates join our telegram channel: Click here to join
These are Nptel Programming in Modern C++ Assignment 11 Answers
Q5. Consider the code segment (C++11) given below.
What will be the output/error?
a) [0, 0], [1011, 0], [2000, 0], [1012, 3000],
b) [0, 1000], [1011, 1000], [0, 2000], [1012, 3000],
c) compiler error at LINE-5: ambiguous call c3 (2000.0)
d) compiler error at LINE-6: ambiguous call c4(1012, 3000.0)
Answer: b) [0, 1000], [1011, 1000], [0, 2000], [1012, 3000],
Q6. Consider the code segment (C++11) given below.
What will be the output/error?
a) [0, unknown, 1011] [0, unknown, 30000]
b) [1011, unknown, 0] [0, unknown, 30000]
c) Compiler error at LINE-1, LINE-2 and LINE-3: C++ does not allow in-class initialization for the data members
d) Compiler error at LINE-4 and LINE-5: ambiguous calls
Answer: b) [1011, unknown, 0] [0, unknown, 30000]
For answers or latest updates join our telegram channel: Click here to join
These are Nptel Programming in Modern C++ Assignment 11 Answers
Q7. Consider the code segment (C++14) given below.
What will be the output?
a) 300, 314.286, 314.286, 300
b) 500, 314.286, 314.286, 500
c) 300, 314.286, 314.286, 500
d) 314.286, 314.286, 314.286, 500
Answer: b) 500, 314.286, 314.286, 500
Q8. Consider the code segment (C++11) given below.
Identify the appropriate functor class implementation in place of the marked code segment (Portion of the code marked by start and end) from the given options, such that the output of the pro- gram remains the same that is the output is 100, 20.
a)
b)
c)
d)
Answer: c)
For answers or latest updates join our telegram channel: Click here to join
These are Nptel Programming in Modern C++ Assignment 11 Answers
Q9. Consider the following code segment (in C++11).
Identify the appropriate option(s) to fill in the blanks at LINE-1 such that the output becomes 10, 20, 30, 40, 100.
a) auto evalute = [] (int m, int& sum)
b) auto evalute = [&vec] (int m, int& sum)
c) auto evalute = [=] (int m, int& sum)
d) auto evalute = [&] (int m, int& sum)
Answer: b), d)
For answers or latest updates join our telegram channel: Click here to join
These are Nptel Programming in Modern C++ Assignment 11 Answers
Programming
Question 1
Consider the following program that implements a recursive lambda function to find out the n, where n, m 2 1.
• Fill in the blank at LINE-1 to declare the signature of recPow as std::function.
• Fill the blank at LINE-2 to complete the definition of lambda function recPow.
• Fill the blank at LINE-3 to complete the definition of lambda expression print, which calls recPow.
The program must satisfy the sample input and output.
Solution:
#include <iostream>
#include <functional>
int main(){
std::function<int(int, int)> recPow;
recPow = [&recPow](int n, int m) -> int {
return m == 1 ? n : n * recPow(n, m - 1);
};
auto print = [&recPow](int n, int m) {
std::cout << recPow(n, m);
};
int n, m;
std::cin >> n >> m;
print(n, m);
return 0;
}
For answers or latest updates join our telegram channel: Click here to join
These are Nptel Programming in Modern C++ Assignment 11 Answers
Question 2
Consider the following program (in C++11).
• Fill in the blanks at LINE-1 and LINE-3 with appropriate template definitions.
• Fill in the blanks at LINE-2 and LINE-4 with appropriate parameters for getmin function.
The program must satisfy the sample input and output.
Solution:
#include <iostream>
template <typename T>
double getmin(T num){ return num; }
template <typename T, typename... Tail>
double getmin(T num, Tail... nums){
return num <= getmin(nums...) ? num : getmin(nums...);
}
For answers or latest updates join our telegram channel: Click here to join
These are Nptel Programming in Modern C++ Assignment 11 Answers
Question 3
Consider the program below (in C++11).
• Fill in the blank at LINE-1 with appropriate template declaration.
• Fill in the blank at LINE-2 with an appropriate parameter for function makePayment.
• Fill in the blank at LINE-3 to complete the return statement.
The program must satisfy the given test cases.
Solution:
#include <iostream>
class payment{
public:
virtual void pay() = 0;
};
class upi_payment : public payment {
public:
upi_payment(const int& upi_num) : upi_num_{upi_num}{
std::cout << "lvalue" << " ";
}
upi_payment(int&& upi_num) : upi_num_{upi_num}{
std::cout << "rvalue" << " ";
}
void pay(){ std::cout << upi_num_ << " "; }
private:
int upi_num_;
};
class bank_transfer : public payment {
public:
bank_transfer(const int& acc_no) : acc_no_(acc_no){
std::cout << "lvalue" << " ";
}
bank_transfer(int&& acc_no) : acc_no_(acc_no){
std::cout << "rvalue" << " ";
}
void pay(){ std::cout << acc_no_ << " "; }
private:
int acc_no_;
};
template <typename T1, typename Arg>
T1 makePayment(Arg&& a){
return T1(std::forward<Arg>(a));;
}
For answers or latest updates join our telegram channel: Click here to join
These are Nptel Programming in Modern C++ Assignment 11 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 11 Answers
Programming
Question 1
Consider the following program (in C++11).
• Fill in the blanks at LINE-1 and LINE-3 with appropriate template definitions.
• Fill in the blanks at LINE-2 and LINE-4 to complete the return statements for product
functions.
The program must satisfy the sample input and output.
Solution:
#include <iostream>
template <typename T>
double product(T num){ return num; }
template <typename T, typename... Tail>
double product(T num, Tail... nums){
return num * product(nums...);
}
These are Nptel Programming in Modern C++ Assignment 11 Answers
Question 2
Consider the program below (in C++11).
• Fill in the blank at LINE-1 with appropriate template declaration.
• Fill in the blanks at LINE-2 with an appropriate universal reference type parameter for constructor of class derived and an the appropriate call forwarding to the base class constructor.
The program must satisfy the given test cases.
Solution:
class derived : public base {
public:
template<typename T>
derived(T&& n) : base(std::forward<T>(n)) { }
void show(){ std::cout << n_ << " "; }
};
These are Nptel Programming in Modern C++ Assignment 11 Answers
Question 3
Consider the following program that implements a recursive lambda function to find the sum of the digits of an input integer.
• Fill in the blank at LINE-1 to declare the signature of revPrint as std::function.
• Fill the blank at LINE-2 to complete the definition of lambda function revPrint.
The program must satisfy the sample input and output.
Solution:
#include<iostream>
#include<functional>
int main() {
std::function<int(int)> revPrint;
revPrint = [&revPrint](int n) -> int {
if (n == 0)
return 0;
return n % 10 + revPrint(n /= 10);
};
These are Nptel Programming in Modern C++ Assignment 11 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 11 Answers
Quiz
Q1. Consider the program (in C++11) given below.
Choose the appropriate option to fill in the blank at LINE-1 so that the output becomes
base – lvalue : 10
base – rvalue : 20
a) LINE-1: a1_(n1), a2_(n2)
b) LINE-1: a1_(std::forward< T1 >(n1)), a2_(std::forward< U >(n2))
c) LINE-1: a1_(n1), a2 (std::forward< U >(n2))
d) LINE-1: a1_(std::forward< T1 >(n1)), a2_(n2)
Answer: b, c
Q2. Consider the code segment (in C++11) given below.
Identify the line/s where && indicates a universal reference.
a) LINE-1
b) LINE-2
c) LINE-3
d) LINE-4
Answer: b
These are Nptel Programming in Modern C++ Assignment 11 Answers
Q3. Consider the following code segment (in C++11).
Identify the statement /s which are true for the given program.
a) It generates compiler error at LINE-1
b) It generates compiler error at LINE-2
c) It generates compiler error at LINE-3
d) It generates the output true
Answer: b, c
Q4. Consider the following code segment.
Identify the appropriate code block to be placed at code-block-1 such that the output is 60
50 40 20 10 .
a) bool compare(int a, int b){ return a > b; }
b) struct compare{
bool operator() (int a, int b){ return a > b; }
}
c) class compare{
bool operator() (int a, int b){ return a > b; }
}
d) bool operator() (int a, int b){ return a > b; }
Answer: b) struct compare{
bool operator() (int a, int b){ return a > b; }
}
These are Nptel Programming in Modern C++ Assignment 11 Answers
Q5. Consider the following code segment (in C++11).
Which of the following line/s generate/s compiler error/s?
a) LINE-1
b) LINE-2
c) LINE-3
d) LINE-4
Answer: a, c
Q6. Consider the following code segment (in C++11).
What will be the output?
a) #1
b) #2 #1
c) #4 #2 #1
d) #1 #2 #4
Answer: c) #4 #2 #1
These are Nptel Programming in Modern C++ Assignment 11 Answers
Q7. Consider the following program (in C++14).
What will be the output?
a) 254, 254
b) 10000, 10000
c) 10000, 254
d) 254, 10000
Answer: c) 10000, 254
Q8. Consider the following A expression (in C++11).
Identify the most appropriate option that define the equivalent closure object for the above lambda function.
a)
b)
c)
d)
Answer: d)
These are Nptel Programming in Modern C++ Assignment 11 Answers
Q9. Consider the following code segment (in C++11).
Which of the following lines will generate compiler error/s?
a) LINE-1
b) LINE-2
c) LINE-3
d) LINE-4
Answer: a
Programming Assignment of Programming in Modern C++
Question 1
Consider the following program (in C++11).
• Fill in the blanks at LINE-1 and LINE-3 with appropriate template definitions.
• Fill in the blanks at LINE-2 and LINE-4 with appropriate parameters for findMax function.
The program must satisfy the sample input and output.
Solution:
#include <iostream>
template<typename T>
double findMax(T num){ return num; }
template<typename T, typename... Args>
double findMax(T num, Args... nums){
return num >= findMax(nums...) ? num : findMax(nums...);
}
These are Nptel Programming in Modern C++ Assignment 11 Answers
Question 2
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 declaration for the function wrapper.
• Fill in the blank at LINE-2 with an appropriate header for function wrapper.
• Fill in the blank at LINE-3 with an appropriate return statement for function wrapper.
The program must satisfy the sample input and output.
Solution:
template<typename T, typename U, typename V>
double wrapper (V&& v, Summation<T, U> s, const std::vector<T>& v1, const std::vector<U>& v2){
return s(v, v1, v2);
}
template<typename T, typename U, typename V>
double wrapper (V&& v, Summation<T, U> s, std::vector<T>&& v1, std::vector<U>&& v2){
return s(v, std::move (v1), std::move (v2));
}
These are Nptel Programming in Modern C++ Assignment 11 Answers
Question 3
Consider the following program (in C++11) to find factorials of 3 integers. Fill in the blanks as per the instructions given below:
• Fill in the blank at LINE-1 to complete the λ function for computing factorial.
• Fill in the blank at LINE-2 to complete the λ function for printing the vector v2.
The program must satisfy the sample input and output.
Solution:
for (auto i : v1)
{
long long factorial = 1;
for (int j = 1; j <= i; j++)
{
factorial *= j;
}
v2.push_back (factorial);
}
for (auto i : v2)
{
std::cout << i << " ";
}
return 0;
}
These are Nptel Programming in Modern C++ Assignment 11 Answers
More Solutions of Programming in Modern C++: Click Here
More NPTEL Solutions: https://progiez.com/nptel-assignment-answers/