Sign Up

Sign In

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Sorry, you do not have permission to ask a question, You must login to ask a question.

SIKSHAPATH Latest Articles

Programming in Modern C++ Assignment Week 2 2022

A blog banner of programming in modern c++ nptel course answer

Students, are you searching for hints of Programming in Modern C++ NPTEL assignment week 2. So, you are landed on the right article where you will get hints for answers by scrolling down.

Programming in Modern C++ NPTEL Assignment Week 2

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

P1: int add(int n1=0, int n2);

P2: int add(int = 0, int = 0);

P3: double add(double = 0, double d = 0.0);

P4: double add(int = 0, double d = 0.0);

P5: double add(int n1 = 0, int n2, double n3 = 0);

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

a. {P1, P3}
b. {P2, P3, P4}
c. {P1, P2, P3, P4}
d. {P2, P3, P5}

Answer: b. {P2, P3, P4}

For instant notification of any updates, Join us on telegram.


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

P1: int add(int n1);

P2: int add(int = 0, int = 0);

P3: double add(int = 0, double d = 0.0);

P4: int add(int = 0, double d = 0.0);

P5: double add(int n1 = 0, int n2 = 0, double n3 = 0);

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

a. P1, P2

b. P2, P3

c. P3, P4

d. P2, P5

Answer: c. P3, P4


Q3. Consider the following code segment.

#include <iostream>
#include <iostream>
using namespace std;
int add(int n1 = 100) { return n1; }
int add(int n1 = 100, int n2 = 100) { return n1 + n2;}
int add(int n1 = 100, double d1 = 100.0){ return n1 + d1; }
int add(int n1 = 100, int n2 = 100, int n3 = 100) { return n1+n2 + n3; }
int main() {
int r = add(10, 20);
cout << r <<< endl;
return 0;
}

What will be the output/error?

a. 30

b. 130

c. 300

d. Compilation Error: call of overloaded ‘add (int, int)’ is ambiguous

Answer: d. Compilation Error: call of overloaded ‘add (int, int)’ is ambiguous


Q4. Consider the following code segment.

#include <iostream>
using namespace std; 
#define DOUBLE (x) (2 + x)
inline int TRIPLE(int x){ 
return 3x;
}
int main(){
cout << DOUBLE (10 + 10) << " ";
cout << TRIPLE (10 + 10) << " ";
return 0;
}

What will be the output?

a. 40 60

b. 30 60

c. 30 40

d. 20 30

Answer: b. 30 60


Q5. Consider the following code segment.

#include <iostream> 
using namespace std;
____________{    //LINE-1
return ++i;
}
int main()
{
int x = 10, y = 20;
int& z = incr(x); cout << x << " " << z << " ";
incr(x) = y; cout << x <<< " " << z; return 0; }

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

a. int incr(int i)

b. int incr(int& i)

c. int& incr(const int& i)

d. int& incr(int& i)

Answer: d. int& incr(int& i)


Q6. Consider the following code segment.

#include<iostream> 
using namespace std;
#define C1 10 + 1 
const int C2 = 10 + 1;
int main()
{
int n1, n2;
n1 = C1 * 100 * C2; 
n2 = C2 100* C1;
cout << n1 << " " << n2;
return 0;
}

What will be the output?

a. 1110 11001

b. 1011 1011

c. 12100 12100

d. 11001 1110

Answer: a. 1110 11001


Q7. Consider the following code segment.

#include <iostream> 
using namespace std;
int main() {
int i= 10;
int &r= i;
r += i += ++r;
cout << i << " " << r;
return 0;
}

What will be the output?

a. 21 32

b. 32 32

c. 21 44

d.  44 44

Answer: d.  44 44


Q8. Consider the following code segment.

#include<iostream>
using namespace std;
int main(){
const char c= 'X';
__________= &c; //LINE-1
cout << *cp++;
return 0;
}

Identify the correct statement/s to fill in the blank at LINE-1 such that the output is X.

a. const char* cp

b. char* const cp

c. char const* cp

d. const char * const cp.

Answer: a. const char* cp & c. char const* cp


Q9. Consider the following statement in C.

int *ip = (int *)malloc(sizeof (int)+ 5);

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

a. int *ip = new int(5);

b. int *ip = new int[5];

c. int *ip = (int *)operator new (sizeof(int) * 5);

d. int *ip = (int *) operator new (sizeof(int)) [5];

Answer: b. int *ip = new int[5];

c. int *ip = (int *)operator new (sizeof(int) * 5);


TELEGRAM FOR NOTIFICATIONClick Here
Follow on Google News (in one click)Click Here

Disclaimer: These answers are provided only for the purpose to help students to take references. This website does not claim any surety of 100% correct answers. So, this website urges you to complete your assignment yourself.


Also Available:

Programming in Modern C++ NPTEL Assignment Week 1

Related Posts