Programming in C++: Assignment Week 8

Similar documents
Programming in C++: Assignment Week 8

Programming in C++: Programming Test-2

21. Exceptions. Advanced Concepts: // exceptions #include <iostream> using namespace std;

Programming in C++: Programming Test-1

C++ Exception Handling 1

CPSC 427: Object-Oriented Programming

Homework 4. Any questions?

Programming in C++: Assignment Week 1

CSC 330 Object-Oriented Programming. Exception Handling CSC 330

Exception Handling Pearson Education, Inc. All rights reserved.

C++ TEMPLATES. Templates are the foundation of generic programming, which involves writing code in a way that is independent of any particular type.

C++ Namespaces, Exceptions

C++ Exception Handling. Dr. Md. Humayun Kabir CSE Department, BUET

TEMPLATES AND EXCEPTION HANDLING

Exercise 1.1 Hello world

Programming in C++: Assignment Week 5

The format for declaring function templates with type parameters

IS 0020 Program Design and Software Tools

Programming in C++: Assignment Week 6

G52CPP C++ Programming Lecture 16

CS102 C++ Exception Handling & Namespaces

2. How many runtime error messages associated with exception? a) 2 b) 4 c) 5 d) infinite

CMPS 221 Sample Final

Exceptions, Case Study-Exception handling in C++.

C++ PROGRAMMING LANGUAGE: DYNAMIC MEMORY ALLOCATION AND EXCEPTION IN C++. CAAM 519, CHAPTER 15

05-01 Discussion Notes

Introduction to Programming

CPSC 427: Object-Oriented Programming

Midterm Review. PIC 10B Spring 2018

! Errors can be dealt with at place error occurs

Intermediate Programming, Spring 2017*

CS11 Introduction to C++ Fall Lecture 6

Programming in C++: Assignment Week 6

! An exception is a condition that occurs at execution time and makes normal continuation of the program impossible.

More Functions. Pass by Value. Example: Exchange two numbers. Storage Classes. Passing Parameters by Reference. Pass by value and by reference

Programming in C++: Assignment Week 3

Programming in C++: Assignment Week 3

Midterm Exam 5 April 20, 2015

Abstract Data Types (ADTs) 1. Legal Values. Client Code for Rational ADT. ADT Design. CS 247: Software Engineering Principles

C++_ MARKS 40 MIN

Lecture 10. To use try, throw, and catch Constructors and destructors Standard exception hierarchy new failures

Arrays. Week 4. Assylbek Jumagaliyev

C and C++ 7. Exceptions Templates. Alan Mycroft

Outline. Introduction. Arrays declarations and initialization. Const variables. Character arrays. Static arrays. Examples.

CS 247: Software Engineering Principles. ADT Design

EE 152 Advanced Programming LAB 7

Exception with arguments

Unit 4 Basic Collections

Exceptions, Templates, and the STL

C++ Programming Assignment 3

Lecture 8. Exceptions, Constructor, Templates TDDD86: DALP. Content. Contents Exceptions

Advanced C++ 4/13/2017. The user. Types of users. Const correctness. Const declaration. This pointer and const.

UEE1303(1070) S12: Object-Oriented Programming Advanced Topics of Class

Before we dive in. Preprocessing Compilation Linkage

Generics Concurrency Exceptions (examples in C++, Java, & Ada)

CS 115 Exam 3, Spring 2010

Exception Handling in C++

CSS 342 Data Structures, Algorithms, and Discrete Mathematics I. Lecture 2. Yusuf Pisan

CSCE 110 PROGRAMMING FUNDAMENTALS

TEMPLATE IN C++ Function Templates

Functions and Recursion

CHAPTER 1.2 INTRODUCTION TO C++ PROGRAMMING. Dr. Shady Yehia Elmashad

CHAPTER 4 FUNCTIONS. Dr. Shady Yehia Elmashad

A Tour of the C++ Programming Language

CS

COM S 213 PRELIM EXAMINATION #2 April 26, 2001

CS242 COMPUTER PROGRAMMING

Software Engineering Concepts: Invariants Silently Written & Called Functions Simple Class Example

The important features of Object Oriented programming are:

COMP6771 Advanced C++ Programming

Exam 1 is on Wednesday 10 / 3 / 2018

CSCI 102L - Data Structures Midterm Exam #1 Fall 2011


Laboratorio di Tecnologie dell'informazione

UEE1303(1070) S12: Object-Oriented Programming Operator Overloading and Function Overloading

COSC 320 Exam 2 Key Spring Part 1: Hash Functions

Introduction. Common examples of exceptions

Intermediate Programming, Spring 2017*

Example:(problem) int a,b cin >> a cin >> b // b = 0

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures

CSE 333. Lecture 10 - references, const, classes. Hal Perkins Paul G. Allen School of Computer Science & Engineering University of Washington

CMSC 202 Midterm Exam 1 Fall 2015

CS 376b Computer Vision

struct Buffer { Buffer(int s) { buf = new char[s]; } ~Buffer() { delete [] buf; } char *buf; };

A Tour of the C++ Programming Language

PESIT Bangalore South Campus Hosur road, 1km before Electronic City, Bengaluru -100 Department of Electronics and Communication

UEE1303(1070) S12: Object-Oriented Programming Constructors and Destructors

AN OVERVIEW OF C++ 1

Chapter 16: Exceptions, Templates, and the Standard Template Library (STL)

I BCS-031 BACHELOR OF COMPUTER APPLICATIONS (BCA) (Revised) Term-End Examination. June, 2015 BCS-031 : PROGRAMMING IN C ++

Week 3: Pointers (Part 2)

Exceptions. Why Exception Handling?

Programming in C++: Assignment Week 4

Discussion 1E. Jie(Jay) Wang Week 10 Dec.2

Exam 2. CSI 201: Computer Science 1 Fall 2016 Professors: Shaun Ramsey and Kyle Wilson. Question Points Score Total: 80

Module 7 b. -Namespaces -Exceptions handling

Module 1. C++ Classes Exercises

CS250 Final Review Questions

int main() { int account = 100; // Pretend we have $100 in our account int withdrawal;

CSE143 Exam with answers MIDTERM #1, 1/26/2001 Problem numbering may differ from the test as given.

Transcription:

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