Programming in Modern C++ Week 2 Assignment Answers

Programming in Modern C Nptel Week 2 Assignment Answer and solution Swayam Platform image

Are you looking for the NPTEL Programming in Modern C++ Assignment 2 Answers 2025? 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

image for Programming in Modern C++ Week 2 Assignment Answers
Programming in Modern C++Nptel Week 2 Assignment Answer

Programming in Modern C++ Week 2 Assignment Answers (Jan-Apr 2025)


1) Consider the following function prototypes of function add().

image 20

Which of the following sets consists of all valid prototypes of function add()?

image 21

View Answer


2) Consider the following function prototypes of function add().

image 22

Which of the above pairs of the prototypes is ambiguous and illegal as per the rule of function overloading?

image 23

View Answer


3) Consider the following code segment.

What will be the output/error?

#include <iostream>
using namespace std;

int add(int n1) { return n1; }
int add(int n1, int n2) { return n1 + n2; }
double add(double d1) { return d1; }
int add(int n1, int n2, double n3) { return n1 + n2 + n3; }

int main() {
    int r = add(10, 20);
    cout << r << endl;
    return 0;
}

A) 30
B) 130
C) 300
D) Compilation Error: call of overloaded add(int, int) is ambiguous

View Answer


4) Consider the following code segment.

What will be the output?

#include <iostream>
using namespace std;

#define DOUBLE(x) (2 * x)
inline int TRIPLE(int x) { return 3 * x; }

int main() {
    cout << DOUBLE(10 + 10) << endl;
    cout << TRIPLE(10 + 10) << endl;
    return 0;
}

A) 40 60
B) 30 60
C) 30 40
D) 20 30

View Answer


5) Consider the following code segment.

Choose the correct option to fill in the blank at LINE—I such that the output is 11 11 20 20.

#include <iostream>
using namespace std;

int& incr(int i) {
    return ++i;
}

int main() {
    int x = 10, y = 20;
    int& z = incr(x);
    cout << x << " " << z;
    incr(x);
    cout << x << " " << z;
    return 0;
}

A) int incr(int i)
B) int incr(int& i)
C) int& incr(const int& i)
D) int& incr(int& i)

View Answer


6) Consider the following code segment.

What will be the output?

using namespace std;

#define CL 10 + 1
const int C2 = 10;

int main() {
    int nl, n2;
    nl = CL * 100 * C2;
    n2 = C2 * 100 * CL;
    cout << nl << " " << n2;
    return 0;
}

A) 1110 11001
B) 1011 1011
C) 12100 12100
D) 11001 1110

View Answer


7) Consider the following code segment.

What will be the output?

#include <iostream>
using namespace std;

int main() {
    int i = 21;
    int* ptr = &i;
    cout << *ptr << " " << i;
    return 0;
}

A) 21 32
B) 32 32
C) 21 44
D) 44 44

View Answer


8) Consider the following code segment.

image 24

Identify the correct statement(s) to fill in the blank at LINE—I such that the output is X.

A) const char* cp
B) char* const cp
C) char const* cp
D) const char* const cp

View Answer


9) Consider the following statement in C.

image 25

Among the given options below, identify the equivalent statement/s (perform the same task).

A) int* new int(5);
B) int* new int[5];
C) int* new int t operator new(sizeof(int) * 5);
D) int* (int*) operator new(sizeof(int))[5];

View Answer


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


Q1.Consider the following code segment.

What will be the output/error?
a) 5
b) 10
c) 0
d) Compilation Error at LINE-1: assignment of read only variable ‘p’

Answer: d) Compilation Error at LINE-1: assignment of read only variable ‘p’


Q2.Consider the following code segment.

What will be the output?
a) 3 4
b) 89
c) 84
d) 3 9

Answer: b) 89


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

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


Q3. Consider the following code segment.

Which line/s will give you an error?
a) LINE-1
b) LINE-2
c) LINE-3
d) LINE-4
e) LINE-5

Answer: a) LINE-1
b) LINE-2


Q4.Consider the following code segment.

What will be the output/error?
a) 10 21
b) 10 20
c) Compilation Error: attempt to increment a constant reference
d) Compilation Error: invalid initialization of non-const reference

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

Answer: c) Compilation Error: attempt to increment a constant reference


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

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


Q5. Consider the following code segment

What will be the output?
a) 6 6 4 4
b) 6 6 8 8
c) 3 3 4 4
d) 3 3 8 8

Answer: a) 6 6 4 4


Q6. Consider the following code segment.

Fill in the blank at LINE-1 such that the output is 20.
a) (int)malloc(20sizeof(int))
b) new int
c) new int (20)
d) new int [20]

Answer: c) new int (20)


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

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


Q7. Consider the following code segment.

What will be the output/error?
a) 0
b) 5
c) 2
d) Compilation Error: invalid value in enum

Answer: c) 2


Q8. Consider the following code segment.

What will be the output/error?
a) 9.0
b) -1.0
c) -1
d) Compilation Error at LINE-1: Call of overloaded ‘calculate (int, int)’ is ambiguous

Answer: d) Compilation Error at LINE-1: Call of overloaded ‘calculate (int, int)’ is ambiguous


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

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


Q9. Consider the following code segment.

Fill in the blank at LINE-1 with the correct function header.
a) vector operator* (vector &v1, vector &v2)
b) vector operator(vector v1, vector v2) c) int operator (vector v1, vector v2)
d) void operator*(vector v1, vector v2)

Answer: a) vector operator* (vector &v1, vector &v2)
b) vector operator(vector v1, vector v2) c) int operator (vector v1, vector v2)


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

These are Programming in Modern C++Nptel Week 2 Assignment Answer


Programming Questions

W1_Programming-Qs.1
Consider the program below.
• Fill in the blank at LINE-1 to declare a stack variable st.
• Fill in the blank at LINE-2 to push values into the stack.
• Fill in the blank at LINE-3 with the appropriate statement.
The program must satisfy the given test cases.

Solution:

Update Soon

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

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


W1_Programming-Qs.2
Consider the following program.
• Fill in the blank at LINE-1 with the appropriate if statement,
• Fill in the blank at LINE-2 and LINE-3 with the appropriate return statements.
The program must satisfy the sample input and output.

Solution:

Update Soon

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

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


W1_Programming-Qs.3
Consider the program below.
• Fill in the blank at LINE-1 to include the appropriate header file to utilize the abs () function.
• Fill in the blank at LINE-2 to compute the Manhattan distance between two points pt1 and pt2 as pt1.ypt2.y+pt1.x pt2.r
The program must satisfy the given test cases.

Solution:

Update Soon

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

These are Programming in Modern C++Nptel Week 2 Assignment Answer

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

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


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


Quiz

Q1. Consider the following program.
What will be the output?

a)15
b)14
c)62
d)Compilation error:call of overload “add(int,int)”is ambiguous

Answer: b)14


Q2. Consider the following program.
What will be the output (Consider right to left execution of the cout statement)?

a) 7 4
b) 8 4
c) 4 4
d) 7 6

Answer: a) 7 4


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

a) 6 6 2 2
b) 6 8 4 2
с) 6 6 4 4
d) 8 6 4 4

Answer: с) 6 6 4 4


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

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

See also  Programming in Modern C++ | Week 7

Q4. Consider the following code segment.
Fill in the blank at LINE-1 so that the program will print 1, 300.

a) int x3, int x4
b) int &x3, int* x4
c) int x3, int& x4
d) int x3, int* x4

Answer: d) int x3, int* x4


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

a) 0
b) 10
c) Compilation error at LINE-1: ‘x’ declared as reference but not initialized
d) Compilation error at LINE-2: assignment of read only reference x

Answer: c), d)
c) Compilation error at LINE-1: ‘x’ declared as reference but not initialized
d) Compilation error at LINE-2: assignment of read only reference x


Q6. Consider the following code segment.
Fill in the blank at LINE-1 with appropriate option/s such that the output is: 11

a) const
b) volatile
c) static
d) inline

Answer: a) const


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

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


Q7. Consider the following code segment.
Fill in the blank at LINE-1 so that the program will print a.

a) (char*)malloc (10*sizeof (char))
b) new char(‘a’)
c) new char (97)
d) new char [97]

Answer: b), c)
b) new char(‘a’)
c) new char (97)


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

a) 97
b) 120
c) Error at LINE-1: cannot convert int* to const char* in initialization
d) Error at LINE-2: assignment of read-only location *p

Answer: c), d)
c) Error at LINE-1: cannot convert int* to const char* in initialization
d) Error at LINE-2: assignment of read-only location *p


Q9. Consider the following function prototypes of overloaded function func().
Which functions will be invoked for the call func (2.1, 3.7f)?

а) 2, 3, 5
b) 2, 3
c) 2
d) 1, 2

Answer: b) 2, 3


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

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


Programming

Question 1

Consider the program below.
• Fill in the blank at LINE-1 with an appropriate statement.
The program must satisfy the given test cases.

Solution:

#include <iostream>

using namespace std;

#define THRICE(X) ((X) * 3)

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

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


Question 2

Consider the following program.
• Fill in the blank at LINE-1 with appropriate formal arguments list.
• Fill in the blank at LINE-2 with the appropriate return statement.
The program must satisfy the sample input and output.

Solution:

#include <iostream>

using namespace std;

int sqr(int num) {
    return num * num;
}

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

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


Question 3

Consider the following program.
• Fill in the blanks at LINE-1 with appropriate function header
The program must satisfy the sample input and output.

Solution:

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

void print(int a, int b) {
    int r = b + 10 * a;
    cout << r;
}

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

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

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

More Nptel Courses: Click here


Programming in Modern C++ Week 2 Assignment Answers (JULY-DEC 2023)

Course Name: Programming in Modern C++

Course Link: Click Here

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


Programming

Question 1
Consider the following program. Fill in the blanks as per the instructions given below:
• at LINE-1 with function header
such that it will satisfy the given test cases.

Solution:

#include <iostream>
using namespace std;

void print(int a, int b=0){

    int r = b + a;
    cout << r;
}

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


Question 2
Consider the following program. Fill in the blanks as per the instructions given below.
• at LINE-1 with appropriate function parameter,
• at LINE-2 with appropriate statement
such that it will satisfy the given test cases.

Solution:

#include <iostream>
using namespace std;

int Fun(int& x) {

    x = x * x;
    return x;
}

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


Question 3
Consider the following program. Fill in the blanks as per the instructions given below:
• at LINE-1 with appropriate function header,
• at LINE-2 with appropriate return statement
such that it will satisfy the given test cases.

See also  Programming in Modern C++ | Week 6

Solution:

#include <iostream>
using namespace std;
struct point {
    int x, y;
};
point operator+(point &pt1, point &pt2){

    pt1.x += pt2.x;
    pt1.y += pt2.y;
    return pt1;
}

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

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

More Nptel Courses: Click here


Programming in Modern C++ Week 2 Assignment Answers (JULY-DEC 2022)

Course Name: Programming in Modern C++

Course Link: Click Here

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


Quiz


Q1. Consider the following program.
What will be the output/error(s)?

a) y
b) z
c) Compilation Error : default argument missing for “char add (char, char, char)”
d) Compilation Error : call of overload “add (char, char)” is ambiguous

Answer: c, d


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

a) 12
b) 25
c) 9
d) 16

Answer: a) 12


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


Q3. Consider the following code segment.
Which line/s will give you an error?

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

Answer: b, d


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

a) 36
b) 30
c) 25
d) Compilation Error : invalid initialization of non-const reference

Answer: d) Compilation Error : invalid initialization of non-const reference


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


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

a) 6 6 2 2
b) 6 6 7 7
c) 1 1 2 2
d) 1 1 7 7

Answer: a) 6 6 2 2


Q6. Consider the following code segment.
Choose the appropriate option to fill in the blanks at LINE-1, such that the output of the code would be : 300 20000.

a) int n3, int* n4
b) int& n3, int n4
c) int n3, int* n4
d) int& n3, int& n4

Answer: b) int& n3, int n4


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


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

a) < garbage value >
b) 5
c) Compilation Error at LINE-1 : uninitialized const ‘ptr’
d) Compilation Error at LINE-2 : assignment of read-only variable ‘ptr’

Answer: c, d


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

a) 5
b) 10
c) 5
10
d) Compilation error at LINE-2 : ambiguating new declaration of ‘int fun (int)’

Answer: d) Compilation error at LINE-2 : ambiguating new declaration of ‘int fun (int)’


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


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

a) complex operator+(complex &c1, complex &c2)
b) complex operator+(const complex &c1, const complex &c2)
c) operator+(complex &c1, complex &c2)
d) complex +(complex &c1, complex &c2)

Answer: a) complex operator+(complex &c1, complex &c2)


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


Assignment Questions


Question 1

Consider the program below.
• Fill in the blank at LINE-1 to complete the function header.
The program must satisfy the given test cases.

Solution:

#include <iostream>
#include <string>
using namespace std;
void print(string a, string b="Any")
{ // LINE-1

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


Question 2

Consider the following program.
• Fill in the blank at LINE-1 with the appropriate parameter,
• Fill in the blank at LINE-2 with the appropriate return statement
The program must satisfy the sample input and output.

Solution:

#include <iostream>
using namespace std;
int Fun(int a){
// LINE-1

  return a*a;
// LINE-2
}

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


Question 3

Consider the program below.
• Fill in the blank at LINE-1 to complete the function header
• Fill in the blank at LINE-2 to complete the return statement
The program must satisfy the given test cases.

Solution:

point operator+( point& pt, int& t) { //LINE-1
pt.x += t;
pt.y -= t;
return pt; //LINE-2
}

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