In this post, We have provided answers of NPTEL Programming In Modern C++ Assignment 2. We provided answers here only for reference. Plz, do your assignment at your own knowledge.
NPTEL Programming In Modern C++ Week 2 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 :- Click Here
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 :- Click Here
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 :- Click Here
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 :- Click Here
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 :- Click Here
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 :- Click Here
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 :- Click Here
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 :- Click Here
NPTEL Programming In Modern C++ Assignment 2 Answers 2022
Q1. Consider the following function prototypes of function add ().
P1: int add (int ni 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 ni = 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
2. Consider the following function prototypes of function add ().
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
a) P2, P5
Answer:- d
Answers will be Uploaded Shortly and it will be Notified on Telegram, So JOIN NOW
3. Consider the following statement in C.
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
4. Consider the following statement in C.
What will be the output?
a) 40 60
b) 30 60
c) 30 40
d) 20 30
Answer:- b
5. Consider the following statement in C.
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 (intk i)
Answer:- d
6. Consider the following statement in C.
Answer:- a
👇For Week 03 Assignment Answers👇
7. Consider the following statement in C.
Answer:- d
8. Consider the following statement in C.
Answer:- a, c
9. Consider the following statement in C.
Answer:- b, c
Programming Assignment Answers
Q1. Consider the program below which defines a type point and overloads operator + such that the x and y coordinates of a given point can be added with an integer.
Complete the program with the following instructions.
Code:-
Q2. Consider the following program. Fill in the blanks at LINE-1 and LINE-2 with appropriate headers of overloaded function product. The program must satisfy the sample input and output.
Code:-
Q3. Consider the following program. Fill in the blank at LINE-1 with an appropriate function header for the function update_and_sum( ) such that it satisfies the sample input and output.
Code:-
For More NPTEL Answers:- CLICK HERE
Join Our Telegram:- CLICK HERE
About Programming In Modern C++
There has been a continual debate on which programming language/s to learn, to use. As the latest TIOBE Programming Community Index for August 2021 indicates – C (13%), Python (12%), C++ (7%), Java (10%), and C#(5%) together control nearly half the programming activities worldwide. Further, C Programming Language Family (C, C++, C#, Objective C etc.) dominate more than 25% of activities. Hence, learning C++ is important as one learns about the entire family, about Object-Oriented Programming and gets a solid foundation to also migrate to Java and Python as needed. C++ is the mother of most general purpose of languages. It is multi-paradigm encompassing procedural, object-oriented, generic, and even functional programming. C++ has primarily been the systems language till C++03 which punches efficiency of the code with the efficacy of OOP.
Then, why should I learn it if my primary focus is on applications? This is where the recent updates of C++, namely, C++11 and several later offer excellent depths and flexibility for C++ that no language can match. These extensions attempt to alleviate some of the long-standing shortcomings for C++ including porous resource management, error-prone pointer handling, expression semantics, and better readability. The present course builds up on the knowledge of C programming and basic data structure (array, list, stack, queue etc.) to create a strong familiarity with C++98 / C++03. Besides the constructs, syntax and semantics of C++ (over C), we also focus on various idioms of C++ and attempt to go to depth with every C++ feature justifying and illustrating them with several examples and assignment problems. On the way, we illustrate various OOP concepts. The course also covers important advances in C++11 and later released features.
COURSE LAYOUT
- Week 1: Programming in C++ is Fun.
- Week 2: C++ as Better C.
- Week 3: OOP in C++.
- Week 4: OOP in C++.
- Week 5: Inheritance.
- Week 6: Polymorphism.
- Week 7: Type Casting.
- Week 8: Exceptions and Templates.
- Week 9: Streams and STL.
- Week 10: Modern C++.
- Week 11: Lambda and Concurrency.
- Week 12: Move, Rvalue and STL Containers.
CRITERIA TO GET A CERTIFICATE
Average assignment score = 25% of average of best 8 assignments out of the total 12 assignments given in the course.
Exam score = 75% of the proctored certification exam score out of 100
Final score = Average assignment score + Exam score
YOU WILL BE ELIGIBLE FOR A CERTIFICATE ONLY IF AVERAGE ASSIGNMENT SCORE >=10/25 AND EXAM SCORE >= 30/75. If one of the 2 criteria is not met, you will not get the certificate even if the Final score >= 40/100.