Practice test for midterm 3 solutions

Similar documents
CS102 C++ Exception Handling & Namespaces

CSCI 104 Exceptions. Mark Redekopp David Kempe

CS11 Advanced C++ Fall Lecture 3

Throwing exceptions 02/12/2018. Throwing objects. Exceptions

void fun() C::C() // ctor try try try : member( ) catch (const E& e) { catch (const E& e) { catch (const E& e) {

C++ Crash Kurs. Exceptions. Dr. Dennis Pfisterer Institut für Telematik, Universität zu Lübeck

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

Object-Oriented Principles and Practice / C++

Bruce Merry. IOI Training Dec 2013

Error Handling in C++

Practice test for midterm 2

What will happen if we try to compile, link and run this program? Do you have any comments to the code?

Review Questions for Final Exam

Problem Solving: Storyboards for User Interaction

CSCI-1200 Computer Science II Fall 2008 Lecture 15 Associative Containers (Maps), Part 2

Unit 1: Preliminaries Part 4: Introduction to the Standard Template Library

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

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

CSci 1113 Midterm 1. Name: Student ID:

C++_ MARKS 40 MIN

C++ Programming Lecture 5 Software Engineering Group

CSCI 104 Classes. Mark Redekopp David Kempe

TABLE OF CONTENTS...2 INTRODUCTION...3

Errors. Lecture 6. Hartmut Kaiser hkaiser/fall_2011/csc1254.html

Basic program The following is a basic program in C++; Basic C++ Source Code Compiler Object Code Linker (with libraries) Executable

17.1 Handling Errors in a Program

8. The C++ language, 1. Programming and Algorithms II Degree in Bioinformatics Fall 2017

COSC 320 Exam 2 Key Spring Part 1: Hash Functions

Assertions and Exceptions

COMP322 - Introduction to C++ Lecture 02 - Basics of C++

(6) The specification of a name with its type in a program. (7) Some memory that holds a value of a given type.

Introduction. Common examples of exceptions

CSE 303, Autumn 2006, Final Examination 12 December 2006

the Stack stack ADT using the STL stack are parentheses balanced? algorithm uses a stack adapting the STL vector class adapting the STL list class

CSCS 261 Programming Concepts Exam 2 Fall EXAM 2 VERSION 1 Fall Points. Absolutely no electronic devices may be used during this exam.

CS 201 (Intro. to Computing) Fall 2015 Sabancı University Sample Questions for Midterm 1

Linked List using a Sentinel

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 1/9/ Review. Here s a simple C++ program:

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

C++: Overview and Features

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

Templates and Vectors

CSCI-1200 Data Structures Spring 2016 Lecture 7 Iterators, STL Lists & Order Notation

Introduction to C++ Day 2

CS 103 Lab V for Vector

CSci 1113 Final. Name: Student ID:

PIC 10A. Lecture 17: Classes III, overloading

Contents. Error Handling Strategies (cont d) Error handling strategies: error code, assert(), throw-try-catch

C++ Programming. Classes, Constructors, Operator overloading (Continued) M1 Math Michail Lampis

2 nd Week Lecture Notes

Introduction to C ++

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

Sample Final Exam. 1) (24 points) Show what is printed by the following segments of code (assume all appropriate header files, etc.

Outline. Zoltán Porkoláb: C++11/14 1

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 8/19/ Review. Here s a simple C++ program:

This test is OPEN Textbook and CLOSED notes. The use of computing and/or communicating devices is NOT permitted.

Review Questions for Final Exam KEY

READ THIS NOW! Do not start the test until instructed to do so!

Practice test for midterm 1

This examination has 11 pages. Check that you have a complete paper.

The University of Nottingham

CSCS 261 Programming Concepts Exam 2 Fall EXAM 2 VERSION 1 Fall Points. Absolutely no electronic devices may be used during this exam.

! Errors can be dealt with at place error occurs

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

4. Structure of a C++ program

CMSC 202 Midterm Exam 1 Fall 2015

ECE 462 Object-Oriented Programming using C++ and Java Design Issues and Multiple Inheritance in C++

CSci 1113 Final. Name: Student ID:

G52CPP C++ Programming Lecture 17

ECE Fall 2018, Test 3

Intermediate Programming, Spring 2017*

Student Name and ID CS 32, WINTER 2015, PRACTICE MIDTERM I.

CSE 333 Midterm Exam Cinco de Mayo, 2017 (May 5) Name UW ID#

Suppose that you want to use two libraries with a bunch of useful classes and functions, but some names collide:

Topics. Functions. Functions

CSCE 110 PROGRAMMING FUNDAMENTALS

Unit 7. 'while' Loops

Data Structures Lecture 3 Order Notation and Recursion

Local and Global Variables

STL components. STL: C++ Standard Library Standard Template Library (STL) Main Ideas. Components. Encapsulates complex data structures and algorithms

The sequence of steps to be performed in order to solve a problem by the computer is known as an algorithm.

CS Introduction to Programming Midterm Exam #2 - Prof. Reed Fall 2015

Variables. Data Types.

Unit 4 Basic Collections

CMSC 202 Section 010x Spring Justin Martineau, Tuesday 11:30am

1- Write a single C++ statement that: A. Calculates the sum of the two integrates 11 and 12 and outputs the sum to the consol.

Discussion 1H Notes (Week 4, April 22) TA: Brian Choi Section Webpage:

COM S 213 PRELIM EXAMINATION #2 April 26, 2001

Distributed Real-Time Control Systems. Lecture 17 C++ Programming Intro to C++ Objects and Classes

Physics 234: Computational Physics

Chapter 5 Errors. Hyunyoung Lee. Based on slides by Bjarne Stroustrup.

Week 3. Function Definitions. Example: Function. Function Call, Return Statement. Functions & Arrays. Gaddis: Chapters 6 and 7.

Your first C++ program

Non-numeric types, boolean types, arithmetic. operators. Comp Sci 1570 Introduction to C++ Non-numeric types. const. Reserved words.

CSCI 102L - Data Structures Midterm Exam #2 Spring 2011

Chapter 5 Errors. Bjarne Stroustrup

CSCI-1200 Data Structures Fall 2016 Lecture 17 Associative Containers (Maps), Part 2

Laboratorio di Tecnologie dell'informazione

Important when developing large programs. easier to write, understand, modify, and debug. each module understood individually

Module 7 b. -Namespaces -Exceptions handling

Transcription:

Practice test for midterm 3 solutions May 5, 2 18 1 Classes Here is a pair of class definitions, and a pair of variable declarations: class A { int x; float y; ; class B { int y; A a; float x; A b; ; A foo; B bar; Label each of the following expressions with OK if it is OK, or Error if it would cause an error: a) foo.x OK b) bar.x Error c) bar.a OK d) foo.y Error e) bar.b.x Error f ) bar.a.y Error Add whatever constructors are necessary to the following class so that the code after it will be correct: class A { // You can put whatever you want in the 1

// definition of the constructor; it's the // parameter list that matters here. A(int v) { x = v; A(float u, int v) { z = u; x = v; A(string s, int u) { y = s; x = u; int x; string y; float z; ; A a1 = 12; A a2{1.2, 10; A a3{"hello", 15; 2

Here is a class with two member functions defined inside of it. Move the definitions of these member functions out of the class. class dog { void feed(); void walk(); bool tired, fed; ; void dog feed() { if(!fed) { cout << "Dog is now fed."; fed = true; else cout << "Dog is not tired."; void dog walk() { if(!tired) { cout << "Walkies"; tired = true; else cout << "Too tired to walk."; Complete the following class definition for a class that stores information about teachers by filling in the definitions of the member functions: class teacher { void give_tenure() { has_tenure = true; void assign_class(string c) { classes.push_back(c); 3

string get_name() { return name; bool has_tenure = false; vector<string> classes; string name; ; Think about a class designed to represent a color. How would you represent a color? Would your representation support mixing colors together to get new colors? Sketch a class (data members and function declarations only) color and explain why you think it would work for this purpose (or explain what its limitations are). There are all sorts of things you can do here. Storing the amount of the three (additive) primary colors, red, green, and blue, is common (e.g., as floats). You can also store the four subtractive primary colors: cyan, yellow, magenta, and black. You could represent colors as strings giving their names, so that mixing "red" with "blue" gives "reddish-blue". class color { float r,g,b; ; color mix(color a, color b) { // Average return color{ (a.r + b.r)/2, (a.g + b.g)/2, (a.b + b.b)/2 ; 2 Multi-file projects Suppose we want to split the following program into three files: main.cpp, triangle.hpp (containing declarations) and triangle.cpp (containing implementations). Circle the parts of the code that code into each file, and add anything else that would be needed to make the resulting project work. 4

// triangle.cpp: #include <iostream> #include <string> #include "triangle.hpp" // Added using namespace std; void triangle set_set(int s) { size = s; void triangle draw() { string t = "*"; string s{size, ' '; for(int i = 1; i < size; ++i) { cout << s << t << s << endl; t += "**"; s.pop_back(); // triangle.hpp #pragma once class triangle { void set_size(int s); void draw(); int size; ; // main.cpp #include "triangle.hpp" // Added int main() { triangle t; t.set_size(10); t.draw(); return 0; 5

What are the commands you would use to manually compile the project in the previous problem? g++ -c main.cpp g++ -c triangle.cpp g++ -o main main.cpp triangle.cpp Explain what the rules are for the order of object (.o) files in the final link step. If A.cpp uses definitions from B.cpp, where should A.o appear in the list of object files, relative to B.o? If A.cpp uses B.cpp, then A.o should come before B.o. For each of the following, state whether it can/should appear in source files, header files, or both: a) Function definitions: Source only b) Function declarations: Both c) using namespace std;: Preferably only in source d) #include<...>: both e) #pragma once: header f ) int main(): source Explain what problem header files are intended to solve; why do we need.hpp files at all? Headers contain declarations and class definitions; in order to use a function or class, its declaration/definition must be visible. So rather than copy-paste everything we need into many different.cpp files, we can put it in one.hpp file and then #include it anywhere we need it. 3 Exceptions What is wrong with the following code? How would you fix it? try { f(); catch(logic_error& e) { cout << "LE"; 6

catch(length_error& e) { cout << "LenE"; catch(runtime_error& e) { cout << "RE"; catch(range_error& e) { A length_error is a kind of logic_error, so the logic_error catch will catch both. Similarly for runtime_error and range_error. The fix is to rearrange them so that the more-specific exception types come first.

The following function takes a vector of pairs of ints and divides the first element of each pair by the second. E.g., if the input vector was {4, 2, 9, 3, 12, 3 then the returned vector would be {2, 3, 4. What kinds of errors could occur in this function? Add assertions to check for them. #include <cassert> vector<int> divide_by(vector<int> v) { assert(v.size() % 2 ); // Even size vector<int> vout; for(int i = 0; i < v.size(); i += 2) { assert(v.at(i) 0); // No divide by 0 vout.push_back(v.at(i) / v.at(i+1)); return vout; 8

For each of the standard expression types to the right, indicate what the following code would print if it were thrown from the function h void h() { throw // exception thrown here void g() { try { h(); catch(domain_error& e) { cout << "DE in g"; catch(runtime_error& e) { cout << "RE in g"; a) domain_error DE in g LE in main b) range_error RE in g Other in main c) out_of_range OOR in main d) length_error LE in main e) system_error RE in g Other in main f ) exception Other in main void main() { try { g(); h(); catch(range_error& e) { cout << "RE in main"; catch(out_of_range& e) { cout << "OOR in main"; catch(logic_error& e) { cout << "LE in main"; catch(...) { cout << "Other in main"; Explain the difference between assertions and expressions. When would you use each? Assertions are used to check your work ; you add them to assert things that should always be true (and which, if false, would mean something has gone very wrong in your program). If an assertion fails, it ends the program immediately. Exceptions are used for situations which your program could potentially recover from. 9

The following code uses assertions to check for problems. Convert it to using standard exceptions (and choose exception types that seem appropriate to you). // Uses remainder hashing to compute the hash value of a string s. // Take CSci 133 if you want to know more! int hash(string s, int m) { if(s.empty()) throw length_error; // Input string cannot be empty if(m 0) throw domain_error; // Size must be positive int h = 0; for(char c : s) { if(c 0) throw invalid_argument; // No non-ascii characters if(256 * h + c h) throw overflow_error; // No numeric overflow h = (256 * h + c) % m; return h; 1