Programming in Modern C++ Week 3 Assignment Answers

Are you looking for the NPTEL Programming in Modern C++ Assignment 2 Answers 2024? You’ve come to the right place! This resource provides comprehensive solutions to all the questions from the Week 2 assignment, helping you navigate through the complexities of modern C++ programming.

Course Link: Click Here


Programming in Modern C++ Week 3 Assignment Answers
Programming in Modern C++ Week 3 Assignment Answers

Programming in Modern C++ Week 3 Assignment Answers (July-Dec 2024)


Q1.Identify the set of all the methods that change the state of the class Rectangle objects.
a) setWidth(), setHeight(), display()
b) setWidth(), setHeight(), doubleSize(), increase Height()
c) setWidth(), setHeight(), doubleSize(), increaseWidth(), increaseHeight()
d) setWidth(), setHeight(), increaseWidth(), increaseHeight()

Answer: b) setWidth(), setHeight(), doubleSize(), increase Height()


Q2.What will be the output?
a) Constructor: 50 Constructor: 20 Constructor: 30 Constructor: 40 Destructor: 40 Destructor: 30 Destructor: 20 Destructor:50
b) Constructor: 20 Constructor: 50 Constructor: 30 Constructor: 40 Destructor: 40 Destructor: 30 Destructor: 50 Destructor:20
c) Constructor: 50 Constructor: 20 Constructor: 40 Constructor: 30 Destructor: 30 Destructor: 40 Destructor: 20 Destructor:50
d) Constructor: 50 Constructor: 20 Constructor: 30 Constructor: 40 Destructor:20 Destructor: 40 Destructor: 30 Destructor: 50

Answer: d) Constructor: 50 Constructor: 20 Constructor: 30 Constructor: 40 Destructor:20 Destructor: 40 Destructor: 30 Destructor: 50


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

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


Q3. Fill in the blanks with proper access specifiers so that member b can be accessed from outside of the class but member a cannot be accessed.
a) public, public
b) private, public
c) public, private
d) private, private

Answer:b) private, public


Q4.What will be the output?
a) memberVar memberVar 6, globalVar = 7 1, globalVar = 8
b) memberVar 1, globalVar = 7 memberVar = 1, globalVar = 0
c) memberVar 1, globalVar = 8 memberVar = 1, globalVar = 0
d) memberVar memberVar 1, globalVar = 7 2, globalVar = 0

Answer: b) memberVar 1, globalVar = 7 memberVar = 1, globalVar = 0


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

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


Q5.What will be the output?
a) 5 6
b) 75
c) 6 7
d) 78

Answer: c) 6 7


Q6. What will be the output?
a) x = 8, y= 6, z = 6
b) x 6, y= 6, z = 8
c) x = 8, y = 7, z = 6
d) x = 6, y= 7, z = 8

Answer: b) x 6, y= 6, z = 8


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

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


Q7. What will be the output/error?
a) 101, Sedan
b) 202, SUV
c) Compiler error at LINE-1: Car:: Car() cannot be private
d) Compiler error at LINE-2: lvalue required as left operand of assignment

Answer: b) 202, SUV


Q8. What will be the output/error?
a) ctor c-ctor ctor c-ctor
b) ctor c-ctor ctor c-assign
c) ctor c-ctor ctor c-ctor c-assign
d) ctor c-assign ctor c-assign

Answer: c) ctor c-ctor ctor c-ctor c-assign


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

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


Q9.What will be the output/erroг?
a) First
b) Second
c) First Second
d) Compilation error: conversion from ‘int’ to ‘Demo’ is ambiguous

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


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

For answers to additional Nptel courses, please refer to this link: NPTEL Assignment Answers

Programming in Modern C++ Week 3 Assignment Answers (Jan-Apr 2024)

Course Name: Programming in Modern C++

See also  Programming in Modern C++ Week 7 Assignment Answers

Course Link: Click Here

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

These are Programming in Modern C++ Week 3 Assignment 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 Programming in Modern C++ Week 3 Assignment 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 Programming in Modern C++ Week 3 Assignment 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 Programming in Modern C++ Week 3 Assignment Answers


Coding Answers

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 Programming in Modern C++ Week 3 Assignment 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.

See also  Programming in Modern C++ | Week 2

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 Programming in Modern C++ Week 3 Assignment 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 Programming in Modern C++ Week 3 Assignment Answers

More Nptel Courses: Click here


Programming in Modern C++ Week 3 Assignment Answers (July-Dec 2023)

Course Name: Programming in Modern C++

Course Link: Click Here

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


Coding Answers

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 Programming in Modern C++ Week 3 Assignment 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 Programming in Modern C++ Week 3 Assignment 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 Programming in Modern C++ Week 3 Assignment Answers

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

More Nptel Courses: Click here


Programming in Modern C++ Week 3 Assignment Answers (July-Dec 2022)

Course Name: Programming in Modern C++

Course Link: Click Here

These are Programming in Modern C++ Week 3 Assignment 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 Programming in Modern C++ Week 3 Assignment 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

See also  All About NPTEL

Answer: c) 2 3


These are Programming in Modern C++ Week 3 Assignment 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 Programming in Modern C++ Week 3 Assignment 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 Programming in Modern C++ Week 3 Assignment 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 Programming in Modern C++ Week 3 Assignment 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 Programming in Modern C++ Week 3 Assignment Answers


Coding Answers


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 Programming in Modern C++ Week 3 Assignment 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 Programming in Modern C++ Week 3 Assignment 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 Programming in Modern C++ Week 3 Assignment Answers