Programming in C++: Assignment Week 8 Total Marks : 20 Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology Kharagpur 721302 partha.p.das@gmail.com April 12, 2017 Question 1 What will be the output of the following program? Marks: 1 int main() try throw a ; catch (int x) cout << "Caught 1 " << x; catch (double x) cout << "Caught 2 " << x; catch (string x) cout << "Caught 3 " << x; catch (...) cout << "Default Exception"; a) Caught 1 a b) Caught 2 a c) Caught 3 a d) Default Exception Answer: d) Solution: No catch argument type matches the type of the thrown object. If the ellipsis (...) is used as the parameter of catch, then that handler can catch any exception no matter what the type of the exception thrown. This can be used as a default handler that catches all exceptions not caught by other handlers. 1
Question 2 Consider the following code snippet and find appropriate option to fill the blank. Marks: 2 T result; result = (a>b)? a : b; return (result); int main () int i = 5, j = 6, k; long l = 10, m = 5, n; k = GetMax<int>(i,j); n = GetMax<long>(l,m); cout << k << endl; cout << n << endl; a) int GetMax (int a, int b) b) template <class T>T GetMax (T a, T b) c) template <class T >class GetMax d) class GetMax Answer: b) Solution: By the definition of template Question 3 What will be the output of the following code snippet? Marks: 1 void myfunction(int test) try if (test) throw test; else throw "Value is zero"; catch (int i) cout << "CaughtOne " ; catch (const char *str) cout << "CaughtString "; 2
myfunction(1); myfunction(2); myfunction(0); myfunction(3); a) CaughtOne CaughtOne CaughtString CaughtString b) CaughtOne CaughtString CaughtString CaughtOne c) CaughtOne CaughtOne CaughtOne CaughtOne d) CaughtOne CaughtOne CaughtString CaughtOne Answer: d) Solution: Multiple handlers (i.e., catch expressions) can be chained; each one with a different parameter type. Only the handler whose argument type matches the type of the exception specified in the throw statement is executed. Question 4 What will be the output of the following code snippet? Marks: 1 #include<iostream> struct MyException : public exception const char * what () const throw () return "C++ Exception"; ; try throw MyException(); catch(myexception& e) std::cout << "MyException caught" << std::endl; std::cout << e.what() << std::endl; catch(std::exception& e) std::cout << "Exception caught" << std::endl; std::cout << e.what() << std::endl; a) MyException caught C++ Exception b) C++ Exception MyException caught c) Exception caught C++ Exception 3
d) C++ Exception MyException caught Exception caught Answer: a) Solution: Multiple handlers (i.e., catch expressions) can be chained; each one with a different parameter type. Only the handler whose argument type matches the type of the exception specified in the throw statement is executed. Question 5 What will the the output of the following code? Marks: 1 #include <iostream> class X public: class Trouble ; class Small : public Trouble ; class Big : public Trouble ; void f() throw Big(); ; X x; try x.f(); catch (X::Trouble&) cout << "caught Trouble" << endl; catch (X::Small&) cout << "caught Small Trouble" << endl; catch (X::Big&) cout << "caught Big Trouble" << endl; catch (...) cout << "default" << endl; a) caught Trouble b) caught Small Trouble c) caught Big Trouble d) default Answer: a) 4
Solution: Multiple handlers (i.e., catch expressions) can be chained; each one with a different parameter type. Only the handler whose argument type matches the type of the exception specified in the throw statement is executed. Question 6 What will be the output of the following program? MCQ, Marks 2 #include <iostream> class Test public: Test() cout << "In Constructor" << endl; ~Test() cout << "In Destructor " << endl; ; try Test t1; throw 10.00; catch(int i) cout << "Caught Integer " << i << endl; catch(...) cout << "Caught Default" << endl; a) In Constructor In destructor Caught Integer 10 Caught Default b) In Constructor Caught Integer 10 c) In Constructor In Destructor Caught Default d) In Constructor Caught Default Answer: c) Solution: Multiple handlers (i.e., catch expressions) can be chained; each one with a different parameter type. Only the handler whose argument type matches the type of the exception specified in the throw statement is executed. 5
Question 7 Fill in the blank in the following code to get the desired output. MCQ, Marks 2 #include <iostream> ----------------------- // Fill in the blank int arrmin(t arr[], int n) int m = max; for (int i = 0; i < n; i++) if (arr[i] < m) m = arr[i]; return m; int arr1[] = 10, 20, 15, 12; int n1 = sizeof(arr1)/sizeof(arr1[0]); char arr2[] = 1, 2, 3; int n2 = sizeof(arr2)/sizeof(arr2[0]); cout << arrmin<int, 10000>(arr1, n1) << endl; cout << arrmin<char, 256>(arr2, n2); ------------ Output: 10 1 a) template <T, int max> b) template <T>, int max c) template <class T>, int max d) template <class T, int max> Answer: d) Solution: By the definition of template 6
Programming Questions Question 1 Consider the following code. Modify the code in editable section to match the public test cases. Marks: 3 #include<iostream> #include<string> // Define Swap() function here int a, b; double s, t; string mr, ms; cin >> a >> b >> s >> t ; cin >> mr >> ms ; Swap(a, b); Swap(s, t); swap(mr, ms); cout << a << " " << b << " "; cout << s << " " << t << " "; cout << mr << " " << ms ; Public 1 I/P: 20 30 2.3 5.6 ppd tm O/P: 30 20 5.6 2.3 tm ppd Public 2 I/P: 45 34 7.9 5.6 kolkata delhi O/P: 34 45 5.6 7.9 delhi kolkata Private I/P: 15 78 4.76 2.45 sm ppm O/P: 78 15 2.45 4.76 ppm sm Answer template<typename T> void Swap(T& x, T& y) T tmp = x; x = y; y = tmp; 7
Question 2 Consider the following code.define the proper function in editable section. Marks: 2 #include <iostream> #include <exception> class myexception : public exception // Define the Proper function which will return a string "DivideByZero" myex; 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) throw myex; return numerator / denominator; ; DivideByZero d; int a, b; cin >> a >> b; try d.divide(a, b); catch (exception& e) cout << e.what() << \n ; Public-1 Input: 10 0 Output: DivideByZero Public-2 Input: 12 0 Output: DivideByZero 8
Private Input: 8 0 Output: DivideByZero 9
Answer virtual const char* what() const throw() return "DivideByZero"; Question 3 Consider the following code. Fill the blank with proper class header Marks: 2 #include<iostream> // Write the class header here class A public: T x; U y; A() cout << "called" << endl; A(T x, U y) cout << x << << y << endl; ; ; int num1 = 0; double num2 = 0; char c; cin >> num1; cin >> num2; cin >> c; A<char> a; A<char, int> (c, num1); A<int, double> (num1, num2); Public-1 Input: 2 3.4 n Output: called n 2 2 3.4 Public-2 Input: 5 5.4 i 10
Output: called i 5 5 5.4 Private Input: 3 3.4 p Output: called p 3 3 3.4 Answer template<class T, class U = int > class A public: T x; U y; A() cout << "called" << endl; A(T x, U y) cout << x << << y << endl; ; ; Question 4 Consider the following code. Modify the code in editable section to match the public test cases. Marks: 3 #include <iostream> #include <vector> class Test static int count; int id; public: Test(int id) count++; cout << count << ; if (count == id) throw id; ~Test() ; int Test::count = 0; int n, m = 0; cin >> n >> m; 11
// Declare testarray here try for (int i = 0; i < n; ++i) testarray.push_back(test(m)); // Write the catch here cout << "Caught " << i << endl; Public-1 Input: 6 5 Output: 1 2 3 4 5 Caught 5 Public-2 Input: 8 6 Output: 1 2 3 4 5 6 Caught 6 Private Input: 3 3 Output: 1 2 3 Caught 3 Answer catch (int i) vector<test> testarray; 12