Programming in C++: Assignment Week 8

Size: px
Start display at page:

Download "Programming in C++: Assignment Week 8"

Transcription

1 Programming in C++: Assignment Week 8 Total Marks : 20 Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology Kharagpur 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

2 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

3 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

4 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

5 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

6 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

7 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: ppd tm O/P: tm ppd Public 2 I/P: kolkata delhi O/P: delhi kolkata Private I/P: sm ppm O/P: ppm sm Answer template<typename T> void Swap(T& x, T& y) T tmp = x; x = y; y = tmp; 7

8 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

9 Private Input: 8 0 Output: DivideByZero 9

10 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: n Output: called n Public-2 Input: i 10

11 Output: called i Private Input: p Output: called p 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

12 // 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: Caught 5 Public-2 Input: 8 6 Output: Caught 6 Private Input: 3 3 Output: Caught 3 Answer catch (int i) vector<test> testarray; 12

Programming in C++: Assignment Week 8

Programming in C++: Assignment Week 8 Programming in C++: Assignment Week 8 Total Marks : 20 September 9, 2017 Question 1 Consider the following code segment. Mark 2 void myfunction(int test) { try { if (test) throw test; else throw "Value

More information

Programming in C++: Programming Test-2

Programming in C++: Programming Test-2 Programming in C++: Programming Test-2 Total Marks : 20 Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology Kharagpur 721302 partha.p.das@gmail.com April 20,

More information

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

21. Exceptions. Advanced Concepts: // exceptions #include <iostream> using namespace std; - 147 - Advanced Concepts: 21. Exceptions Exceptions provide a way to react to exceptional circumstances (like runtime errors) in our program by transferring control to special functions called handlers.

More information

Programming in C++: Programming Test-1

Programming in C++: Programming Test-1 Programming in C++: Programming Test-1 Total Marks : 20 Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology Kharagpur 721302 partha.p.das@gmail.com April 18,

More information

C++ Exception Handling 1

C++ Exception Handling 1 C++ Exception Handling 1 An exception is a problem that arises during the execution of a program. A C++ exception is a response to an exceptional circumstance that arises while a program is running, such

More information

CPSC 427: Object-Oriented Programming

CPSC 427: Object-Oriented Programming CPSC 427: Object-Oriented Programming Michael J. Fischer Lecture 19 November 7, 2018 CPSC 427, Lecture 19, November 7, 2018 1/18 Exceptions Thowing an Exception Catching an Exception CPSC 427, Lecture

More information

Homework 4. Any questions?

Homework 4. Any questions? CSE333 SECTION 8 Homework 4 Any questions? STL Standard Template Library Has many pre-build container classes STL containers store by value, not by reference Should try to use this as much as possible

More information

Programming in C++: Assignment Week 1

Programming in C++: Assignment Week 1 Programming in C++: Assignment Week 1 Total Marks : 20 Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology Kharagpur 721302 partha.p.das@gmail.com February 24,

More information

CSC 330 Object-Oriented Programming. Exception Handling CSC 330

CSC 330 Object-Oriented Programming. Exception Handling CSC 330 Object-Oriented Programming Exception Handling 1 C++ Exception Handling Topics Exception Handling C++ Exception Handling Basics Throwing and Catching Exceptions Constructors, Destructors and Exceptions

More information

Exception Handling Pearson Education, Inc. All rights reserved.

Exception Handling Pearson Education, Inc. All rights reserved. 1 16 Exception Handling 2 16.1 Introduction Exceptions Indicate problems that occur during a program s execution Occur infrequently Exception handling Can resolve exceptions Allow a program to continue

More information

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

C++ TEMPLATES. Templates are the foundation of generic programming, which involves writing code in a way that is independent of any particular type. C++ TEMPLATES http://www.tutorialspoint.com/cplusplus/cpp_templates.htm Copyright tutorialspoint.com Templates are the foundation of generic programming, which involves writing code in a way that is independent

More information

C++ Namespaces, Exceptions

C++ Namespaces, Exceptions C++ Namespaces, Exceptions CSci 588: Data Structures, Algorithms and Software Design http://www.cplusplus.com/doc/tutorial/namespaces/ http://www.cplusplus.com/doc/tutorial/exceptions/ http://www.cplusplus.com/doc/tutorial/typecasting/

More information

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

C++ Exception Handling. Dr. Md. Humayun Kabir CSE Department, BUET C++ Exception Handling Dr. Md. Humayun Kabir CSE Department, BUET Exception Handling 2 An exception is an unusual behavior of a program during its execution Exception can occur due to Wrong user input

More information

TEMPLATES AND EXCEPTION HANDLING

TEMPLATES AND EXCEPTION HANDLING CONTENTS: Function template Class template Exception handling Try-Catch-Throw paradigm Exception specification Terminate functions Unexcepted functions Uncaught exception UNIT-III TEMPLATES AND EXCEPTION

More information

Exercise 1.1 Hello world

Exercise 1.1 Hello world Exercise 1.1 Hello world The goal of this exercise is to verify that computer and compiler setup are functioning correctly. To verify that your setup runs fine, compile and run the hello world example

More information

Programming in C++: Assignment Week 5

Programming in C++: Assignment Week 5 Programming in C++: Assignment Week 5 Total Marks : 20 Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology Kharagpur 721302 partha.p.das@gmail.com April 3, 2017

More information

The format for declaring function templates with type parameters

The format for declaring function templates with type parameters UNIT 3 Function and class templates - Exception handling try-catch-throw paradigm exception specification terminate and Unexpected functions Uncaught exception. Templates Function templates Function templates

More information

IS 0020 Program Design and Software Tools

IS 0020 Program Design and Software Tools 1 Introduction 2 IS 0020 Program Design and Software Tools Exception Handling Lecture 12 November 23, 200 Exceptions Indicates problem occurred in program Not common An "exception" to a program that usually

More information

Programming in C++: Assignment Week 6

Programming in C++: Assignment Week 6 Programming in C++: Assignment Week 6 Total Marks : 20 Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology Kharagpur 721302 partha.p.das@gmail.com April 6, 2017

More information

G52CPP C++ Programming Lecture 16

G52CPP C++ Programming Lecture 16 G52CPP C++ Programming Lecture 16 Dr Jason Atkin http://www.cs.nott.ac.uk/~jaa/cpp/ g52cpp.html 1 Last Lecture Casting static cast dynamic cast const cast reinterpret cast Implicit type conversion 2 How

More information

CS102 C++ Exception Handling & Namespaces

CS102 C++ Exception Handling & Namespaces CS102 C++ Exception Handling & Namespaces Bill Cheng http://merlot.usc.edu/cs102-s12 1 Topics to cover C Structs (Ch 10) C++ Classes (Ch 11) Constructors Destructors Member functions Exception Handling

More information

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

2. How many runtime error messages associated with exception? a) 2 b) 4 c) 5 d) infinite 2. How many runtime error messages associated with exception? a) 2 b) 4 c) 5 d) infinite Answer:c Explanation:There are five runtime error messages associated with exceptions.they are no handler for the

More information

CMPS 221 Sample Final

CMPS 221 Sample Final Name: 1 CMPS 221 Sample Final 1. What is the purpose of having the parameter const int a[] as opposed to int a[] in a function declaration and definition? 2. What is the difference between cin.getline(str,

More information

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

Exceptions, Case Study-Exception handling in C++. PART III: Structuring of Computations- Structuring the computation, Expressions and statements, Conditional execution and iteration, Routines, Style issues: side effects and aliasing, Exceptions, Case

More information

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

C++ PROGRAMMING LANGUAGE: DYNAMIC MEMORY ALLOCATION AND EXCEPTION IN C++. CAAM 519, CHAPTER 15 C++ PROGRAMMING LANGUAGE: DYNAMIC MEMORY ALLOCATION AND EXCEPTION IN C++. CAAM 519, CHAPTER 15 This chapter introduces the notion of dynamic memory allocation of variables and objects in a C++ program.

More information

05-01 Discussion Notes

05-01 Discussion Notes 05-01 Discussion Notes PIC 10B Spring 2018 1 Exceptions 1.1 Introduction Exceptions are used to signify that a function is being used incorrectly. Once an exception is thrown, it is up to the programmer

More information

Introduction to Programming

Introduction to Programming Introduction to Programming session 6 Instructor: Reza Entezari-Maleki Email: entezari@ce.sharif.edu 1 Spring 2011 These slides are created using Deitel s slides Sharif University of Technology Outlines

More information

CPSC 427: Object-Oriented Programming

CPSC 427: Object-Oriented Programming CPSC 427: Object-Oriented Programming Michael J. Fischer Lecture 22 November 28, 2016 CPSC 427, Lecture 22 1/43 Exceptions (continued) Code Reuse Linear Containers Ordered Containers Multiple Inheritance

More information

Midterm Review. PIC 10B Spring 2018

Midterm Review. PIC 10B Spring 2018 Midterm Review PIC 10B Spring 2018 Q1 What is size t and when should it be used? A1 size t is an unsigned integer type used for indexing containers and holding the size of a container. It is guarenteed

More information

! Errors can be dealt with at place error occurs

! Errors can be dealt with at place error occurs UCLA Stat 1D Statistical Computing and Visualization in C++ Instructor: Ivo Dinov, Asst. Prof. in Statistics / Neurology University of California, Los Angeles, Winter 200 http://www.stat.ucla.edu/~dinov/courses_students.html

More information

Intermediate Programming, Spring 2017*

Intermediate Programming, Spring 2017* 600.120 Intermediate Programming, Spring 2017* Misha Kazhdan *Much of the code in these examples is not commented because it would otherwise not fit on the slides. This is bad coding practice in general

More information

CS11 Introduction to C++ Fall Lecture 6

CS11 Introduction to C++ Fall Lecture 6 CS11 Introduction to C++ Fall 2006-2007 Lecture 6 Today s Topics C++ exceptions Introduction to templates How To Report Errors? C style of error reporting: return values For C Standard Library/UNIX functions,

More information

Programming in C++: Assignment Week 6

Programming in C++: Assignment Week 6 Programming in C++: Assignment Week 6 Total Marks : 20 August 26, 2017 Question 1 class A { virtual void f(int) { virtual void g(double) { virtual void d(char) { int h(a *) { class B: public A { void f(int)

More information

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

! An exception is a condition that occurs at execution time and makes normal continuation of the program impossible. Exceptions! Exceptions are used to signal error or unexpected events that occur while a program is running.! An exception is a condition that occurs at execution time and makes normal continuation of the

More information

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

More Functions. Pass by Value. Example: Exchange two numbers. Storage Classes. Passing Parameters by Reference. Pass by value and by reference Pass by Value More Functions Different location in memory Changes to the parameters inside the function body have no effect outside of the function. 2 Passing Parameters by Reference Example: Exchange

More information

Programming in C++: Assignment Week 3

Programming in C++: Assignment Week 3 Programming in C++: Assignment Week 3 Total Marks : 20 August 6, 2017 Question 1 What is the output of the sizeof operator for t in the following code snippet? sizeof(int) = 4) Mark 1 (Assume #include

More information

Programming in C++: Assignment Week 3

Programming in C++: Assignment Week 3 Programming in C++: Assignment Week 3 Total Marks : 20 Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology Kharagpur 721302 partha.p.das@gmail.com March 17,

More information

Midterm Exam 5 April 20, 2015

Midterm Exam 5 April 20, 2015 Midterm Exam 5 April 20, 2015 Name: Section 1: Multiple Choice Questions (24 pts total, 3 pts each) Q1: Which of the following is not a kind of inheritance in C++? a. public. b. private. c. static. d.

More information

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

Abstract Data Types (ADTs) 1. Legal Values. Client Code for Rational ADT. ADT Design. CS 247: Software Engineering Principles Abstract Data Types (ADTs) CS 247: Software Engineering Principles ADT Design An abstract data type (ADT) is a user-defined type that bundles together: the range of values that variables of that type can

More information

C++_ MARKS 40 MIN

C++_ MARKS 40 MIN C++_16.9.2018 40 MARKS 40 MIN https://tinyurl.com/ya62ayzs 1) Declaration of a pointer more than once may cause A. Error B. Abort C. Trap D. Null 2Whice is not a correct variable type in C++? A. float

More information

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

Lecture 10. To use try, throw, and catch Constructors and destructors Standard exception hierarchy new failures Lecture 10 Class string Exception Handling To use try, throw, and catch Constructors and destructors Standard exception hierarchy new failures Class template auto_ptr Lec 10 Programming in C++ 1 Class

More information

Arrays. Week 4. Assylbek Jumagaliyev

Arrays. Week 4. Assylbek Jumagaliyev Arrays Week 4 Assylbek Jumagaliyev a.jumagaliyev@iitu.kz Introduction Arrays Structures of related data items Static entity (same size throughout program) A few types Pointer-based arrays (C-like) Arrays

More information

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

C and C++ 7. Exceptions Templates. Alan Mycroft C and C++ 7. Exceptions Templates Alan Mycroft University of Cambridge (heavily based on previous years notes thanks to Alastair Beresford and Andrew Moore) Michaelmas Term 2013 2014 1 / 20 Exceptions

More information

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

Outline. Introduction. Arrays declarations and initialization. Const variables. Character arrays. Static arrays. Examples. Outline Introduction. Arrays declarations and initialization. Const variables. Character arrays. Static arrays. Examples. 1 Arrays I Array One type of data structures. Consecutive group of memory locations

More information

CS 247: Software Engineering Principles. ADT Design

CS 247: Software Engineering Principles. ADT Design CS 247: Software Engineering Principles ADT Design Readings: Eckel, Vol. 1 Ch. 7 Function Overloading & Default Arguments Ch. 12 Operator Overloading U Waterloo CS247 (Spring 2017) p.1/17 Abstract Data

More information

EE 152 Advanced Programming LAB 7

EE 152 Advanced Programming LAB 7 EE 152 Advanced Programming LAB 7 1) Create a class called Rational for performing arithmetic with fractions. Write a program to test your class. Use integer variables to represent the private data of

More information

Exception with arguments

Exception with arguments Exception Handling Introduction : Fundamental Syntax for Exception Handling code : try catch throw Multiple exceptions Exception with arguments Introduction Exception: An abnormal condition that arises

More information

Unit 4 Basic Collections

Unit 4 Basic Collections Unit 4 Basic Collections General Concepts Templates Exceptions Iterators Collection (or Container) Classes Vectors (or Arrays) Sets Lists Maps or Tables C++ Standard Template Library (STL Overview A program

More information

Exceptions, Templates, and the STL

Exceptions, Templates, and the STL Exceptions, Templates, and the STL CSE100 Principles of Programming with C++, Fall 2018 (based off Chapter 16 slides by Pearson) Ryan Dougherty Arizona State University http://www.public.asu.edu/~redoughe/

More information

C++ Programming Assignment 3

C++ Programming Assignment 3 C++ Programming Assignment 3 Author: Ruimin Zhao Module: EEE 102 Lecturer: Date: Fei.Xue April/19/2015 Contents Contents ii 1 Question 1 1 1.1 Specification.................................... 1 1.2 Analysis......................................

More information

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

Lecture 8. Exceptions, Constructor, Templates TDDD86: DALP. Content. Contents Exceptions Lecture 8 Exceptions, Constructor, Templates TDDD86: DALP Utskriftsversion av Lecture in Data Structures, Algorithms and Programming Paradigms 19th September 2017 Ahmed Rezine, IDA, Linköping University

More information

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

Advanced C++ 4/13/2017. The user. Types of users. Const correctness. Const declaration. This pointer and const. The user. Advanced C++ For : COP 3330. Object oriented Programming (Using C++) http://www.compgeom.com/~piyush/teach/3330 #define private public #define protected public #define class struct Source: Lutz

More information

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

UEE1303(1070) S12: Object-Oriented Programming Advanced Topics of Class UEE1303(1070) S12: Object-Oriented Programming Advanced Topics of Class What you will learn from Lab 6 In this laboratory, you will learn the advance topics of object-oriented programming using class.

More information

Before we dive in. Preprocessing Compilation Linkage

Before we dive in. Preprocessing Compilation Linkage Templates Before we dive in Preprocessing Compilation Linkage 2 Motivation A useful routine to have is void swap( int& a, int &b ) int tmp = a; a = b; b = tmp; 3 Example What happens if we want to swap

More information

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

Generics Concurrency Exceptions (examples in C++, Java, & Ada) Generics Concurrency Exceptions (examples in C++, Java, & Ada) Generic Programming Definition (WikiPedia): Generic programming is a style of computer programming in which algorithms are written in terms

More information

CS 115 Exam 3, Spring 2010

CS 115 Exam 3, Spring 2010 Your name: Rules You must briefly explain your answers to receive partial credit. When a snippet of code is given to you, you can assume o that the code is enclosed within some function, even if no function

More information

Exception Handling in C++

Exception Handling in C++ Exception Handling in C++ Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2 nd edition, by Kernighan and Ritchie, Absolute C++, by

More information

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

CSS 342 Data Structures, Algorithms, and Discrete Mathematics I. Lecture 2. Yusuf Pisan CSS 342 Data Structures, Algorithms, and Discrete Mathematics I Lecture 2 Yusuf Pisan Compiled helloworld yet? Overview C++ fundamentals Assignment-1 CSS Linux Cluster - submitting assignment Call by Value,

More information

CSCE 110 PROGRAMMING FUNDAMENTALS

CSCE 110 PROGRAMMING FUNDAMENTALS CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 15. Dictionaries (1): A Key Table Class Prof. amr Goneid, AUC 1 Dictionaries(1): A Key Table Class Prof. Amr Goneid, AUC 2 A Key Table

More information

TEMPLATE IN C++ Function Templates

TEMPLATE IN C++ Function Templates TEMPLATE IN C++ Templates are powerful features of C++ which allows you to write generic programs. In simple terms, you can create a single function or a class to work with different data types using templates.

More information

Functions and Recursion

Functions and Recursion Functions and Recursion 1 Storage Classes Scope Rules Functions with Empty Parameter Lists Inline Functions References and Reference Parameters Default Arguments Unary Scope Resolution Operator Function

More information

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

CHAPTER 1.2 INTRODUCTION TO C++ PROGRAMMING. Dr. Shady Yehia Elmashad CHAPTER 1.2 INTRODUCTION TO C++ PROGRAMMING Dr. Shady Yehia Elmashad Outline 1. Introduction to C++ Programming 2. Comment 3. Variables and Constants 4. Basic C++ Data Types 5. Simple Program: Printing

More information

CHAPTER 4 FUNCTIONS. Dr. Shady Yehia Elmashad

CHAPTER 4 FUNCTIONS. Dr. Shady Yehia Elmashad CHAPTER 4 FUNCTIONS Dr. Shady Yehia Elmashad Outline 1. Introduction 2. Program Components in C++ 3. Math Library Functions 4. Functions 5. Function Definitions 6. Function Prototypes 7. Header Files 8.

More information

A Tour of the C++ Programming Language

A Tour of the C++ Programming Language A Tour of the C++ Programming Language We already know C Everything that can be done with a computer, can be done in C Why should we learn another language? Newer languages provide a bigger toolbox Some

More information

CS

CS CS 1666 www.cs.pitt.edu/~nlf4/cs1666/ Programming in C++ First, some praise for C++ "It certainly has its good points. But by and large I think it s a bad language. It does a lot of things half well and

More information

COM S 213 PRELIM EXAMINATION #2 April 26, 2001

COM S 213 PRELIM EXAMINATION #2 April 26, 2001 COM S 213 PRELIM EXAMINATION #2 April 26, 2001 Name: Student ID: Please answer all questions in the space(s) provided. Each question is worth 4 points. You may leave when you are finished with the exam.

More information

CS242 COMPUTER PROGRAMMING

CS242 COMPUTER PROGRAMMING CS242 COMPUTER PROGRAMMING I.Safa a Alawneh Variables Outline 2 Data Type C++ Built-in Data Types o o o o bool Data Type char Data Type int Data Type Floating-Point Data Types Variable Declaration Initializing

More information

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

Software Engineering Concepts: Invariants Silently Written & Called Functions Simple Class Example Software Engineering Concepts: Invariants Silently Written & Called Functions Simple Class Example CS 311 Data Structures and Algorithms Lecture Slides Friday, September 11, 2009 continued Glenn G. Chappell

More information

The important features of Object Oriented programming are:

The important features of Object Oriented programming are: Q.2.a. Briefly explain the features of OOP. Ans. The important features of Object Oriented programming are: 1. Inheritance: Inheritance as the name suggests is the concept of inheriting or deriving properties

More information

COMP6771 Advanced C++ Programming

COMP6771 Advanced C++ Programming 1.... COMP6771 Advanced C++ Programming Week 5 Part One: Exception Handling 2016 www.cse.unsw.edu.au/ cs6771 2.... Memory Management & Exception Handling.1 Part I: Exception Handling Exception objects

More information

Exam 1 is on Wednesday 10 / 3 / 2018

Exam 1 is on Wednesday 10 / 3 / 2018 Exam 1 is on Wednesday 10 / 3 / 2018 1) True/False : Indicate whether the statement is true or false. _ 1. A class is an example of a structured data type. _ 2. In C++, class is a reserved word and it

More information

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

CSCI 102L - Data Structures Midterm Exam #1 Fall 2011 Print Your Name: Page 1 of 8 Signature: Aludra Loginname: CSCI 102L - Data Structures Midterm Exam #1 Fall 2011 (10:00am - 11:12am, Wednesday, October 5) Instructor: Bill Cheng Problems Problem #1 (24

More information

POINTERS - Pointer is a variable that holds a memory address of another variable of same type. - It supports dynamic allocation routines. - It can improve the efficiency of certain routines. C++ Memory

More information

Laboratorio di Tecnologie dell'informazione

Laboratorio di Tecnologie dell'informazione Laboratorio di Tecnologie dell'informazione Ing. Marco Bertini marco.bertini@unifi.it http://www.micc.unifi.it/bertini/ Exceptions What are exceptions? Exceptions are a mechanism for handling an error

More information

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

UEE1303(1070) S12: Object-Oriented Programming Operator Overloading and Function Overloading UEE1303(1070) S12: Object-Oriented Programming Operator Overloading and Function Overloading What you will learn from Lab 7 In this laboratory, you will learn how to use operator overloading and function

More information

COSC 320 Exam 2 Key Spring Part 1: Hash Functions

COSC 320 Exam 2 Key Spring Part 1: Hash Functions COSC 320 Exam 2 Key Spring 2011 Part 1: Hash s 1. (5 Points) Create the templated function object lessthan, you may assume that the templated data type T has overloaded the < operator. template

More information

Introduction. Common examples of exceptions

Introduction. Common examples of exceptions Exception Handling Introduction Common examples of exceptions Failure of new to obtain memory Out-of-bounds array subscript Division by zero Invalid function parameters Programs with exception handling

More information

Intermediate Programming, Spring 2017*

Intermediate Programming, Spring 2017* 600.120 Intermediate Programming, Spring 2017* Misha Kazhdan *Much of the code in these examples is not commented because it would otherwise not fit on the slides. This is bad coding practice in general

More information

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

Example:(problem) int a,b cin >> a cin >> b // b = 0 Exceptions Exceptions are unexpected events that occur during the execution of a program. An exception can be the result of an error condition or simply an unanticipated input. Exceptions can be thought

More information

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

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures The main body and cout Agenda 1 Fundamental data types Declarations and definitions Control structures References, pass-by-value vs pass-by-references The main body and cout 2 C++ IS AN OO EXTENSION OF

More information

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

CSE 333. Lecture 10 - references, const, classes. Hal Perkins Paul G. Allen School of Computer Science & Engineering University of Washington CSE 333 Lecture 10 - references, const, classes Hal Perkins Paul G. Allen School of Computer Science & Engineering University of Washington Administrivia New C++ exercise out today, due Friday morning

More information

CMSC 202 Midterm Exam 1 Fall 2015

CMSC 202 Midterm Exam 1 Fall 2015 1. (15 points) There are six logic or syntax errors in the following program; find five of them. Circle each of the five errors you find and write the line number and correction in the space provided below.

More information

CS 376b Computer Vision

CS 376b Computer Vision CS 376b Computer Vision 09 / 25 / 2014 Instructor: Michael Eckmann Today s Topics Questions? / Comments? Enhancing images / masks Cross correlation Convolution C++ Cross-correlation Cross-correlation involves

More information

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

struct Buffer { Buffer(int s) { buf = new char[s]; } ~Buffer() { delete [] buf; } char *buf; }; struct Buffer { Buffer(int s) { buf = new char[s]; ~Buffer() { delete [] buf; char *buf; ; struct FBuffer : public Buffer { FBuffer(int s) : Buffer(s) { f = fopen("file", "w"); ~FBuffer() { fclose(f);

More information

A Tour of the C++ Programming Language

A Tour of the C++ Programming Language A Tour of the C++ Programming Language We already know C Everything that can be done with a computer, can be done in C Why should we learn another language? Newer languages provide a bigger toolbox Some

More information

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

PESIT Bangalore South Campus Hosur road, 1km before Electronic City, Bengaluru -100 Department of Electronics and Communication INTERNAL ASSESSMENT TEST 1 Date : 28.2.2018 Marks: 40 Subject & Code : DataStructures Using C++ (15EC661) Sem : VI A,B,C Name of faculty : Vandana M L Time : 11:30-1:00 PM Note: Answer FIVE full questions,

More information

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

UEE1303(1070) S12: Object-Oriented Programming Constructors and Destructors UEE1303(1070) S12: Object-Oriented Programming Constructors and Destructors What you will learn from Lab 5 In this laboratory, you will learn how to use constructor and copy constructor to create an object

More information

AN OVERVIEW OF C++ 1

AN OVERVIEW OF C++ 1 AN OVERVIEW OF C++ 1 OBJECTIVES Introduction What is object-oriented programming? Two versions of C++ C++ console I/O C++ comments Classes: A first look Some differences between C and C++ Introducing function

More information

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

Chapter 16: Exceptions, Templates, and the Standard Template Library (STL) Chapter 16: Exceptions, Templates, and the Standard Template Library (STL) 6.1 Exceptions Exceptions Indicate that something unexpected has occurred or been detected Allow program to deal with the problem

More information

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

I BCS-031 BACHELOR OF COMPUTER APPLICATIONS (BCA) (Revised) Term-End Examination. June, 2015 BCS-031 : PROGRAMMING IN C ++ No. of Printed Pages : 3 I BCS-031 BACHELOR OF COMPUTER APPLICATIONS (BCA) (Revised) Term-End Examination 05723. June, 2015 BCS-031 : PROGRAMMING IN C ++ Time : 3 hours Maximum Marks : 100 (Weightage 75%)

More information

Week 3: Pointers (Part 2)

Week 3: Pointers (Part 2) Advanced Programming (BETC 1353) Week 3: Pointers (Part 2) Dr. Abdul Kadir abdulkadir@utem.edu.my Learning Outcomes: Able to describe the concept of pointer expression and pointer arithmetic Able to explain

More information

Exceptions. Why Exception Handling?

Exceptions. Why Exception Handling? Exceptions An exception is an object that stores information transmitted outside the normal return sequence. It is propagated back through calling stack until some function catches it. If no calling function

More information

Programming in C++: Assignment Week 4

Programming in C++: Assignment Week 4 Programming in C++: Assignment Week 4 Total Marks : 20 March 22, 2017 Question 1 Using friend operator function, which set of operators can be overloaded? Mark 1 a.,, , ==, = b. +, -, /, * c. =,

More information

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

Discussion 1E. Jie(Jay) Wang Week 10 Dec.2 Discussion 1E Jie(Jay) Wang Week 10 Dec.2 Outline Dynamic memory allocation Class Final Review Dynamic Allocation of Memory Recall int len = 100; double arr[len]; // error! What if I need to compute the

More information

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

Exam 2. CSI 201: Computer Science 1 Fall 2016 Professors: Shaun Ramsey and Kyle Wilson. Question Points Score Total: 80 Exam 2 CSI 201: Computer Science 1 Fall 2016 Professors: Shaun Ramsey and Kyle Wilson Question Points Score 1 18 2 29 3 18 4 15 Total: 80 I understand that this exam is closed book and closed note and

More information

Module 7 b. -Namespaces -Exceptions handling

Module 7 b. -Namespaces -Exceptions handling Module 7 b -Namespaces -Exceptions handling C++ Namespace Often, a solution to a problem will have groups of related classes and other declarations, such as functions, types, and constants. C++provides

More information

Module 1. C++ Classes Exercises

Module 1. C++ Classes Exercises Module 1. C++ Classes Exercises 1. The ZooAnimal class definition below is missing a prototype for the Create function. It should have parameters so that a character string and three integer values (in

More information

CS250 Final Review Questions

CS250 Final Review Questions CS250 Final Review Questions The following is a list of review questions that you can use to study for the final. I would first make sure you review all previous exams and make sure you fully understand

More information

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

int main() { int account = 100; // Pretend we have $100 in our account int withdrawal; Introduction to Exceptions An exception is an abnormal condition that occurs during the execution of a program. For example, divisions by zero, accessing an invalid array index, or ing to convert a letter

More information

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

CSE143 Exam with answers MIDTERM #1, 1/26/2001 Problem numbering may differ from the test as given. CSE143 Exam with answers MIDTERM #1, 1/26/2001 Problem numbering may differ from the test as given. All multiple choice questions are equally weighted. You can generally assume that code shown in the questions

More information