Programming in Modern C++ | Week 8

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


Quiz

Q1. Consider the following code segment.
What will be the output/error?
a) all
b) int
c) double
d) Compiler error LINE-1: ‘…’ handler must be the last handler for its try block

Answer: d) Compiler error LINE-1: ‘…’ handler must be the last handler for its try block


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

a) float int char *
b) int float
c) all int char *
d) all

Answer: c) all int char *


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

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


Q3. Consider the program given below.
Identify the option/s to fill in the blank at LINE-1 such that output IS NOT DBErrors::KeyException.

a) DBErrors:: DBCon::print_err (-10)
b) DBErrors:: DBCon::print_err (0)
c) DBErrors::DBCon::print_err (7)
d) DBErrors:: DBCon::print_err (20)

Answer: d) DBErrors:: DBCon::print_err (20)


Q4. Consider the program given below.
What will be the output?

a) SQLException
b) KeyException
c) PrimaryKeyException
(d) ForeignKeyException

Answer: c) PrimaryKeyException


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

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


Q5. Consider the code segment given below.
Fill in the blank at LINE-1 such that the output of the program is:
a = 97, b = A
a = a, b = A

a)
b)
c)
d)

Answer: b)


Q6. Consider the code segment given below.
Identify the correct statement(s) such that the above program executes successfully.

a) sum(10, 20)
b) sum(10.3, 20.3f)
c) sum (10.3, 20)
d) sum(10, int(‘A’))

Answer: a), d)


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

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


Q7. Consider the following code segment.
What will be the output / error?

a) 333
b) QQQ
c) Compiler error at LINE-1: non-type argument is not allowed
d) Compiler error at LINE-2: non-type argument must be constant

Answer: d) Compiler error at LINE-2: non-type argument must be constant


Q8. Consider the code segment given below.
Identify the appropriate option to fill in the blank at LINE-1 such that the program gives output as
6.9, 9, 15 + 125

a) complex add(complex x, complex y)
b) template<complex> complex add(complex x, complex y)
c) template<> complex add<complex>(complex x, complex y)
d) template<T> add<complex>(complex x, complex y)

Answer: c) template<> complex add<complex>(complex x, complex y)


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

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


Q9. Consider the following code segment.
Identify the appropriate option to fill in the blank at LINE-1 such that the output becomes
sum = 10, mean = 2.5

a) operator (int arr[], int 1, int* s)
b) operator (int arr[], int 1, int& s) ()
c) operator() (int arr[], int 1, int& s)
d) operator()(int arr[], int 1, int* s)

Answer: c) operator() (int arr[], int 1, int& s)


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

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


Programming

Question 1

Consider the program below.
• Fill in the blank at LINE-1 to specify the fourth argument of the function call, which accepts correct type of function pointer to each member function of class Computation.
• Fill in the blank at LINE-2 with appropriate return statement.
The program must satisfy the given test cases.

Solution:

int call(Computation *obj, int x, int y, int(Computation::*fp)(int, int)){    //LINE-1
 
    return  (obj->*fp)(x,y);    //LINE-2
}

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

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


Question 2

Consider the following program that computes the mean of the squares of a number of integers. Fill in the blanks as per the instructions given below to complete the definition of functor max with local state:
• Fill in the blank at LINE-1 with constructor header.
• At LINE-2 overload the function call operator.
The program must satisfy the given test cases. Marks: 3

Solution:

#include<iostream>
#include<algorithm>
#include<vector>
 
struct max {    
    max(int cnt=0, int ss=0) : cnt_(cnt), ss_(ss)  {}    //LINE-1
 
    void operator() (int x ) { ss_ += x*x; ++cnt_; }    //LINE-2
 
    int cnt_;    //count of element
    int ss_;     //sum of square of emements
};

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

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


Question 3

Consider the following program, which define a type plist that stores N elements. Fill in the blanks as per the instructions given below:
• Fill in the blank at LINE-1 with appropriate template definition,
• At LINE-2 implement void insert(int i, T val) function, which inserts a value at specified index; however, if the input index is N, it throws OutOfArray exception,
• At LINE-3 implement void T peek(int i) function, which returns value of a speci- fied index. However, if the element at the specified position is negative then it thorws InvalidElement exception.
The program must satisfy the given test cases.

Solution:

template<typename T, int N>    //LINE-1
 
class plist{
    public:
        plist(){
            for(int i = 0; i < N; i++)
                arr_[i] = -1;
        }
        //LINE-2: impelement insert() function
        void insert(int i, T val)
        {
          if ( i >= N )
            throw OutOfArray();
          else 
            arr_[i] = val;
        }
          
        //LINE-3: impelement peek() function
        T peek(int i )
        {
          if ( arr_[i] < 0 )
            throw InvalidElement();
          else 
            return arr_[i];
        }
        
    private:
        T arr_[N];
};

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

These are Nptel Programming in Modern C++ Assignment 8 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 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: https://progiez.com/nptel-assignment-answers/


These are Nptel Programming in Modern C++ Assignment 8 Answers
The content uploaded on this website is for reference purposes only. Please do it yourself first.