Programming in Modern C++ | Week 3

Course Name: Programming in Modern C++

Course Link: Click Here

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


Quiz


Q1. Consider the following program.
Which line/s will generate an error?
a) LINE-1
b) LINE-2
c) LINE-3
d) LINE-4

Answer: a) LINE-1


Q2. Consider the following class.
Fill in the blanks with proper access specifiers so that member y can be accessed from outside of the class but member x cannot be accessed.

a) public, public
b) public, private
c) private, public
d) private, private

Answer: c) private, public


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


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

a) 1st
b) 2nd
c) 1st 2nd
d) Compilation Error : call of overloaded ‘myClass()’ is ambiguous

Answer: d) Compilation Error : call of overloaded ‘myClass()’ is ambiguous


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

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

Answer: c) 2 3


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


Q5. Consider the following code segment.
Fill in the blank at LINE-1 so that the program will print fun is coding.

a) *lstr, *mstr, *fstr
b) *mstr, *fstr, *lstr
c) *lstr, *fstr, *mstr
d) *fstr, *lstr, *mstr

Answer: a) *lstr, *mstr, *fstr


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

a) 27
b) 00
c) Compilation error : no default constructor
d) Compilation error : constructor is private

Answer: d) Compilation error : constructor is private


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


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

a) 10 20
b) 20 50
c) Compilation Error : 1value required as left operand of assignment
d) Compilation Error : private data members are inaccessible

Answer: c) Compilation Error : 1value required as left operand of assignment


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


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

a) 1st
b) 2nd
c) 1st 2nd
d) Compilation error: conversion from ‘int’ to ‘Test’ is ambiguous

Answer: d) Compilation error: conversion from ‘int’ to ‘Test’ is ambiguous


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


Q9. Consider the following code segment.
Fill in the blank at LINE-1 such that the program will print 5

a) int
b) const int
c) mutable int
d) int mutable

Answer: c, d


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


Assignment Questions


Question 1

Consider the program below. Fill in the blanks at LINE-1, LINE-2, and LINE-3 with appropriate keywords such that the program must satisfy the given test cases.

Solution:

#include<iostream>
using namespace std;
class Employee{
    const int id;
    string name;
    mutable  int salary; //LINE-1
    public:
        Employee(int a, string b, int c) : id(a), name(b), salary(c) {}
        void updateSalary(int x) const { salary += x; } //LINE-2
        void print() const{    //LINE-3

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


Question 2

Consider the following program.
  • Fill in the blank at LINE-1 with the appropriate initializer statement for the parameterized constructor,
  • Fill in the blank at LINE-2 with the appropriate statement which deletes the dynamically allocated memory to data member arr
The program must satisfy the sample input and output.

Solution:

#include<iostream>
using namespace std;
class Array{
    char *arr;
    int size;
    public:
        Array(int n) : size(n), arr(new char[n]){} //LINE-1
        ~Array(){ delete [] arr; } //LINE-2

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


Question 3

Consider the program below.
  • Fill in the blank at LINE-1 to complete parameterized constructor
  • Fill in the blank at LINE-2 to complete copy constructor
  • Fill in the blank at LINE-3 to complete return statement
The area( ) evaluates the area of the given right-angled triangle.
The program must satisfy the given test cases. 

Solution:

#include <iostream>
using namespace std;
class triangle {
    const int *_base, *_height;
public:
    triangle(int b, int h) : _base(new int(b)), _height(new int(h)) { } // Line-1
    // LINE-1: Complete Constructor definition
    ~triangle() {
       delete _base,_height; // Line-2
        // LINE-2: Complete destructor to delete both data pointers
    }
    double area();
};
double triangle::area() {  // LINE-3: Complete function header
    return 0.5 * *_base * *_height;
}

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


* The material and content uploaded on this website are for general information and reference purposes only. Please do it by your own first. COPYING MATERIALS IS STRICTLY PROHIBITED.


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

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

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


More from PROGIEZ

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