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

COMP322 - Introduction to C++

University of Toronto

University of Toronto

1. (25 pts) Short Answer. Provide brief (1-3 sentence) answers to the following:

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

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

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

CMSC 202 Midterm Exam 1 Fall 2015

ECE 461 Fall 2011, Final Exam

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

Function Overloading

Outline. User-dened types Categories. Constructors. Constructors. 4. Classes. Concrete classes. Default constructor. Default constructor

CSCE 110 PROGRAMMING FUNDAMENTALS

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

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

Pointers and Arrays CS 201. This slide set covers pointers and arrays in C++. You should read Chapter 8 from your Deitel & Deitel book.

ADTs in C++ In C++, member functions can be defined as part of a struct

Operator overloading: extra examples

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.

ECE 462 Fall 2011, Second Exam

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.

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

CE221 Programming in C++ Part 1 Introduction

Short Notes of CS201

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

Name: I. 20 II. 20 III. 20 IV. 40. CMSC 341 Section 01 Fall 2016 Data Structures Exam 1. Instructions: 1. This is a closed-book, closed-notes exam.

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

CSC 126 FINAL EXAMINATION Spring Total Possible TOTAL 100

CS201 - Introduction to Programming Glossary By

ECE Fall 2017, Third Exam

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.

C++_ MARKS 40 MIN

Review Questions for Final Exam

l Determine if a number is odd or even l Determine if a number/character is in a range - 1 to 10 (inclusive) - between a and z (inclusive)

ComS 228 Exam 1. September 27, 2004

Midterm Review. PIC 10B Spring 2018

Roxana Dumitrescu. C++ in Financial Mathematics

The University of Nottingham

Multiple Choice Questions (20 questions * 5 points per question = 100 points)

Review. Outline. Array Pointer Object-Oriented Programming. Fall 2017 CISC2200 Yanjun Li 1. Fall 2017 CISC2200 Yanjun Li 2

CSC1322 Object-Oriented Programming Concepts

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

University of Michigan EECS 183: Elem. Programming Concepts Fall 2011 Exam 1: Part 1: Form 1. Professors: ML Dorf, Elliot Soloway

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

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

Implementing Abstract Data Types (ADT) using Classes

Object oriented programming

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

Review Questions for Final Exam KEY

PIC 10A. Lecture 17: Classes III, overloading

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

/************************************************ * CSC 309/404 Review for Final -- Solutions * ************************************************/

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

pointers & references

The University of Nottingham

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

Definition Matching (10 Points)

Constructor - example

ECE Fall 2018, Test 3

ECE Fall 20l2, Second Exam

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

Introducing C++ to Java Programmers

Review. Outline. Array Pointer Object-Oriented Programming. Fall 2013 CISC2200 Yanjun Li 1. Fall 2013 CISC2200 Yanjun Li 2

การทดลองท 8_2 Editor Buffer Array Implementation

Overloading Operators in C++

Pointers, Dynamic Data, and Reference Types

2 2

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

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

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

Midterm Exam. Sample Solutions

CS 2604 Homework 1 C++ Review Summer I 2003

Implementing an ADT with a Class

IS0020 Program Design and Software Tools Midterm, Fall, 2004

Assignment operator string class c++ Assignment operator string class c++.zip

Reference Parameters A reference parameter is an alias for its corresponding argument in the function call. Use the ampersand (&) to indicate that

Initializing and Finalizing Objects

Due Date: See Blackboard

CS201 Latest Solved MCQs

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

2 nd Week Lecture Notes

Introduction to Programming using C++

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

double d0, d1, d2, d3; double * dp = new double[4]; double da[4];

Abstract Data Types. Different Views of Data:

Suppose we find the following function in a file: int Abc::xyz(int z) { return 2 * z + 1; }

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

Programming, numerics and optimization

Object Oriented Design

GE U111 Engineering Problem Solving & Computation Lecture 6 February 2, 2004

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

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

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

Slide Set 14. for ENCM 339 Fall Steve Norman, PhD, PEng. Electrical & Computer Engineering Schulich School of Engineering University of Calgary

Lab 2: ADT Design & Implementation

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

Transcription:

University of Toronto Faculty of Applied Science and Engineering ECE 244F PROGRAMMING FUNDAMENTALS Fall 2014 Midterm Test Examiners: T.S. Abdelrahman and M. Stumm 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 25

Question 1. (8 marks). Pointers. (a) Consider the following declarations int i; int *pi; double d; double *pd; Which of the following statements is valid? Circle all the ones that are valid i=π *pi=&i; pd=π pd=i; pi=&i; (b) What would be printed from the following C++ code segment? int i=10; int *p; int **q; int ***r; p=&i; *p=15; q=&p; **q=20; r=&q; ***r= (*p) + 1; cout<< i; (c) What is the output of the following C++ code segment? #include <iostream> using namespace std; int do_something (int* & ptr1, int* ptr2) {\ int* t; *ptr1 = *ptr1 + *ptr2; *ptr2 = *ptr1 + *ptr2; t = ptr1; ptr1 = ptr2; ptr2 = ptr1; return (*ptr1 + *ptr2); int main () { int* p = new int; int* q = new int; int z; *p = 5; *q = 8; z = do_something (p, q); z = z + *p + *q; cout << z << endl; return (0); Page 2 of 25

Question 2. (5 marks). Functions. Assume there exists four classes: Mystery, StoryLine, Novel and Author. We wish to write a member function of class Author, called WriteIt, which takes two arguments. The first is an object of type Mystery, passed by value. The second is a pointer to an object of type StoryLine, passed by reference. The function returns an object of type Novel, by reference. Write the declaration (prototype) of the function below. Page 3 of 25

Question 3. (5 marks). Classes. Consider the following code. It may contain several errors. Identify these lines of code with errors and list them in the table shown on the next page, along with a short explanation of what the error is. To make it easy to refer to lines of code, lines numbers are shown (they are not part of the code). Where a line is not numbered, you may assume it is error-free. You may need all the rows of the table. class PossiblyBad { private: 01 int x; 02 int negate(); public: 03 float y; 04 PossiblyBad(int i, float f); 05 PossiblyBad(PossiblyBad source); 06 ~PossiblyBad(int k); 07 void Mystery(float f); ; 08 PossiblyBad::PossiblyBad(int i, float f) { 09 x = i; 10 11 PossiblyBad::PossiblyBad(PossiblyBad source) { 12 x = source.x; y = source.y; 13 14 void PossiblyBad::~PossiblyBad (int k) { 15 x = k; 16 17 void PossiblyBad::Mystery(float f) { 18 y = (float) -1*x; 19 20 int main () { 21 PossiblyBad a; 22 PossiblyBad b(1); 23 PossiblyBad c(1,3.5); 24 c.x = 2; 25 c.y = 5.8; 26 c.negate(); 27 c.mystery(6.8); return (0); Page 4 of 25

Line Number Explanation of Error Page 5 of 25

Question 4. (5 marks). Constructors and Destructors. Consider the following definition of a class Foo. class Foo { private: int x; public: Foo(int i); // call this the Integer constructor int getx() const; void setx(int i); Foo Mystery(Foo one, Foo & two); ; Foo::Foo(int i) { x = i; int Foo::getX() const { return x; void Foo::setX(int i) { x = i; Foo Foo::Mystery(Foo one, Foo & two) { Foo t(8); t.x = one.x + two.x; two.x = t.x; return (*this); Consider the code in the main functions below. int main () { Foo a(0); Foo b(1); // point A a.mystery(a,b); // point B return (0); Page 6 of 25

(a) How many constructors of class Foo are called between points A and B in the code? (b) Name the constructors that are called between points A and B. Use the table below to name the constructors, one per line. (c) How many constructors are called in the execution of main? (d) How many destructors are called between points A and B in the code? (e) How many destructors are called in the execution of main? Page 7 of 25

Question 5. (5 marks). C++ I/O. We wish to write a program that prompts the user to enter her first and last names at the standard input and then print her initials to the standard output. #include <iostream> using namespace std; int main () { char firstinitial; char lastinitial; cout << "Enter your first name followed by your last name: "; cout << "Your initials are: " << firstinitial << lastinitial << endl; return (0); Complete the program by writing code in the box shown above. You are not to declare/use any other variables than the ones shown in the program. However, you may use any of the functions of iostream (e.g., cin.peek(), cin.ignore() or cin.fail()). You may also assume that the user will always enter her first name followed by her last name. Your answer should be at most 3 lines of code. You will lose marks for additional lines. Here are example inputs and outputs for the program (user input is shown in italics): Enter your first name followed by your last name: Patricia Williams Your initials are PW Enter your first name followed by your last name: Sandy Your initials are SS Smith Enter your first name followed by your last name: Rachel McDonalds Your initials are RM Page 8 of 25

Question 6. (13 marks). Constructors and Destructors. Consider the following declaration of a class that represents a day of the year. You may assume that this class is implemented correctly and is error-free. using namespace std; #include <iostream> class DayOfYear { private: int day; int month; ; public: DayOfYear(); DayOfYear(int d, int m); DayOfYear(const DayOfYear & other); ~DayOfYear(); int getday() const; int getmonth() const; void setday(int d); void setmonth(int m); DayOfYear & operator=(const DayOfYear & src); void print() const; We wish to be able to write the following code in the function main. #include <iostream> #include <DayOfYear.h #include <string> #include <sstream> using namespace std; int main () { string s; s = 28 10 ; // Day is 28 and month is 10 DayOfYear today(s); today.print(); return (0); The above code requires demands that one member function be added to the class. Write this function in the space below. Your answer should not exceed a few lines of code. { Write function header here Write function body here Page 9 of 25

Question 7. (8 marks). Operator Overloading. The following class is used to create objects that represent rectangles. Each rectangle is represented by a pair of coordinates: (x top,y top ) and (x bottom,y bottom ) corresponding respectively to the x and y coordinates of the top left and bottom right corners of the rectangle. using namespace std; #include <iostream> class Rectangle { private: int x_top; int y_top; int x_bottom; int y_bottom; ; public: Rectangle(int x_t, int y_t, int x_b, int y_b); int getxtop() const; int getytop() const; int getxbottom() const; int getybottom() const; void setxtop (int x_t); void setytop (int y_t); void setxbottom (int x_b); void setybottom (int y_b); Rectangle::Rectangle (int x_t, int y_t, int x_b, int y_b) { x_top = x_t; y_top = y_t; x_bottom = x_b; y_bottom = y_b; int Rectangle::getXtop() const {return (x_top); int Rectangle::getYtop() const {return (y_top); int Rectangle::getXbottom() const {return (x_bottom); int Rectangle::getYbottom() const {return (y_bottom); void Rectangle::setXtop(int x_t) {x_top = x_t; void Rectangle::setYtop(int y_t) {y_top = y_t; void Rectangle::setXbottom(int x_b) {x_bottom = x_b; void Rectangle::setYbottom(int y_b) {y_bottom = y_b; Page 10 of 25

We wish to overload the == operator for the Rectangle class to be able to write code like this in a non-member function (say main): Rectangle X(0,0,2,3); Rectangle Y(4.8,20,10); : if (X == Y) ; The function returns true if the areas of the two rectangles being compared are equal, otherwise returns false. Thus, for the example declarations above, (X == Y) returns in false. Write the implementation of the overloaded operator== function, once as a member of Rectangle and once as a non-member function. Clearly show the function header and its body. Write the overloaded operator== function as a member of Rectangle below Write the overloaded operator== function as a non-member function below Page 11 of 25

Question 8. (5 marks). Functions. Consider the following code of a class Foo. You may assume that the code is error free. class Foo { private: int x; ; public: Foo(int i); int getx() const; void setx(int i); Foo::Foo(int i) { x = i; int Foo::getX() const { return x; void Foo::setX(int i) { x = i; Consider the following non-member function printnagative: #include <iostream> using namespace std; void printnagative(const Foo & source) { cout << -1*source.x << endl; (a) This function, as written, has a problem with it. Describe what the problem is (one short sentence). (b) If we do not wish to change the function nor make it a member function of Foo, what change must be made to the class Foo to make printnegative correct? You may write your answer in the code of Foo above. (c) If we can change the function but still not make it a member function of Foo, re-write the body of the function to make it correct? Write your answer below. Page 12 of 25

Question 9. (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 () { #include A.h B::B () { #include A.h #include B.cpp int main () { A mya; B myb; return (0); What command would you use to compile your program? Write your answer below. Page 13 of 25

Question 10. (5 marks). Classes. Consider the following code of a class Mystery. You may assume that the declaration of the class is correct. However, the implementation of the class functions does contain several errors. Identify these errors and provide a brief explanation of each error in the table on the following page. To help you refer to lines of code, lines numbers are added to the left of the code (they are not part of the code). Note that you may not need all rows of the table. class Mystery { private: int x; int y; char* str; public: Mystery() const; int getx () const; int gety () const; void setx(int i); void sety(int i); ; int operator-(const Foo & rhs) const; Mystery::Mystery () const { x = 0; y = 0; str = new char[1]; str[0]= `\0`; 01 Mystery::~Mystery (){ delete str; 02 int Mystery::getX () const { if (x>10) x=10; return x; 03 int Mystery::getY () const { if (y>10) y=10; return y; 04 void Mystery::setX(int i) { x = i; 05 void Mystery::setY(int i) { y = i; 06 int operator-(const Mystery & rhs) const { 07 Mystery t; 08 int temp; 09 temp = x; 10 t.x = x rhs.x; 11 x = rhs.x - x; 12 t.x = x rhs.x; 13 x = rhs.x - x; 14 t.str[0] = rhs.str[0]; 15 rhs.str[0] = \0 ; 16 str[0] = t.str[0]; 17 return (temp); Page 14 of 25

Line Number Explanation of Error Page 15 of 25

Question 11. (5 marks). Classes and Objects. Consider the following declaration and implementation of the class Complex: #include <iostream> using namespace std; class Complex { private: float* real; float* imag; ; public: Complex(); Complex(float r, float i); float getreal() const; float getimag() const; void setreal(float r); void setimag(float i); void print() const; #include Complex.h Complex::Complex() { real = new float; *real = 0.0; imag = new float; *image = 0.0; Complex::Complex(float r, float i) { real = new float; *real = r; imag = new float; *image = i; float Complex::getReal() const { return (*real) ; int Complex::getImag() const { return (*imag) ; void Complex::setReal(float r) { *real = r; void Complex::setImag(float i) { *imag = i; void Complex::print() const { cout << ( << real <<, << imag << ) << endl; Page 16 of 25

Now consider the program below that uses the above Complex class. #include <iostream> #include Complex.h using namepace std; void flip(complex x, Complex &y) { x.setreal(y.getimag()); y.setimag(x.getimag)); void main() { Complex a(5.0,4.6); Complex b(3.7,1.5); flip(a,b); a.print(); b.print(); return (0); (a) What is the output produced by the program above? (b) Does the program above suffer from any memory leaks? If so, then how many variables (ints, floats, or objects of type Complex) end up being memory leaks? (c) There are problems with the above implementation of Complex that can be fixed by the addition of three member functions. In the space below, write the implementation of two of these functions (any two). Make sure to include the function header and the function body for each. Write the first function below Write the second function below Page 17 of 25

Question 12. (5 marks). Constructors and operator overloading. Consider the following C++ program: class Complex { private: double re ; double im ; static int ctorcount ; public: Complex() { re=im=0 ; cout << ++ctorcount << " "; Complex(const Complex& c) { re = c.re ; im = c.im ; cout << "copy" << ++ctorcount << " " ; Complex(double r, double i) { re = r ; im - i ; cout << ++ctorcount << " " ; ~Complex() { cout << "~" << ctorcount-- << " " ; Complex operator+( const Complex & rhs ) const { return Complex( re+rhs.re, im+rhs.im ) ; Complex operator-() const { return Complex( -re, im ) ; ; int Complex::ctorCount = 0 ; int main() { Complex *p ; Complex a(1,1) ; Complex b(-2,2) ; p = new Complex[3] ; cout << "A " <<endl ; p[0] = a ; cout << "B " <<endl ; p[1] = a + b ; cout << "C " <<endl ; p[2] = -a ; cout << "D" << endl; What is the output of this program? Page 18 of 25

Question 13. (5 marks). A Str class. Joe Programmer wrote a Str class to implement strings in a way that uses less memory than the C++ String or the C++ string class. The Str class is to behave just like the String class. For the following lines of code, provide a valid header for the constructor that would be invoked: Str s1 = abc ; Str s2 ; Str s3 = s2 ; Now, Joe wishes to write concatenate operators so that the following code works as expected: s3 = s1 + s2 ; s3 = abc + s2 ; s3 = s1 + bcd ; s3 = abc + cba ; How many operator+ functions does Joe have to define and implement? How many of those can be a member of the class? Page 19 of 25

Question 14. (5 marks). Operators. Assume C++ has a binary! valid C++ statements: operator defined. And assume the following two statements are c = a! b ; e = a! b! c! d ; where the operator in the first statement is guaranteed not to modify a and b, and second statement is interpreted as follows: e = ((a! b)! c)! d ; Programmer Suzy Hacker has defined a class T and started writing the declaration for operator!: T::operator!( T & ) ; (a) Place a in the code above where the cost keyword should be. (b) What should the return type be and does the operator return by value or by reference? (c) What goes in the space above in the code? Page 20 of 25

Question 15. (5 marks). Compilation. A program, prog.cpp, makes use of five classes, A, B, C, D, and E that are defined in the files A.cpp, B.cpp, C.cpp, D.cpp, and E.cpp, respectively, and declared in their corresponding.h files: A.h, B.h, C.h, D.h, and E.h, respectively. The following table describes which.h files are included in which files using the #include preprocessor statement: File prog.cpp A.cpp B.cpp B.h C.cpp D.cpp E.cpp Includes: A.h, C.h A.h, B.h B.h C.h C.h D.h, E.h E.h Assume that (i) the program has been compiled so that the executable prog has been generated along with all of the.o files: A.o, B.o, C.o, D.o, E.o, and prog.o, and subsequently (ii) the file C.h is modified. List below the precise Unix compiler commands required, in the correct order, to obtain the executable prog so that (i) the minimal number of.cpp files are compiled and so that (ii) subsequent changes to the source files allow the compilation of the minimal number of.cpp files. Page 21 of 25

Question 16. (5 marks). perhaps as part of a collection of short simple questions a) A C++ class can have multiple constructors. Can it have multiple destructors? If so, how many destructors can be there for any class that has n constructors? Explain. b) The C++ statement T a = b ; a. invokes the assignment operator b. invokes the default constructor c. invokes the copy constructor d. this is an incorrect statement c) True or False: In your C++ program, the input operator, <<, and the output operator, >>, can be overloaded either as a member function or as a non-member function d) What is wrong with the following code fragment: int *p1, *p2 ; p1 = new int ; p2 = new int ; *p1 = 11 ; *p2 = 0 ; p2 = p1 ; cout << *p1 << << *p2 << endl ; delete p1 ; delete p2 ; A: nothing B: p1 and p2 both have the same value, so the delete p2 will cause an error C: you have a memory leak D: B and C Page 22 of 25

e) Linked Lists Which of the following code fragment correctly inserts a new node containing num into a linked list after the node pointed to by afterme? void insert( Node *afterme, int num) { // code goes here A: afterme->link = new Node ; afterme->link ->number = num ; afterme->link->link = afterme->link ; B: Node * tmp = new Node ; tmp->number = num ; afterme->link = tmp ; tmp->link = afterme->link ; C: Node *tmp = new Node ; tmp->number = num ; tmp->link = afterme->link ; afterme->link = tmp ; D: Node *tmp = new Node ; tmp->number = num ; afterme->link = tmp ; tmp->link = NULL ; Page 23 of 25

Question 17. (5 marks). Constructors. Consider the following class definition: class A { private: B b; B *bp ; int value ; public: Write a copy constructor that does a deep copy in the space provided below: Page 24 of 25

Question 18. (5 marks). Call-by-reference, pointers and arrays. Write the output of the following program. (Note that int x[3]={1,2,3 initializes array x with x[0]=1, x[2]=2, and x[2]=3.) #include <iostream> using namespace std; void fun1 (int *p, int *&q) { *p = 100; p = p + 2; *p = *q; *q = *(p+1); cout << "p=" << *p << " q=" << *q << "\n"; bool fun2 (int x[], int size) { int *p = x; int *q; q = p; for (int i=1; i<size; i++) { x[i] += x[i-1]; cout << x[i] << " "; cout << "\n"; fun1 (p, q); return (*p == *q); int main ( ) { int a[5] = {2,4,6,8,10; if (fun2 (a,5)) cout << "true\n"; else cout << "false\n"; for (int i=0; i<5; i++) cout << a[i] << " "; return 0; Page 25 of 25