Programming in Modern C++ | Week 3

Session: JULY-DEC 2023

Course Name: Programming in Modern C++

Course Link: Click Here

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


Programming

Question 1
Consider the following program. Fill in the blanks as per the instructions given below:
    • Complete two constructor statements at LINE-1 and LINE-2,
    • Complete the return statement at LINE-3 to calculate the manhattan distance between two points,
such that it will satisfy the given test cases.

Solution:

#include<iostream>
#include<cmath>
using namespace std;

class Point{
    const int x,y;

    public:
        Point(int _x=0, int _y=0) : x(_x), y(_y) {} //LINE-1

        Point(const Point& p) : x(p.x), y(p.y) {} //LINE-2

        double distance(Point p){

            return abs(x - p.x) + abs(y - p.y); //LINE-3
        }

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


Question 2
Consider the following program. Fill in the blanks as per the instructions given below.
    • at LINE-1 with constructor definition,
    • at LINE-2 with appropriate statement to deallocate array memory
such that it will satisfy the given test cases.

Solution:

#include<iostream>
using namespace std;

class Array{
    int *arr;
    int size;

    public:
        Array(int n) : size(n), arr(new int[size]) {} //LINE-1

        ~Array(){ delete[] arr; } //LINE-2

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


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

Solution:

#include<iostream>
using namespace std;
class Student{
    const int sid;
    string sname;

    mutable int marks; //LINE-1

    public:
        Student(int a, string b, int c) : sid(a), sname(b), marks(c) {}

        void updateMarks(int x) const { marks += x; } //LINE-2

        void print() const {    //LINE-3

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

More Weeks of Programming in Modern C++: Click here

More Nptel Courses: Click here


Session: JULY-DEC 2022

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



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/


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

This content is uploaded for study, general information, and reference purpose only.