Programming in Modern C++ | Week 3

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


Quiz

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

Answer: a) LINE-1


Q2. Consider the following program.
What will be the output /error?

a) Default
b) Parameterized
c) Default
Parameterized
d) Compilation error: call of overload ‘Check()’ is ambiguous

Answer: d) Compilation error: call of overload ‘Check()’ is ambiguous


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

a) 1 9
b) 9 1
c) Compilation error: constructor is private
d) Compilation error: no default constructor

Answer: c) Compilation error: constructor is private


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

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


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

a) a, c
b) o, k
c) Compilation Error: private data members are inaccessible
d) Compilation Error: lvalue required as left operand of assignment

Answer: d) Compilation Error: lvalue required as left operand of assignment


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

a) 2 13 4 1 4 3 2
b) 2 1 3 4 4 3 1 2
с) 1 2 3 4 1 4 3 2
d) 1 2 3 4 4 3 2 1

Answer: a) 2 13 4 1 4 3 2


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

a) 1, 11
0, 10
b) 1, 11
0, 12
c) 1, 12
1, 11
d) 1, 12
0,11

Answer: c) 1, 12
1, 11


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

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


Q7. Consider the following code segment.
Fill in the blank at LINE-1 such that the output is 10 : Ram : 86.

a) marks++
b) this->marks++
c) this.marks++
d) this->(++marks)

Answer:


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

a) myClass(double) myClass() myClass(double) myClass(myClass&) 2.3 2.3 1.2
b) myClass(double) myClass(double) myClass(myClass&) 2.3 2.3 1.2
c) myClass(double) myClass() myClass(double) myClass (myClass&) 2.3 2.3 0.0 1.2
d) Compilation error: data members are private

Answer: a), b)
a) myClass(double) myClass() myClass(double) myClass(myClass&) 2.3 2.3 1.2
b) myClass(double) myClass(double) myClass(myClass&) 2.3 2.3 1.2


Q9. Consider the following code segment.
Fill in the blank at LINE-1 such that the output is 012.

а) *у, *z, *x
b) *х, *у, *z
с) *у, *x, *z
d) *z, *x, *у

Answer: с) *у, *x, *z


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

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


Programming

Question 1

Consider the program below.
• Fill in the blank at LINE-1 to complete the parameterized constructor.
• Fill in the blank at LINE-2 to complete the copy constructor.
• Fill in the blank at LINE-3 to complete the appropriate function header.
The program must satisfy the given test cases.

Solution:

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

class Complex_num{
    const int x, y;
public:
    Complex_num(int _x = 0, int _y = 0) : x(_x), y(_y) {}

    Complex_num(const Complex_num& c) : x(c.x), y(c.y) {}

    Complex_num addition(const Complex_num& c) {
        return Complex_num(x + c.x, y + c.y);
    }

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

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 constructor statement.
• Fill in the blank at LINE-2 with the appropriate destructor statement so that the dynamic memory allocated in the constructor can be properly deleted.
The program must satisfy the sample input and output.

Solution:

#include<iostream>
using namespace std;

class CharArray {
    char *arr;
    int size;

public:
    CharArray(int n) : size(n), arr(new char[n]) {}

    ~CharArray() { delete[] arr; }

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

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


Question 3

Consider the following program.
• Fill in the blanks at LINE-1 to declare the data member z.
• Fill in the blanks at LINE-2 to complete the change() function header.
• Fill in the blanks at LINE-3 to complete the print() function header.
The program must satisfy the sample input and output.

Solution:

#include<iostream>
using namespace std;

class Constant {
    int x, y;
    mutable int z;

public:
    Constant(int _x, int _y) : x(_x), y(_y) {}

    void change() const { z = x * y; }

    void print() const {
        cout << "x = " << x << ", y = " << y << ", z = " << z;
    }
};

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

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 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-assignment-answers/


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