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

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

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

University of Toronto

University of Toronto

University of Toronto

CSCE 110 PROGRAMMING FUNDAMENTALS

Name: Username: I. 10. Section: II. p p p III. p p p p Total 100. CMSC 202 Section 06 Fall 2015

COMP322 - Introduction to C++

Operator overloading: extra examples

CMSC 202 Midterm Exam 1 Fall 2015

a. a * c - 10 = b. a % b + (a * d) + 7 =

The American University in Cairo Department of Computer Science & Engineering CSCI &09 Dr. KHALIL Exam-I Fall 2011

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

University of Maryland Baltimore County. CMSC 202 Computer Science II. Fall Mid-Term Exam. Sections

Due Date: See Blackboard

Review: C++ Basic Concepts. Dr. Yingwu Zhu

Lab 2: ADT Design & Implementation

CE221 Programming in C++ Part 1 Introduction

PIC 10A Objects/Classes

Class Example. student.h file: Declaration of the student template. #ifndef STUDENT_H_INCLUDED #define STUDENT_H_INCLUDED

Object Oriented Design

Multiple Choice (Questions 1 14) 28 Points Select all correct answers (multiple correct answers are possible)

Due Date: See Blackboard

2. It is possible for a structure variable to be a member of another structure variable.

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.

Introducing C++ to Java Programmers

Multiple Choice Questions (20 questions * 6 points per question = 120 points)

The American University in Cairo Department of Computer Science & Engineeringt CSCI &09 Dr. KHALIL Exam-I Fall 2009

Lecture 8. Xiaoguang Wang. February 13th, 2014 STAT 598W. (STAT 598W) Lecture 8 1 / 47

Module Operator Overloading and Type Conversion. Table of Contents

CSC1322 Object-Oriented Programming Concepts

Multiple Choice (Questions 1 14) 28 Points Select all correct answers (multiple correct answers are possible)

Implementing an ADT with a Class

MARKING KEY The University of British Columbia MARKING KEY Computer Science 260 Midterm #1 Examination 12:30 noon, Tuesday, February 14, 2012

CS 115 Exam 3, Spring 2010

Scientific Computing

CISC 2200 Data Structure Fall, C++ Review:3/3. 1 From last lecture:

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

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

Introduction to Programming using C++

Instance and Static Members Each instance of a class has its own copies of the class s instance (member) variables

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

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

Lecture 7. Log into Linux New documents posted to course webpage

Midterm Practice Exam

Starting Savitch Chapter 10. A class is a data type whose variables are objects. Some pre-defined classes in C++ include int,

Basic memory model Using functions Writing functions. Basics Prototypes Parameters Return types Functions and memory Names and namespaces

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

Name MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

Fundamental of Programming (C)

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

C++_ MARKS 40 MIN

CIS 190: C/C++ Programming. Classes in C++

Engineering Problem Solving with C++, 3e Chapter 2 Test Bank

2 nd Week Lecture Notes

EL2310 Scientific Programming

2 2

CSI33 Data Structures

Linked List using a Sentinel

Abstract Data Types. Different Views of Data:

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

Short Notes of CS201

Part I: Short Answer (12 questions, 65 points total)

Summary of basic C++-commands

CSE 303, Spring 2006, Final Examination 6 June Please do not turn the page until everyone is ready.

Definition Matching (10 Points)

COSC 320 Exam 2 Key Spring Part 1: Hash Functions

Total 100. The American University in Cairo Computer Science & Engineering Department CSCE 106. Instructor: Final Exam Fall Section No.

Circle all of the following which would make sense as the function prototype.

Operator overloading. Instructor: Bakhyt Bakiyev

ComS 228 Exam 1. September 27, 2004

CS201 - Introduction to Programming Glossary By

G52CPP C++ Programming Lecture 17

University of Illinois at Urbana-Champaign Department of Computer Science. First Examination

No aids allowed. No books. No notes. No calculators. No computers. No communicating devices.

Using the Xcode Debugger

The American University in Cairo Computer Science & Engineering Department CSCE 106 Fundamentals of Computer Science

Midterm Exam. Sample Solutions

Intro to Programming & C Why Program? 1.2 Computer Systems: Hardware and Software. Why Learn to Program?

BEng (Hons) Electronic Engineering. Resit Examinations for / Semester 1

C:\Temp\Templates. Download This PDF From The Web Site

BITG 1233: Introduction to C++

Practice Problems CS2620 Advanced Programming, Spring 2003

CSCE Practice Midterm. Data Types

EINDHOVEN UNIVERSITY OF TECHNOLOGY Department of Mathematics and Computer Science

Constructor - example

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

CS 117 Programming II, Spring 2018 Dr. Ghriga. Midterm Exam Estimated Time: 2 hours. March 21, DUE DATE: March 28, 2018 at 12:00 PM

C++ Basics. Data Processing Course, I. Hrivnacova, IPN Orsay

CMPS 221 Sample Final

Review Questions for Final Exam KEY

University of Illinois at Urbana-Champaign Department of Computer Science. First Examination

! A literal represents a constant value used in a. ! Numbers: 0, 34, , -1.8e12, etc. ! Characters: 'A', 'z', '!', '5', etc.

C++ Programming Assignment 3

The American University in Cairo Computer Science & Engineering Department CSCE 106 Fundamentals of Computer Science. Instructor: Final Exam Fall 2011

CS242 COMPUTER PROGRAMMING

Total 100. The American University in Cairo Computer Science & Engineering Department CSCE 106. Dr. Khalil Exam II Fall 2011

CSCE 2004 Final Exam Spring Version A

1. Match each of the following data types with literal constants of that data type. A data type can be used more than once. A.

Transcription:

University of Toronto Faculty of Applied Science and Engineering ECE 244F PROGRAMMING FUNDAMENTALS Fall 2013 Midterm Test Examiners: T.S. Abdelrahman, V. Betz, M. Stumm and H. Timorabadi Duration: 110 minutes This test is OPEN Textbook and CLOSED notes. The use of computing and/or communicating devices is NOT permitted. Do not remove any sheets from this test book. Answer all questions in the space provided. No additional sheets are permitted. Work independently. The value of each question is indicated. The total value of all questions is 100. Write your name and student number in the space below. Do the same on the top of each sheet of this exam book. Name: (Underline last name) Student Number: Q1. Q7. Q2. Q8. Q3. Q9. Q4. Q10. Q5. Q11. Q6. Q12. Total Page 1 of 17

Question 1. (6 marks). Stringstreams A token in a stream is defined as a group of adjacent non-white-space characters surrounded by white space, except for the first token that may not have white space before the first character, and except for the last token that may not have white space after the last character. You are to write a function, getnextint() that reads from stringstream ss. If the next token is an integer, the function should return TRUE and the integer; otherwise it should return FALSE. Fill in the required code below to define the function getnextint() and fill in the code required to properly make the call to the function. int main() { int x ; stringstream ss ; ss << getcontent() ; if( getnextint( ) ) cout << The first token is << x << endl ; else cout << Error: first token is not an int << endl ; bool getnextint( ) { Page 2 of 17

Question 2. (12 marks). Operator Overloading. The following class is used to create objects that represent distance, consisting of feet and inches. using namespace std; #include <iostream> class Distance { private: int feet; int inches; ; public: Distance(int f, int i); int getfeet(); int getinches(); void setfeet(int f); void setinches(int i); void print(); Distance::Distance(int f, int i) { feet = f; inches = i; int Distance::getFeet() { return (feet); int Distance::getInches() { return (inches); void Distance::setFeet(int f) { feet = f; void Distance::setInches(int i) { inches = i; void Distance::print() { cout << Distance is: << feet << feet and << inches << inches << endl; Page 3 of 17

We wish to overload the + and == operators for the Distance class to be able to write code like this in a non-member function (say main): Distance X(10,2); Distance Y(1,3); Distance Z(0,0); : Z = X + Y; if (X == Y) ; For the example declarations above, X + Y results in an object that represents 11 feet and 5 inches and (X == Y) results in false. Write the implementation of the two overloaded operator functions as members of the class Distance. Clearly show the function header and its body. Write the overloaded + operator function below Write the overloaded == operator function below Page 4 of 17

Question 3. (6 marks). Operator Overloading. The Toronto Census Bureau keeps the records for each of Toronto s residents in a Resident class. A portion of the declaration for the Resident class is shown below. class Resident { public: Resident();... private: string first_name; // e.g. John string last_name; // e.g. Baggenstos... ; The string class allows you to use the < operator to lexically compare two strings. So for example: Judy < Suzy Jack < Joe are both true. For consistency, the symbol < should also be overloaded for the Resident class to sort the residents alphabetically based on their name. As in a standard directory, the names are sorted first by last name and if the last names are the same they are then sorted by first name. For example, Judy Baggenstos < Alice Zachoski Aaron Baggenstos < Frodo Baggenstos Write the definition of the operator< member function for the Resident class. Write the overloaded < operator function below Page 5 of 17

Question 4. (13 marks). Constructors and Destructors. Consider the following definition and implementation of a class that represents a day of the year. You may assume that this code is error-free. using namespace std; #include <iostream> class DayOfYear { private: int day; int month; DayOfYear(const DayOfYear & other); ; public: ~DayOfYear(); DayOfYear(int d, int m); int getday(); int getmonth(); void setday(int d); void setmonth(int m); DayOfYear operator=(dayofyear src); void print(); DayOfYear::~DayOfYear () {/* Nothing to do */ DayOfYear::DayOfYear (int d, int m) { day = d; month = m; DayOfYear::DayOfYear(const DayOfYear & other) { day = other.day; month = other.month; int DayOfYear::getDay() {return (day); int DayOfYear::getMonth() {return (month); void DayOfYear::setDay(int d) {day = d; void DayOfYear::setMonth(int m) {month = m; DayOfYear DayOfYear::operator=(DayOfYear src) { day = src.day; month = src.month; return(*this); void DayOfYear::print() {cout << day << / << month << endl; Page 6 of 17

Now consider the following code in the functions main and mystery. The code is shown in a table to facilitate the writing of your answers. Indicate which lines generate a compile time error by writing X next the line in the second column of the table. using namespace std; Code Compile-time Error? #include <iostream> #include DayOfYear.h void mystery (DayOfYear); int main() { DayOfYear FirstOfJuly(1,7); DayOfYear Christmas(25,12); DayOfYear temp; DayOfYear May11(11,5); DayOfYear MothersDay = May11; DayOfYear today(1,1); DayOfYear Oct22(22,10); today = Oct22; mystery(today); return(0); void mystery(dayofyear source) { DayOfYear* temp = new DayOfYear(0,0); temp->setmonth(source.getday()); temp->setday(source.getmonth()); delete temp; Page 7 of 17

Question 5. (11 marks). Scopes. The following class definition describes a simple C++ class called AnExampleClass. #include <iostream> using namespace std; class AnExampleClass { private: int value; public: AnExampleClass (); AnExampleClass (int v); ~AnExampleClass (); ; AnExampleClass::AnExampleClass () { value = 0; cout << "Constructing object with value 0"<< endl; AnExampleClass::AnExampleClass (int v) { value = v; cout << "Constructing object with value " << value << endl; AnExampleClass::~AnExampleClass () { cout << "Destructing object with value " << value << endl; Now consider the following code, which uses AnExampleClass: AnExampleClass a; void mystery() { cout << "Entering mystery" << endl; AnExampleClass x(100); AnExampleClass* p; p = new AnExampleClass(5); cout << "Leaving mystery" << endl; return; int main() { cout << "Staring main" << endl; AnExampleClass a; if (true) { AnExampleClass* p; p = new AnExampleClass[2]; delete [] p; mystery(); cout << "Leaving main" << endl; return 0; Page 8 of 17

In the table below, write the output that an execution of the above program produces in the order in which it is produced. Use one entry in the table for each line of output produced. Page 9 of 17

Question 6. (6 marks). Pointers. What is the output produced by the following segments of code? (a) int* p1; int* p2; p1 = new int; p2 = new int; *p1 = 10; *p2 = 20; cout << *p1 << << *p2 << endl; p1 = p2; cout << *p1 << << *p2 << endl; *p1 = 30; cout << *p1 << << *p2 << endl; Write the output here. (b) int* p1; int* p2; p1 = new int; p2 = new int; *p1 = 10; *p2 = 20; cout << *p1 << << *p2 << endl; *p1 = *p2; cout << *p1 << << *p2 << endl; *p1 = 30; cout << *p1 << << *p2 << endl; Write the output here. Page 10 of 17

Question 7. (12 marks). Classes. Consider the following definition for a class Mystery. #include <string> class Mystery { private: int value; ; public: Mystery (const Mystery & s); Mystery (const string & s); ~Mystery (); int getvalue() const; void setvalue(int i); bool operator<(const Mystery & rhs); Indicate by placing an X in the appropriate column whether each of the following statements is valid, or not valid given the definition of Mystery above. A statement is valid if it compiles and executes correctly. A statement is not valid if it either generates a compile-time error, or if it compiles correctly, but produces a run-time error. Assume that each of the statements is independent of the others and that they all appear in the main function. Statement Comments Valid Not Valid Mystery s; Mystery a(sv); Mystery W(X); Mystery c = sv; cout << f.getvalue(); sv is a string object X is an object of type Mystery sv is a string object f is an object of type Mystery (d < e); d and e are objects of type Mystery Mystery h = j; Y = W; e = sv; e < v; cout << f.value; f = d + e h and j are objects of type Mystery Y and W are objects of type Mystery e is an object of type Mystery sv is a string object e is an object of type Mystery v is an integer f is an object of type Mystery f, d and e are objects of type Mystery Page 11 of 17

Question 8. (8 marks). Arrays and Objects. Consider the following definition of the class Complex: class Complex { private: float real; float imag; ; public: Complex(); // Default constructor Complex(float r, float imag); // Second constructor int getreal(); int getimag(); void setreal(float r); void setimag(float i); void print(); The following function has been written by a programmer to allocate an array with n Complex objects, where n is an integer value read from the input. Complete the code of the function to delete all dynamically allocated data so that there is no memory leak at the end of the function. void create () { Complex** p; int n; cin >> n; p = new Complex* [n]; for (int i=0; i < n; ++i) { p[i] = new Complex((float)i,(float)2*i); Complex* q = new Complex(); *q = *p[n-1]; q->setreal(5.0); q->setimag(3.0); // ADD CODE HERE Page 12 of 17

Question 9. (5 marks). Debugging and Compilation. (a) You are debugging a program in NetBeans, and execution has paused due to hitting a breakpoint at the line highlighted in the figure above. You wish to see what happens when you execute one line of the getresname function (i.e. you want to execute just the start of that function, and then examine some variables). Either circle the button you should click on in the screen shot above, or describe the menu command you should use to make this happen. (b) What is the difference between a compiler error and a compiler warning? (c) Assuming a file main.cpp exists and contains valid C++ code, what is the file that will be generated by the command: g++ -g Wall main.cpp o foo (d) Assuming a file main.cpp exists and contains valid C++ code, what is the file that will be generated by the command: g++ -g -c main.cpp Page 13 of 17

Question 10. (4 marks). Program Organization. Consider the program below, which is organized into two header files and three.cpp files. A.h #ifndef A_H #define A_H class A { private: double d1; public: A(); ; #endif B.h #ifndef A_H #define A_H #include A.h class B { private: A a1; public: B(); ; #endif A.cpp B.cpp main.cpp #include A.h A::A () { B::B () { #include A.h #include B.h int main () { A mya; B myb; return (0); When compiled with the command: g++ A.cpp B.cpp main.cpp o prog two compiler errors are reported. Modify some of the files shown above so that the program compiles properly you can add, delete or modify lines in any of the 5 files. You should make only the minimal modifications necessary in order to make the program compile properly. Page 14 of 17

Question 11. (7 marks). Functions and Parameters. (a) (3 marks). Consider the functions below. #include <iostream> using namespace std; int compute (int i1) { i1 = i1 + 3; return (i1); int compute (int i1, int& i2) { i1 = i1 + 4; i2 = i1 + i2; return (i1 + i2); int compute2 (const int& i1) { return (i1 + i1); int main () { int ival1 = 2; int ival2 = 3; int resval = compute (ival1, ival2); resval = resval + compute (ival2); resval = resval + compute2 (ival2); cout << "resval is " << resval << endl; Write the program output below. Page 15 of 17

(b) (4 marks). Consider the following class definition and implementation, contained in a file called number.cc. The line numbers shown are for reference and are not part of the code. 1 #include <iostream> 2 using namespace std; 3 4 class Number { 5 private: 6 int _number; 7 public: 8 Number (); 9 Number (int v); 10 void increment () const; 11 void copy_and_destroy (const Number & other); 12 void print() const; 13 ; 14 15 Number::Number () { 16 _number = 0; 17 18 19 Number::Number (int num) { 20 _number = num; 21 22 23 void Number::increment () const { 24 _number = _number + 1; 25 26 27 void Number:: copy_and_destroy (const Number & other) { 28 _number = other._number; 29 other._number = 0; 30 31 32 void Number::print() const { 33 cout << _number << endl; 34 The above code with the command g++ -c number.cc generates some compiler errors. In the table below indicate what these errors are by writing the number of each line of code that generates the error and a brief explanation of the error. There may or may not be as many errors as there are rows in the table. Line # causing error Explanation Page 16 of 17

Question 12. (10 marks). Programming and File I/O. Write a full program, that inputs the name of a text file from cin, opens that file, and then count the number of words and lines in the file. The program should output two numbers: the number of words in the file and the number of lines in the file. You may assume a file with the input name exists and that the file only contains words with no numbers or isolated punctuation marks. Your program should include the required #include statements. (You can open a file with ifstream filename( filename ). You will get full marks if the program is written with 25 or fewer statements, where statements counted are if-, while-, for-, and the #include statements, as well as any statement ending with ;. You will get a 10% reduction in your mark for every 2 statements over 25. Page 17 of 17