Assignment 8
Assignment 8
Total Marks : 20
September 9, 2017
Question 1
Consider the following code segment. Mark 2
catch (int i) {
cout << "CaughtOne ";
}
b) CaughtOne CaughtString
c) CaughtOne CaughtOne
Question 2
Consider the following Template. Mark 2
1
template <class T>
T Comp(T x, T y) {
return x + y;
}
a) 8 and 5
b) 8.1 and 5
d) 8 and 5.7
Answer: d)
Explanation: a is declared as int hence 3.1 will gets converted to 3 implicitly. Further,
iC is declared as int, the result will be integer (8). The result will be double (5.7) for the
call dC = Comp(c, d) since all variables are declared as double.
Question 3
Consider the following code segment. Assume that the sizeof(double)= 8. What will be the
size of the object derived ? Mark 2
class base {
double arr[5];
};
a) 40
b) 20
c) 80
d) 88
Answer: c)
Explanation: Class derived ISA a base1 and base2 and hence inherits all its data and
function members. But, memory will be allocated only for data members. Hence, the total
memory will be 80 (5 (array elements) * 8 (double size) + 5 (array elements) * 8 (double
size))
2
Question 4
Consider the following Class Template. Mark 2
a) a,b
a,12.9
b) a,b
97,12
c) a,b
a,12
d) 97,b
97,12
Answer: b)
Explanation: Default arguments to a class template. Since by default, arguments are int
to the template. When we make a statement Test<>c(’a’, 12.9); without any explicit data
types within <>, all arguments will be converted to int type.
Question 5
What will be the output of the following program? Mark 2
#include <iostream>
using namespace std;
class Test {
public:
Test() { cout << "Created" << endl; }
~Test() { cout << "Destroyed " << endl; }
};
int main() {
try {
Test t1;
throw 98;
}
catch(char i) {
3
cout << "Caught Char " << i << endl;
}
catch(double i) {
cout << "Caught Double " << i << endl;
}
catch(...) {
cout << "Default" << endl;
}
return 0;
}
a) Created
Destroyed
Caught Char b
Caught Double 98
b) Created
Caught Char b
Caught Double 98
c) Created
Caught Double 98
d) Created
Destroyed
Default
Answer: d)
Explanation: Object instantiation calls the constructor and then calls the destructor when
the scope ends. Since 98 is neither char nor double, catch(...) will be called. Refer course
materials for more details on exception handling.
Programming Assignment
Question 1
Fill up the missing code segment by following the comments below such that output matches
the test cases. Marks 3
#include <iostream>
using namespace std;
/* Write the function header and body of display() which takes single generic parameter
and prints it’s value */
4
/* Write overladed display() which takes two generic parameters and prints the values */
int main() {
double d;
int i;
char c;
cin >> i;
cin >> d;
cin >> c;
display(c);
display(i, d);
display(c, d);
return 0;
}
Answer:
a. Input:
10 12.2 c
Output:
c
10 12.2
c 12.2
b. Input:
5
8 8 a
Output:
a
8 8
a 8
c. Input:
2 10.8 g
Output:
g
2 10.8
g 10.8
Question 2
Consider the following code. Write the correct swap function to match the test cases. Marks 3
#include <iostream>
#include <string>
using namespace std;
// --------------------------------------------------------------------
int main() {
int a, b;
double s, t;
string mr, ms;
Swap(a, b);
Swap(s, t);
Swap(mr, ms);
return 0;
}
6
Answer:
Output: 2 5
3.3 11.66
wel come
Output: 30 20
3.3 2.2
world hello
Output: 15 9
88.8 77.7
c++ program
Question 3
Consider the following code. Write the correct Min function which returns the minimum of
two values to match the test cases. Marks 2
#include <iostream>
using namespace std;
// --------------------------------------------------------------------
int main() {
int a, b, iMin;
cin >> a >> b;
double c, d, dMin;
cin >> c >> d;
7
iMin = Min(a, b);
cout << "Min(" << a << ", " << b << ") = " << iMin << endl;
dMin = Min(c, d);
cout << "Min(" << c << ", " << d << ") = " << dMin << endl;
return 0;
}
Answer:
template<class T>
T Min(T x, T y) {
return x < y ? x : y;
}
Explanation: Use template to find the min. of two values that are of two different type.
a. Input:
23
-2.2 3.3
Output:
Min(2, 3) = 2
Min(-2.2, 3.3) = -2.2
b. Input:
80
0.0 3.0
Output:
Min(8, 0) = 0
Min(0, 3) = 0
c. Input:
-2 -10
-8 8
Output:
Min(-2, -10) = -10
Min(-8, 8) = -8
8
Question 4
Consider the following code. Insert the code for error handling using exception, in editable
section to match the test cases. Marks 2
#include <iostream>
#include <exception>
using namespace std;
class DivideByZero {
public:
int numerator, denominator;
DivideByZero(int a = 0, int b = 0) : numerator(a), denominator(b) {}
int divide(int numerator, int denominator){
if (denominator == 0) {
____________ // Call exception suitably to handle divide by zero error
}
return (numerator / denominator);
}
};
int main() {
DivideByZero d;
int a, b;
cin >> a >> b;
try {
d.divide(a, b);
}
catch (exception& e) {
cout << e.what() << endl;
}
return 0;
}
Answer
Input:
9
20 0
Output: DivideByZero
Input:
3.3 0
Output: DivideByZero
Input:
10 0
Output: DivideByZero
10