Programming in Modern C++ | Week 7
Course Name: Programming in Modern C++
Course Link: Click Here
These are Nptel Programming in Modern C++ Assignment 7 Answers
Quiz
Q1. Consider the following code segment.
Fill in the blank at LINE-1 and LINE-2 with the same statement such that the program will print Rajan : 5000.
a) const_cast (this)
b) static_cast (this)
c) dynamic_cast (this)
d) (employee*) (this)
Answer: a, d
Q2. Consider the following code segment.
What will be virtual function table (VFT) for the class C?
a) C::f(C* const)
B::g(B* const)
C::h(C* const)
B::i(B* const)
b) A::f(A* const)
B::g(B* const)
C::h(C* const)
B::i(B* const)
c) A::f(A* const)
B::g(B* const)
B::h(B* const)
C::i(C* const)
d) A::f(A* const)
B::g(B* const)
C::h(C* const)
C::i(C* const)
Answer: b) A::f(A* const)
B::g(B* const)
C::h(C* const)
B::i(B* const)
These are Nptel Programming in Modern C++ Assignment 7 Answers
Q3. Consider the following code segment.
Which line/s will give compilation error?
a) LINE-1
b) LINE-2
c) LINE-3
d) LINE-4
Answer: c, d
Q4. Consider the following code segment.
Which of the following type-casting is permissible?
a) t2 = static_cast< Test2* >(t1);
b) t2 = dynamic_cast< Test2* >(t1);
c) t2 = reinterpret_cast< Test2* >(t1);
d) t2 = const_cast< Test2* >(t1);
Answer: c) t2 = reinterpret_cast< Test2* >(t1);
These are Nptel Programming in Modern C++ Assignment 7 Answers
Q5. Consider the following code segment.
What will be the output?
a) 0101
b) 0111
c) 0110
d) 0010
Answer: a) 0101
Q6. Consider the following code segment.
What will be the output?
a) 0101
b) 0111
c) 0110
d) 0010
Answer: c) 0110
These are Nptel Programming in Modern C++ Assignment 7 Answers
Q7. Consider the following code segment.
Fill in the blank at LINE-1 so that the program will print 9.81.
a) const_cast< double* >
b) static_cast< double* >
c) dynamic_cast< double* >
d) (const doubles)
Answer: a) const_cast< double* >
Q8. Consider the following code segment.
Fill in the blank at LINE-1 so that the program will print “C++”.
a) reinterpret._cast< C2* >
b) static_cast< C2* >
c) dynamic_cast< C2* >
d) (C2%)
Answer: a, d
These are Nptel Programming in Modern C++ Assignment 7 Answers
Q9. Consider the following code segment below.
How many virtual function table (VFT) will be created?
a) 1
b) 2
c) 3
d) 4
Answer: b) 2
Programming Assignment of Programming in Modern C++
Question 1
Complete the program with the following instructions.
• Fill in the blank at LINE-1 to complete operator overloading for assignment operator.
• Fill in the blanks at LINE-2 and LINE-3 to complete the type casting statements.
The program must satisfy the given test cases.
Solution:
void operator=(int x)
{
b = b + x;
}
};
void fun(const Test1 &t, int x)
{
Test1 &u = const_cast<Test1&>(t);
u.show();
Test2 &v = reinterpret_cast<Test2&>(u);
These are Nptel Programming in Modern C++ Assignment 7 Answers
Question 2
Consider the following program with the following instructions.
• Fill in the blank at LINE-1 to complete constructor definition.
• Fill in the blank at LINE-2 to complete assignment operator overload function signature.
• Fill in the blank at LINE-3 to complete integer cast operator overload function signature.
The program must satisfy the sample input and output.
Solution:
#include<iostream>
using namespace std;
class intString{
int *arr;
int n;
mutable int index;
public:
intString(int k):n(k),arr(new int[k]),index(k-1){}
operator int()const
{
return arr[index--];
}
intString& operator=(int &k){
These are Nptel Programming in Modern C++ Assignment 7 Answers
Question 3
Consider the following program. Fill in the blanks as per the instructions given below:
• at LINE-1 to complete the constructor definition,
• at LINE-2 to complete the function header to overload operator for type-casting to char,
• at LINE-3 to complete the function header to overload operator for type-casting to int,
such that it will satisfy the given test cases.
Solution:
#include<iostream>
#include<cctype>
using namespace std;
class classChar
{
char ch;
public:
classChar(char _ch) : ch(tolower(_ch)){}
operator char() const
{
return ch;
}
operator int() const
{
return ch - 'a' + 1;
}
These are Nptel Programming in Modern C++ Assignment 7 Answers
* The material and content uploaded on this website are for general information and reference purposes only. Please do it by your own first. COPYING MATERIALS IS STRICTLY PROHIBITED.
These are Nptel Programming in Modern C++ Assignment 7 Answers
More Solutions of Programming in Modern C++: Click Here
More NPTEL Solutions: https://progiez.com/nptel/
More from PROGIEZ
