Programming in Modern C++ | Week 12
Course Name: Programming in Modern C++
Course Link: Click Here
These are Nptel Programming in Modern C++ Assignment 12 Answers
Quiz
Q1. Which of the following types of smart pointers follow /s shared ownership policy?
a) std: :auto_ptr
b) std: :unique_ptr
c) std: :std: :shared ptr
d) std: :weak ptr
Answer: c, d
Q2. Consider the code segment (in C++11) given below.
What will be the output?
a) SmartPtr::ctor(), MyClass::ctor(), MyClass::fun(), SmartPtr::operator->(),
b) MyClass::ctor(), SmartPtr::ctor(), SmartPtr::operator->(), MyClass::fun(),
c) SmartPtr::ctor(), MyClass::ctor(), SmartPtr::operator(), MyClass::fun(),
d) MyClass: :ctor(), SmartPtr::ctor(), SmartPtr::operator(), MyClass::fun(),
Answer: d) MyClass: :ctor(), SmartPtr::ctor(), SmartPtr::operator(), MyClass::fun(),
These are Nptel Programming in Modern C++ Assignment 12 Answers
Q3. What will be the output?
a) -7 6
b) -8 6
c) -7 7
d) -8 16
Answer: a) -7 6
Q4. Consider the following code segment.
What will be the output?
a) 22322
b) 22420
c) 23430
d) 23422
Answer: b) 22420
These are Nptel Programming in Modern C++ Assignment 12 Answers
Q5. Consider the following code segment (in C++11).
What will be the output?
a) 1 1 expired expired
b) 1 0 1 0
c) 1 1 expired 1
d) 1 1 1 expired
Answer: a) 1 1 expired expired
Q6. Consider the following code segment (in C++11).
Choose the appropriate option to fill in the blank at LINE-1 such that output becomes 24.5.
a) std: :thread (Summation (dv))
b) std: :async(Summation(dv))
c) std: :atomic(Summation(dv))
d) std: :thread{bind(Summation(dv))}
Answer: b) std: :async(Summation(dv))
These are Nptel Programming in Modern C++ Assignment 12 Answers
Q7. Consider the following program (in C++14).
Identify the statement/s that is/are not true about the program.
a) It generates output as :
0 1 2 3 5 6 7 8
b) It generates output as :
5 6 7 8 0 1 2 3
c) It generates output as :
5 0 6 1 7 2 8 3
d) It generates output as :
0 1 2 3
Answer: b, c, d
Q8. Consider the following program (in C++11)
Identify the statement/s that is/are not true about the program.
a) It generates output as :
R1 : 10 10
R2 : 30 30
b) It generates output as :
R1 : 10 10
R2 : 20 20
c) It generates output as :
R2 : 20 20
R1 : 30 30
d) It results in deadlock
Answer: b, c, d
These are Nptel Programming in Modern C++ Assignment 12 Answers
Q9. Consider the following code segment (in C++11).
Choose the appropriate option to fill in the blanks at LINE-1 and LINE-2 so that the output becomes 100 400 900 1600.
a) LINE-1 : p1.set_value (v)
LINE-2 : auto rv = f1.get()
b) LINE-1 : p1.set_value (v)
LINE-2 : auto rv = f2.get()
c) LINE-1 : p2.set_value (v)
LINE-2 : auto rv = f1.get()
d) LINE-1 : p2.set_value (v)
LINE-2 : auto rv = f2.get()
Answer: b) LINE-1 : p1.set_value (v)
LINE-2 : auto rv = f2.get()
Programming Assignment of Programming in Modern C++
Question 1
Consider the program below (in C++11), which implements a smart pointer.
• Fill in the blank at LINE-1 with appropriate header and initializer list for the copy constrcutor.
• Fill in the blank at LINE-2 with appropriate header to overload dereferenceing operator.
• Fill in the blank at LINE-3 with appropriate header to overload indirection operator.
The program must satisfy the given test cases.
Solution:
template <typename T>
class SmartPtr {
public:
explicit SmartPtr(T* pointee) : pointee_(pointee) { }
SmartPtr(const SmartPtr& other) : pointee_(other.pointee_) { }
~SmartPtr() { if(pointee_ != nullptr) delete pointee_; }
T& operator*() { return *pointee_; }
T* operator->() { return pointee_; }
private:
T* pointee_;
};
These are Nptel Programming in Modern C++ Assignment 12 Answers
Question 2
Consider the following program in C++14 to represent a doubly linked list (of generic type), which allows adding items at the front of the list. Complete the program as per the instructions given below.
• Fill in the blank at LINE-1 with appropriate statements to traverse the list in forward direction.
• fill in the blank at LINE-2 with appropriate statements to traverse the list in backward direction,
The program must satisfy the sample input and output.
Solution:
void biTraverse()
{
std::shared_ptr<node<T>> t = first;
std::shared_ptr<node<T>> p = last;
while (t != nullptr)
{
std::cout << t->info << " ";
t = t->next;
}
std::cout << std::endl;
while (p != nullptr)
{
std::cout << p->info << " ";
p = p->prev.lock();
}
}
private:
std::shared_ptr<node<T>> first = nullptr;
std::shared_ptr<node<T>> last = nullptr;
};
*Sorry can’t able to pass private cases please try to do it yourself.
These are Nptel Programming in Modern C++ Assignment 12 Answers
Question 3
Consider the following program (in C++11).
• Fill in the blank at LINE-1 by defining a mutex object.
• Fill the blanks at LINE-2 and LINE-4 by locking the mutex object.
• Fill the blanks at LINE-3 and LINE-5 by unlocking the mutex object.
The program must satisfy the sample input and output.
Solution:
#include <iostream>
#include <thread>
#include <functional>
#include <chrono>
#include <mutex>
std::mutex mtx; //LINE-1
class account {
public:
account() = default;
void deposit(double amount){
mtx.lock(); //LINE-2
update_amount = amount;
int delay = (int)((double)std::rand() / (double)(RAND_MAX)* 10);
std::this_thread::sleep_for(std::chrono::milliseconds(delay));
balance_ += update_amount;
mtx.unlock(); //LINE-3
}
void withdrawal(double amount){
mtx.lock(); //LINE-4
update_amount = amount;
int delay = (int)((double)std::rand() / (double)(RAND_MAX)* 10);
std::this_thread::sleep_for(std::chrono::milliseconds(delay));
balance_ -= update_amount;
mtx.unlock(); //LINE-5
}
These are Nptel Programming in Modern C++ Assignment 12 Answers
These are Nptel Programming in Modern C++ Assignment 12 Answers
More Solutions of Programming in Modern C++: Click Here
More NPTEL Solutions: https://progiez.com/nptel/
