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

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 (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

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.

See also  Programming in Modern C++ | Week 3

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


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

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

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.

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

See also  Programming in Modern C++ | Week 4

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