Programming in Modern C++ | Week 2

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


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

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

More NPTEL Solutions: https://progiez.com/nptel/


Nptel Programming in Modern C++ Assignment 2 Answers

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