This blog provides solutions to Assignment 8 of the Programming in Modern C++ course on Swayam NPTEL. The assignment consists of multiple-choice questions covering exception handling, templates, function pointers, and operator overloading in C++.
Consider the following program.
#include <iostream>
using namespace std;
void testIt(int i) {
if (i)
throw i;
else
throw "zero";
}
int main() {
try {
// statement-1
}
catch (int e) {
cout << "int" << endl;
}
catch (float e) {
cout << "float" << endl;
}
catch (double e) {
cout << "duoble" << endl;
}
catch (const char* e) {
cout << "string" << endl;
}
catch (...) {
cout << "anything else" << endl;
}
return 0;
}
What will be the outputs in consecutive two runs if statement-1 is replaced by (i) testIt(3.14); and (ii) testIt(0); respectively?
a) (i) int and (ii) string
b) (i) double and (ii) string
c) (i) float and (ii) anything else
d) (i) double and (ii) anything else
✅ Ans: a
Consider the following code segment.
#include<iostream>
using namespace std;
class InternalError {
public:
virtual const char what() const throw() {
return "internal exception";
}
};
class DBError public InternalError {
public:
virtual const char what() const throw() {
return "DB exception";
}
};
class SQLError public DBError {
public:
virtual const char* what() const throw() {
return "SQL exception";
}
};
int main(){
try{
throw SQLError();
}
catch (InternalError& e) {
cout << e.what();
}
catch (DBError& e) {
cout << e.what();
}
catch(SQLError& e) {
cout << e.what();
}
catch(...){
cout << "default" << endl;
}
return 0;
}
What will be the output?
a) internal exception
b) SQL exception
c) DB exception
d) default
✅ Ans: a
Consider the following code segment.
#include <iostream>
using namespace std;
namespace ServerExceptions{
class InternalError {};
class DBError public InternalError {};
class SQLError public DBError {};
};
void proc(){
try {
throw ServerExceptions::SQLError();
}
catch (ServerExceptions::SQLError& e) {
throw ServerExceptions::DBError();
}
}
int main() {
try{
proc();
}
catch (ServerExceptions::InternalError&) { //LINE-1
cout << "internal error" << endl;
}
catch (ServerExceptions::SQLError&) { //LINE-2
cout << "sql error" << endl;
}
catch (ServerExceptions::DBError&) {
cout << "db error" << endl;
}
catch (...) {
cout << "default" << endl;
}
return 0;
}
What will be the output?
a) internal error
b) sql error
c) db error
d) default
✅ Ans: a
Consider the following code segment.
#include<iostream>
using namespace std;
void throwIt(){
try{
throw "divide by zero";
}
catch(int&) {
throw;
}
}
int main(){
try{
throwIt();
}
catch(...){
cout << "default";
}
catch(int& e) {
cout << "int";
}
//LINE-1
catch (double& e) {
cout << "double";
}
return 0;
}
What will be the output/error?
a) default
b) int
c) double
d) Compiler error LINE-1: '...' handler must be the last handler for its try block
✅ Ans: d
Consider the following code segment.
#include<iostream>
#include<cstring>
#include<cstdlib>
using namespace std;
template<class T>
class Modifier{
T val;
public:
Modifier (Tval 0) val(val) {}
T subtract (int d){
Tt val d;
return t;
}
};
//LINE-1
char* val;
{ //LINE-2
public:
Modifier (const char* _val 0) val (strdup(_val)) {}
char subtract(int d){
char buf (char*)malloc(strlen(val) d + 1);
int i;
for(i=0; i < strlen(val)
buf [i] val [i];
buf[i] = '\0';
return buf;
d; i++)
};
int main(){
Modifier<double> d 26.44;
Modifier<const char*> s("programming");
cout << d.subtract (4) << ", ";
cout << s.subtract (4);
return 0;
}
Identify the appropriate option to fill in the blanks at LINE-1 and LINE-2 such that the output becomes 22.44, program.
a) LINE-1: template<T>
LINE-2: class Modifier<char*>
b) LINE-1: template<>
LINE-2: class Modifier<char*>
c) LINE-1: template<>
LINE-2: class Modifier<const char*>
d) LINE-1: template<const char*>
LINE-2: class Modifier
✅ Ans: c
Consider the following code segment.
#include <iostream>
using namespace std;
void test(int a) {
cout << "integer" << endl;
}
void test(float a) {
cout << "float" << endl;
}
void test(double a) {
cout << "double" << endl;
}
int main() {
test(3.14); // LINE-1
return 0;
}
What will be the output?
a) integer
b) float
c) double
d) Compiler error at LINE-1
✅ Ans: c
Consider the following code segment.
#include<iostream>
using namespace std;
template<typename T>
void display(T t){
cout << "General Template" << endl;
}
template<>
void display<int>(int t){
cout << "Specialized Template" << endl;
}
int main(){
display(10); // LINE-1
display(10.5); // LINE-2
return 0;
}
What will be the output?
a) General Template
Specialized Template
b) Specialized Template
General Template
c) General Template
General Template
d) Compiler error at LINE-1
✅ Ans: b
Consider the following code segment.
#include<iostream>
using namespace std;
void test(int i) {
cout << "int" << endl;
}
void test(double i) {
cout << "double" << endl;
}
void test(char* i) {
cout << "char*" << endl;
}
int main(){
test(NULL); // LINE-1
return 0;
}
What will be the output?
a) int
b) double
c) char*
d) Compiler error at LINE-1
✅ Ans: a
Consider the following code segment.
#include <iostream>
using namespace std;
class Base {
public:
virtual void show() {
cout << "Base" << endl;
}
};
class Derived : public Base {
public:
void show() {
cout << "Derived" << endl;
}
};
int main() {
Base* bptr;
Derived d;
bptr = &d;
bptr->show(); // LINE-1
return 0;
}
What will be the output?
a) Base
b) Derived
c) Compiler error at LINE-1
d) Undefined behavior
✅ Ans: b
This concludes Assignment 8 solutions for the Programming in Modern C++ NPTEL course. If you found this useful, stay tuned for more solutions and explanations. 🚀
C++ is a powerful and widely used programming language, known for its efficiency, flexibility, and support for object-oriented, procedural, and functional programming. This course provides a comprehensive understanding of modern C++, covering fundamental concepts, advanced topics, and best practices for writing efficient, maintainable, and robust code.
This free online course is designed for both undergraduate and postgraduate students and is self-paced, allowing learners to go through the material at their own convenience. However, students who wish to obtain a certificate must take a proctored examination.
This course spans 12 weeks, gradually introducing key concepts in C++ programming. The initial weeks focus on basic programming principles, followed by an in-depth exploration of object-oriented programming (OOP), templates, type casting, and exception handling. The latter part of the course dives into modern C++ features, including lambda functions, concurrency, and move semantics, which are crucial for writing efficient and high-performance applications.
Week | Topic | Summary |
---|---|---|
Week 1 | Programming in C++ is Fun | Introduction to C++, its history, and why it is a preferred language for system programming. |
Week 2 | C++ as Better C | Understanding how C++ builds upon C and its additional features. |
Week 3 | Object-Oriented Programming (OOP) in C++ | Introduction to OOP principles such as classes, objects, and encapsulation. |
Week 4 | OOP in C++ (Continued) | Deep dive into constructors, destructors, and operator overloading. |
Week 5 | Inheritance | Understanding single, multiple, and hierarchical inheritance in C++. |
Week 6 | Polymorphism | Exploring runtime and compile-time polymorphism, function overloading, and virtual functions. |
Week 7 | Type Casting | Safe and effective type conversions using static_cast, dynamic_cast, const_cast, and reinterpret_cast. |
Week 8 | Exceptions and Templates | Handling runtime errors using exceptions and introduction to generic programming with templates. |
Week 9 | Streams and STL | Working with file streams, input-output operations, and the Standard Template Library (STL). |
Week 10 | Modern C++ | Introduction to C++11, C++14, and C++17, and their new features. |
Week 11 | Lambda and Concurrency | Understanding anonymous functions (lambda expressions) and multithreading concepts. |
Week 12 | Move Semantics, Rvalue References, and STL Containers | Optimizing code using move semantics and rvalue references to enhance performance. |
By the end of the course, learners will have a solid grasp of modern C++ programming and will be able to write efficient, modular, and maintainable code.
This course is supplemented with online materials and books that cover both fundamental and advanced aspects of C++ programming. The references include official ISO C++ standards, modern best practices, and insights from renowned experts in the field.
Book Title | Author | Summary |
---|---|---|
C++ Move Semantics - The Complete Guide | Nicolai M. Josuttis | Explains move semantics and rvalue references in depth. |
C++ Concurrency in Action (2nd Edition) | Anthony Williams | Covers multithreading and parallel programming in C++. |
C++17 - The Complete Guide | Nicolai M. Josuttis | Detailed guide to C++17 features and improvements. |
C++17 In Detail | Bartlomiej Filipek | Discusses practical applications of C++17. |
Professional C++ (4th Edition) | Marc Gregoire | Covers best practices and design principles for writing professional C++ code. |
Functional Programming in C++ | Ivan Čukić | Explains functional programming techniques in C++. |
Effective Modern C++ | Scott Meyers | Highlights 42 best practices for modern C++11 and C++14. |
These resources will help learners reinforce concepts and stay updated with the latest developments in the C++ ecosystem.
The course is free to learn, but to receive an official NPTEL certificate, students must take a proctored exam at designated test centers.
To earn the certificate, students must meet both assignment and exam score requirements.
Component | Weightage | Details |
---|---|---|
Assignments Score | 25% | Calculated from the best 8 out of 12 assignments. |
Exam Score | 75% | Based on the final proctored exam score. |
Final Score = (25% of Assignment Score) + (75% of Exam Score)
Requirement | Minimum Score Required |
---|---|
Assignments Score | At least 10/25 |
Exam Score | At least 30/75 |
Final Score | At least 40/100 |
This course is ideal for:
✔️ Beginners looking to learn C++ from scratch.
✔️ Intermediate programmers who want to deepen their understanding of OOP, STL, and Modern C++.
✔️ Students preparing for technical interviews and competitive programming.
✔️ Developers transitioning from C to C++ or upgrading from older versions of C++.
By completing this course, learners will gain in-depth knowledge of modern C++ features, making them proficient in writing efficient and scalable applications.