Programming in Modern C++ | Week 8
Session: JULY-DEC 2023
Course Name: Programming in Modern C++
Course Link: Click Here
These are Nptel Programming in Modern C++ Assignment 8 Answers
Programming
Question 1
Consider the following program. Fill in the blanks as per the instructions given below:
• Fill in the blank at LINE-1 and LINE-2 with appropriate statements for class template specialization.
• Fill in the blank at LINE-3 with appropriate initializer list.
The program must satisfy the given test cases.
Solution:
template<>
class Manipulator<const char*> {
char* val;
public:
Manipulator(const char* _val = 0) : val(strdup(_val)) { }
char* deduct(int d){
char* buf = (char*)malloc(strlen(val) - d + 1);
int i;
for(i = 0; i < strlen(val) - d; i++)
buf[i] = val[i];
buf[i] = '\0';
return buf;
}
};
These are Nptel Programming in Modern C++ Assignment 8 Answers
Question 2
Consider the following program. Fill in the blanks as per the instructions given below.
• Fill in the blank at LINE-1 with appropriate template declaration for class DataSet.
• Fill in the blank at LINE-2 with appropriate declaration of array arr.
• Fill in the blank at LINE-3 with appropriate parameter / parameters for function operator=.
such that it will satisfy the given test cases.
Solution:
#include <iostream>
template <typename T, int N>
class DataSet
{
private:
T arr[N];
int i;
public:
DataSet() : i(-1) { }
void operator=(T data)
{
arr[++i] = data;
}
These are Nptel Programming in Modern C++ Assignment 8 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 constructor for structure Stat.
• Fill in the blank at LINE-2 with appropriate header declaration for functor.
• Fill in the blank at LINE-3 with appropriate return statement.
The program must satisfy the given test cases.
Solution:
#include <iostream>
struct Stat
{
int s;
Stat(int sum) : s(sum) {}
double operator()(int* arr, int n)
{
for(int i = 0; i < n; i++)
s += arr[i];
double a = (double)s / n;
return a;
}
};
These are Nptel Programming in Modern C++ Assignment 8 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 8 Answers
Quiz
Q1. Consider the program given below.
What will be the output?
a) int float char *
b) int float
c) int ALL
d) int ALL char *
Answer: c) int ALL
Q2. Consider the program given below.
Identify the option/s to fill in the blank at LINE-1 such that output IS NOT
PrinterErrors: :PrinterException.
a) PrinterErrors::Printer::print(-1)
b) PrinterErrors::Printer::print(0)
c) PrinterErrors::Printer::print(5)
d) PrinterErrors::Printer: :print(10)
Answer: c) PrinterErrors::Printer::print(5)
These are Nptel Programming in Modern C++ Assignment 8 Answers
Q3. Consider the following program.
What will be the output?
a) NozzleException CartridgeException CartridgeException
b) PrinterException Some other exception
c) PrinterException PrinterException
d) NozzleException Some other exception
Answer: b) PrinterException Some other exception
Q4. Consider the following program.
What will be the output?
a) NozzleException* PrinterException* CartridgeException*
b) PrinterException* PrinterException* PrinterException*
c) NozzleException*
d) 7
Answer: a) NozzleException* PrinterException* CartridgeException*
These are Nptel Programming in Modern C++ Assignment 8 Answers
Q5. Consider the following code segment.
Which of the following call/s to compare at LINE-1 will result in compiler error?
a) compare(’i’, ’k’)
b) compare (31.46, 34.0)
c) compare (31.46, 34)
d) compare(’A’, 34)
Answer: c, d
Q6. Consider the following code segment.
Choose the appropriate option to fill in the blank at LINE-1 so that the output become:
120 X
120 X
a) template < typename T1, typename T2 >
b) template < typename T1, typename T2 = char >
c) template < typename T1 = char, typename T2 = char >
d) template < typename T1 = int, typename T2 = char >
Answer: d) template < typename T1 = int, typename T2 = char >
These are Nptel Programming in Modern C++ Assignment 8 Answers
Q7. Consider the following program.
Choose the correct option to fill in the blanks at LINE-1 and LINE-2 so that the output becomes
7.85 [12, 23].
a) LINE-1: template < class point > LINE-2: class calculator<>
b) LINE-1: template < class point > LINE-2: class calculator
c) LINE-1: template<> LINE-2: class calculator < point >
d) LINE-1: template<> LINE-2: class calculator
Answer: c) LINE-1: template<> LINE-2: class calculator < point >
Q8. Consider the following code segment.
Choose the appropriate option to fill in the blank at LINE-1 and LINE-2 so that the output
becomes
15 16
a) LINE-1: int operator()
LINE-2: int operator(int j)
b) LINE-1: int data()
LINE-2: int data(int j)
c) LINE-1: int operator() ()
LINE-2: int operator()(imt j)
d) LINE-1: void operator() ()
LINE-2: void operator(int j)()
Answer: c) LINE-1: int operator() ()
LINE-2: int operator()(imt j)
These are Nptel Programming in Modern C++ Assignment 8 Answers
Q9. Consider the following code segment.
Choose the appropriate option to fill in the blank at LINE-1 so that the output becomes
30 -10
a) int caller(int& x, int& y, compute* obj, int(compute::fp) (int, int))
b) int caller(int x, int y, compute* obj, int(compute::*fp)(int, int))
c) int caller(int& x, int& y, compute* obj, int(compute::fp)(int, int))
d) int caller(int x, int y, compute* obj, int*(compute::fp) (int, int))
Answer: b) int caller(int x, int y, compute* obj, int(compute::*fp)(int, int))
Programming Assignment of Programming in Modern C++
Question 1
Consider the program below.
• Fill in the blank at LINE-1 with appropriate template definition for function average.
• Fill in the blank at LINE-2 with appropriate header for function average.
• Fill in the blank at LINE-3 with appropriate declaration of total.
• Fill in the blank at LINE-4 with appropriate declaration of size.
The program must satisfy the given test cases.
Solution:
#include <iostream>
template <typename T, typename U, int N>
void average(T arr[N], U &avg) {
T total = 0 ;
for(int i = 0; i < N; i++)
total += arr[i];
avg = (double)total / N;
}
int main(){
int size = 5;
These are Nptel Programming in Modern C++ Assignment 8 Answers
Question 2
Consider the following program.
• Fill in the blank at LINE-1 with an appropriate template definition.
• Fill in the blank at LINE-2 with appropriate initializer list for the constructor.
• Fill in the blank at LINE-3 with appropriate template definition of function print.
• Fill in the blank at LINE-4 to complete the header of function print.
The program must satisfy the sample input and output.
Solution:
#include<iostream>
template <class type, int N>
class FiveVals{
type *base;
public:
FiveVals(type a[]) : base(a) {
for(int i = 0; i < N; i++)
base[i] = a[i];
}
void print();
};
template <class type, int N>
void FiveVals<type, N>::print(){
These are Nptel Programming in Modern C++ Assignment 8 Answers
Question 3
Consider the following program, in which add function has a generic version along with a specialized version for complex type.
• Fill in the blank at LINE-1 to define the template for the generic version of add function.
• Fill in the blank at LINE-2 to define the template for the specialize version of add function.
• Fill in the blank at LINE-3 with the header of the specialize version of add function.
The program must satisfy the sample input and output.
Solution:
template<typename T>
T add(T n1, T n2){
return n1 + n2;
}
template<>
complex add<complex>(complex n1, complex n2) {
These are Nptel Programming in Modern C++ Assignment 8 Answers
More Solutions of Programming in Modern C++: Click Here
More NPTEL Solutions: http://13.235.254.184/nptel/
