NPTEL Programming in Modern C++ Week 2 Assignment Answers 2024

Join Our WhatsApp Group Join Now
Join Us On Telegram Join Now

NPTEL Programming in Modern C++ Week 2 Assignment Answers 2024

1. Consider the following program.

#include ‹iostream>
using namespace std;
int add (int n1 = 0) { return n1; }
int add(int n1 = 0, int n2 = 0) & {return n1 + n2 – 1;}
int add (int n1 = 0, char c1 = ‘a’){ return n1 + c1 – 1; }
int add (char c1 = ‘z’, int n1 = 0) { return n1 + c1 ; }
int main() {
int c = add (10, 5);
cout << c << endl;
return 0;
}
What will be the output?

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

Answer :- For Answer Click Here 

2. Consider the following program.

include ‹iostream>
using namespace std;
define DOUBLE1 (x) x + x
define DOUBLE2 (x) 2 * x
int main() {
int a=2;
cout « DOUBLE1 (a++) « ” ” << DOUBLE2(a++) « endl;
return 0;
}
What will be the output (Consider right to left execution of the cout statement)?
a) 7 4
b) 84
c) 4 4
d) 7 6

Answer :- For Answer Click Here 

3. Consider the following code segment.

include ‹iostream>
using namespace std;
int& func (int& x) { //LINE-1
return x = x*2;
}
int main() {
int a = 3, b = 4;
int& c = func(a) ;
cout «a« ” ” «c«” “;
func (a) = b;
cout « a «” ” « c;
return 0;
}
What will be the output?
а) 6 6 2 2
b) 6 8 4 2
с) 6 64 4
d) 86 4 4

Answer :- For Answer Click Here 

4. Consider the following code segment.

include ‹iostream>
using namespace std;
void compute (int x1, int x2,________________,______________) {//LINE-1
x3 = x1 + x2;
*x4 = x1 * x3;
}
int main() {
int a = 10, b = 20, c = 1, d = 1;
compute (a, b, c, &d); //LINE-2
cout « c«<“, “;
cout << d;
return 0;
}
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 :- For Answer Click Here 

5.

NPTEL Programming in Modern C++ Week 2 Assignment Answers 2024
Answer :- 

6. Consider the following code segment.

include <iostream>
using namespace std;
int main(){
int x = 2, y = 7;
_________ int &r = ++ + ++y; //LINE-1
cout «< r;
return 0;
}
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 :- For Answer Click Here 

7.

NPTEL Programming in Modern C++ Week 2 Assignment Answers 2024
Answer :- 

8.

NPTEL Programming in Modern C++ Week 2 Assignment Answers 2024
Answer :- 

9. Consider the following function prototypes of overloaded function func().

  1. int func(int , char* = 0);
  2. double func(double = 0, double = 0) ;
  3. float func(float, float, char* = 0);
  4. float func(float n) ;
  5. int func (int n1 = 0, int n2 = 0, int n3 = 0) ;
    Which functions will be invoked for the call func (2.1, 3.7f)?
    a) 2, 3, 5
    b) 2, 3
    c) 2
    d) 1, 2
Answer :- For Answer Click Here 

W2_Programming_Qs-1

NPTEL Programming in Modern C++ Week 2 Assignment Answers 2024

Solution:-

#include <iostream>

using namespace std;

#define THRICE(X) ((X)*(X)) //LINE-1

int main(){
    int n;
    cin >> n;
    cout << THRICE(n+1);
    return 0;
}

W2_Programming_Qs-2

NPTEL Programming in Modern C++ Week 2 Assignment Answers 2024

Solution:-

For Solution Click Here