NPTEL Programming In Modern C++ Week 3 Assignment Answers 2023

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

NPTEL Programming In Modern C++ Assignment Solutions 2023

NPTEL Programming In Modern C++ Week 3 Assignment Answers 2023

NPTEL Programming In Modern C++ Week 3 Quiz Assignment Answers 2023

1. Consider the following program.

NPTEL Programming In Modern C++ Week 3 Assignment Answers 2023
Answer :- For Answer Click Here

2. Consider the code segment given below.

NPTEL Programming In Modern C++ Week 3 Assignment Answers 2023
Answer :- For Answer Click Here

3. Consider the following code segment.

NPTEL Programming In Modern C++ Week 3 Assignment Answers 2023
Answer :- For Answer Click Here

4. Consider the segment given below.

NPTEL Programming In Modern C++ Week 3 Assignment Answers 2023
Answer :- For Answer Click Here

5. Consider the code Segment.

NPTEL Programming In Modern C++ Week 3 Assignment Answers 2023
Answer :- For Answer Click Here

6. Consider the code segment given below.

NPTEL Programming In Modern C++ Week 3 Assignment Answers 2023
Answer :- For Answer Click Here

7. Consider the code segment below.

NPTEL Programming In Modern C++ Week 3 Assignment Answers 2023
Answer :- For Answer Click Here

8. Consider the Code Segment given below.

NPTEL Programming In Modern C++ Week 3 Assignment Answers 2023
Answer :- For Answer Click Here

9. Consider the code segment given below.

NPTEL Programming In Modern C++ Week 3 Assignment Answers 2023
Answer :- For Answer Click Here

NPTEL Programming In Modern C++ Week 3 Programming Assignment Answers 2023

Q1. Consider the following program. Fill in the blanks as per the instructions given below:

    • Complete two constructor statements at LINE-1 and LINE-2,

    • Complete the return statement at LINE-3 to calculate the manhattan distance between two points,

such that it will satisfy the given test cases.

NPTEL Programming In Modern C++ Week 3 Assignment Answers 2023
Answer :- For Answer Click Here

Q2. Consider the following program. Fill in the blanks as per the instructions given below.

    • at LINE-1 with constructor definition,

    • at LINE-2 with appropriate statement to deallocate array memory

such that it will satisfy the given test cases. 

Answer :- For Answer Click Here

Q3. Consider the following program. Fill in the blanks at LINE-1, LINE-2 and LINE-3 with appropriate keywords such that it will satisfy the given test cases.

Answer :- For Answer Click Here
Course NameProgramming In Modern C++
CategoryNPTEL Assignment Answer
Home Click Here
Join Us on TelegramClick Here

NPTEL Programming In Modern C++ Week 2 Quiz Assignment Answers 2023

1. Consider the following program.

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

class Sample {
    string name;
public:
    Sample() {
        cout << "s" << "  ";
    }
    Sample(string s) : name(s) {
        cout << name << "  ";
    }
};

int main() {
    Sample s1; //LINE-1
    Sample *s2 = new Sample("s2");
    Sample *s3;
    new Sample("s4");
    return 0;
}

What will be the output /error?

a. compilation error at line-1
b. s s2 s s4
c. s s2 s4
d. s2 s4

Answer :- b. s s2 s s4 

2. Consider the code segment given below.

#include <iostream>
using namespace std;

int i = 0;

class myClass {
public:
    myClass() { i += 5; }
};

void f() {
    myClass mi();
    myClass m2;
}

int fun() {
    i = 3;
    f();
    return i++;
}

int main() {
    cout << fun() << " ";
    cout << i << endl;
    return 0;
}

What will be the output?

a) 3 4
b) 15 16
c) 9 10
d) 4 5

Answer :- c) 9 10 

3. Consider the following code segment.

#include <iostream>
using namespace std;

class Private_Data {
    int x;
    void f1() {
        cout << "Within f1";
    }
public:
    int y;
    void f2() {
        cout << "Within f2";
    }
};

int main() {
    Private_Data t;
    t.x = 1;  // LINE-1
    t.f1();   // LINE-2
    t.y = 2;  // LINE-3
    t.f2();   // LINE-4
    return 0;
}

Which line/s will generate compilation error/s?

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

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

4. Consider the code segment given below.

#include<iostream>
using namespace std;

class MyClass {
public:
    MyClass(int i) { cout << i; }
    MyClass(const MyClass &t) { cout << "2"; }
};

int main() {
    MyClass *t1, *t2;
    // LINE-1
    t1 = new MyClass(0);
    // LINE-2
    t2 = new MyClass(*t1); // LINE-3
    MyClass t3 = *t1;
    // LINE-4
    MyClass t4 = *t2;
    // LINE-5
    return 0;
}

What will be the output?

a) 02020202
b) 0202
c) 0222
d) 0002

Answer :- b) 0202.

5. Consider the code segment.

class Check {
// code.
};
int main() {
const Check c; // LINE-1
return 0;
}

What is the type of this pointer associated with the object c?

a) const Check* this
b) Check* const this
c) Check const const this
d) const Check* const this

Answer :- a) const Check this

Here, c is a constant object of the class Check because it is declared as const Check c;. Since c is a constant object, any member function called on it will be treated as a const member function. So, the type of this pointer associated with the object c is const Check* this.

6. Consider the code segment given below.

#include <iostream>
using namespace std;

class Test {
public:
    Test() { cout << "O" << endl; }
    Test(int x=0) { cout << "K" << endl; }
};

int main() {
    Test t1;
    return 0;
}

What will be the output/error?
a) 0
b) K
c) OK
d) Compilation error: call of overload Test () is ambiguous

Answer :- a) 0.

Since we are calling the default constructor Test() without any arguments, the output will be: O

7. Consider the code segment below.

#include <iostream>
using namespace std;
class Point {
int x, y;
public:
Point (int _x, int _y) : x(x), y(y) ( )
Point (Point &c) : x(c.x), y (c.y) € )
void change (Point new_c) ( this = new_c; )
void show() ( cout « x « “, ” « y « endl; )
};
int main() {
Point c1(10, 20);
Point c2(20, 50);
Point c3(c1);
c3. change (&c2);
c3. show ();
return 0;
}

What will be the output/error?
a) 10 20
b) 20 50
c) Compilation Error: Ivalue required as left operand of assignment
d) Compilation Error: private data members are inaccessible

Answer :- b) 20 50.

8. Consider the code segment given below.

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

class Data {
    int _d = 0;

public:
    int set_d(int d) const {
        _d = d;
    }
    int get_d() const {
        return _d;
    }
};

int main() {
    const Data obj;
    obj.set_d(5);
    cout << obj.get_d();
    return 0;
}

What will be the output/error?

a) 0
b) 5
c) 6
d) Compilation error: assignment of member ‘Data:: _d’ in read-only object

Answer :- b) 5

9. Consider the code segment given below.

#include<iostream>
using namespace std;

class Print {
    int x;

public:
    Print(int _x) : x(_x) {}
    void display() { cout << x; }
};

int main() {
    Print i(1);
    i.display();
    // Fill in the blank here to increment x and make it 2.
}

Fill in the blank at LINE-1 so that the program will print 2.

a) ++×
b) ++this->x
C) X++
d) ++this.x

Answer :- b) ++this->x

NPTEL Programming In Modern C++ Week 2 Programming Assignment Answers 2023

Q1. 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){ // LINE-1

    int r = b + a;
    cout << r;
}
int main() {

    int a, b;
    cin >> a >> b;
    if (b < 0)
        print(a);
    else
        print(a,b);
    return 0;
}

Q2. 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) { // LINE-1

    x = x * x; // LINE-2
    return x;
}
int main() {
    int x, y;
    cin >> x;
    y = Fun(x);
    cout << x << " " << y;
    return 0;
}

Q3. 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){ //LINE-1

    pt1.x += pt2.x;
    pt1.y += pt2.y;
    return pt1;    //LINE-2
}

int main() {
    int a, b, c, d;
    cin >> a >> b >> c >> d;
    point p1 = {a, b};
    point p2 = {c, d};
    point np = p1 + p2;
    cout << "(" << p1.x << ", " << p1.y << ")" << "(" << np.x << ", " << np.y << ")";
    return 0;
}
Course NameProgramming In Modern C++
CategoryNPTEL Assignment Answer
Home Click Here
Join Us on TelegramClick Here

NPTEL Programming In Modern C++ Week 1 Quiz Assignment Answers 2023

1. Consider the code segment given below.
#include <iostream>
#include<cmath>
int main() {
int n = 4;
_________ //LINE-1
return 0;
}

Fill in the blank at LINE-1 such that the output is 16 2.
a) std:: cout << std::pow(n, 2) < ” ” <‹ std: :sqrt (n)
b) cout <‹ pow(n,2) <‹ ” ” <‹ sqrt (n)
c) std:: cout << std::pow(n) « ” ” << std: :sqrt (n)
d) cout << pow(n) << ” ” << sqrt (n)

Answer :- a) std::cout << std::pow(n, 2) << " " << std::sqrt(n)

The code segment uses the functions from the <cmath> library to calculate the square and square root of the variable "n" and then prints the results using std::cout. The pow() function calculates the power (in this case, the square) of a number, and the sqrt() function calculates the square root of a number. The output will be "16 2" for n = 4, indicating the square of 4 is 16, and the square root of 4 is 2.

2. Consider the code segment given below.

#include <iostream>
#includevector>
using namespace std;
int main(){
___________; //LINE-1

arr.resize (10);
for (int i=0;i<10;i++)
arr [i] = i+1;
return 0;
}

Fill in the blank at LINE-1 such that the program will run successfully.

a) vector<int> arr
b) vector<int> arr (3)
c) vector<int> arr [3]
d) int arr [3]

Answer :- a) vector<int> arr

The code segment is using vectors from the <vector> library. In order to use a vector named "arr," it needs to be declared with the correct syntax, which is "vector<int> arr;".

3. Consider the following code segment.

#include ‹iostream>
#include<cstring>
using namespace std;

int main() {
string S = S “programming in modern C++”;
cout << s. size() ; //LINE-1
cout << strlen(s); //LINE-2
cout << s. length(); //LINE-3
cout << strlen(s.c_str()); //LINE-4
return 0;
}

Which line/ will give compilation error/s?

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

Answer :- b) LINE-2

In the given code segment, there is a compilation error on LINE-2.

The reason is that "s" is declared as "S" in lowercase, but it should be "S" in uppercase to match the declaration of the string variable "S" on LINE-5. Also, on LINE-1, there is a typo in the assignment statement, it should be "S = "programming in modern C++";" instead of "string S = S "programming in modern C++";".

4. Consider the code segment given below.

#include <iostream>
#include <algorithm>
using namespace std;
int main () {
int data[] = {50, 30, 40, 10, 20};
sort (&data [2], &data [5]);
for (int i = 0; i < 5; i++)
cout <‹ data[i] << “
return 0;
}

What will be the output?

a) 10 20 30 40 50
b) 10 30 40 50 20
c) 50 30 10 20 40
d) 50 10 20 30 40

Answer :- c) 50 30 10 20 40

In the given code segment, the "sort" function is used to sort elements of the array "data" starting from the element at index 2 (data[2]) up to the element before index 5 (data[4]). The sort function arranges these elements in ascending order, which results in the output:

5. Consider the code segment given below.
#include <iostream>
#include <string>
using namespace std;
int main(void) {
string stri = “Welcome “;
string str2 = “students”;
_________ //LINE-1
cout << stri;
return 0;
}

Choose the appropriate option to fill in the blank at LINE-1, such that the output of the code would be: Welcome students.

a) stri += str2
b) strcat (stri, str2)
c) str1. append(str2)
d) str1.insert (str2)

Answer :- a) stri += str2

The code snippet provided aims to concatenate the strings "stri" and "str2" to produce the output "Welcome students." The operator "+=" is used for string concatenation in C++.

6. Consider the code segment given below.

NPTEL Programming In Modern C++ Week 3 Assignment Answers 2023
Answer :- b) CORETUPM

7. Consider the Code segment below.

NPTEL Programming In Modern C++ Week 3 Assignment Answers 2023
Answer :- b) 0 2 6 8 4

8. Consider the code segment given below.
#include ‹iostream>
#include <algorithm>
using namespace std;
int main () {
int iarr [] = {10, 20, 50, 40, 10, 50};
rotate(&iarr [0], &iarr[2], &iarr [6]) ;
remove (&iarr[0], &iarr [6], 10); //LINE1
for (int i = 0; i < 4; ++i) cout << iarr [i] << ” “;
return 0;
}

What will be the output?

a) 40 10 10 20
b) 50 40 50 20
C) 50 50 40 20
d) 40 10 40 20

Answer :- C) 50 40 50 20

9. Consider the code segment given below.
#include<iostream>
#include<algorithm>
#include<string.h>
using namespace std;
bool compare (char c1, char c2){
return tolower (c1) > tolower(c2); //LINE-1
}
int main() {
char arr1[20] “C++ Program”, arr2[20] = “C Program”;
cout << lexicographical_compare (arr1, arr1+strlen(arr1), arr2, arr2+5, compare) ;
return 0;
}

What will be the output?

a) 1
b) 0
c) -1
d) Compilation Error: function is not defined

Answer :- a) 1

The ranges are compared up to the first 5 characters of arr1 and arr2. Since "C++" is lexicographically greater than "C Pro" (comparing case-insensitively), the output of the lexicographical_compare function will be true, which is represented as 1.
Course NameProgramming In Modern C++
CategoryNPTEL Assignment Answer
Home Click Here
Join Us on TelegramClick Here

NPTEL Programming In Modern C++ Week 1 Programming Assignment Answers 2023

Q1. Consider the following program. Fill in the blanks as per the instructions given below:

  • at LINE-1 with stack declaration,
  • at LINE-2 to push values into stack,
  • at LINE-3 with appropriate statement

such that it will satisfy the given test cases.

Solution:-

#include<iostream>
#include<cstring>
#include<stack>
using namespace std;


int main() {
    char str[20];
    char ch;
    cin >> str;
    stack<char> s; //LINE-1

    for(int i = 0; i < strlen(str); i+=2)
        s.push(str[i]); //LINE-2

    int len = s.size();

    for(int i = 0; i < len; i++) {

    	ch = s.top();  //LINE-3
    	cout << ch;

        s.pop();
    }
    return 0;
}

Q2. Consider the following program. Fill in the blanks as per the instructions given below.

    • at LINE-1 with appropriate header statement,

    • at LINE-2 with appropriate statement to calculate Euclidean distance

such that it will satisfy the given test cases.

Solution:-

#include <iostream>
#include <cmath>     //LINE-1

using namespace std;
struct point{
    int x, y;
};

double get_len(point p1, point p2){
    return sqrt((p2.x - p1.x)*(p2.x - p1.x)+(p2.y - p1.y)*(p2.y - p1.y));    //LINE-2
}

int main() {
    int x1, y1, x2, y2;
    cin >> x1 >> y1 >> x2 >> y2;
    point p1, p2;
    p1.x = x1;
    p1.y = y1;
    p2.x = x2;
    p2.y = y2;
    cout << round(get_len(p1, p2));
    return 0;
}

Q3. 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>
#include <algorithm>
using namespace std;

bool max_str(string a, string b) {    //LINE-1

    return a > b;            //LINE-2
}

int main() {
    std::string words[3], word;
    for(int i = 0; i < 3; i++){
        cin >> word;
        words[i] = word;
    }
    sort(words, words + 3, max_str);
    for (int i = 0; i < 3; i++)
        cout << words[i] << " ";
    return 0;
}

Leave a comment