+ All Categories
Home > Documents > University of Toronto Faculty of Applied Science and Engineering...

University of Toronto Faculty of Applied Science and Engineering...

Date post: 21-Mar-2020
Category:
Upload: others
View: 0 times
Download: 0 times
Share this document with a friend
25
Page 1 of 25 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
Transcript
Page 1: University of Toronto Faculty of Applied Science and Engineering …exams.skule.ca/exams/ECE244H1_20149_621445716827Midterm... · 2018-02-03 · Page 3 of 25 Question 2. (5 marks).

Page 1 of 25

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 2: University of Toronto Faculty of Applied Science and Engineering …exams.skule.ca/exams/ECE244H1_20149_621445716827Midterm... · 2018-02-03 · Page 3 of 25 Question 2. (5 marks).

Page 2 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 3: University of Toronto Faculty of Applied Science and Engineering …exams.skule.ca/exams/ECE244H1_20149_621445716827Midterm... · 2018-02-03 · Page 3 of 25 Question 2. (5 marks).

Page 3 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 4: University of Toronto Faculty of Applied Science and Engineering …exams.skule.ca/exams/ECE244H1_20149_621445716827Midterm... · 2018-02-03 · Page 3 of 25 Question 2. (5 marks).

Page 4 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 5: University of Toronto Faculty of Applied Science and Engineering …exams.skule.ca/exams/ECE244H1_20149_621445716827Midterm... · 2018-02-03 · Page 3 of 25 Question 2. (5 marks).

Page 5 of 25

Line Number

Explanation of Error

Page 6: University of Toronto Faculty of Applied Science and Engineering …exams.skule.ca/exams/ECE244H1_20149_621445716827Midterm... · 2018-02-03 · Page 3 of 25 Question 2. (5 marks).

Page 6 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 7: University of Toronto Faculty of Applied Science and Engineering …exams.skule.ca/exams/ECE244H1_20149_621445716827Midterm... · 2018-02-03 · Page 3 of 25 Question 2. (5 marks).

Page 7 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 8: University of Toronto Faculty of Applied Science and Engineering …exams.skule.ca/exams/ECE244H1_20149_621445716827Midterm... · 2018-02-03 · Page 3 of 25 Question 2. (5 marks).

Page 8 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 Smith Your initials are SS Enter your first name followed by your last name: Rachel McDonalds Your initials are RM

Page 9: University of Toronto Faculty of Applied Science and Engineering …exams.skule.ca/exams/ECE244H1_20149_621445716827Midterm... · 2018-02-03 · Page 3 of 25 Question 2. (5 marks).

Page 9 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 10: University of Toronto Faculty of Applied Science and Engineering …exams.skule.ca/exams/ECE244H1_20149_621445716827Midterm... · 2018-02-03 · Page 3 of 25 Question 2. (5 marks).

Page 10 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: (xtop,ytop) and (xbottom,ybottom) 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 11: University of Toronto Faculty of Applied Science and Engineering …exams.skule.ca/exams/ECE244H1_20149_621445716827Midterm... · 2018-02-03 · Page 3 of 25 Question 2. (5 marks).

Page 11 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 12: University of Toronto Faculty of Applied Science and Engineering …exams.skule.ca/exams/ECE244H1_20149_621445716827Midterm... · 2018-02-03 · Page 3 of 25 Question 2. (5 marks).

Page 12 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 13: University of Toronto Faculty of Applied Science and Engineering …exams.skule.ca/exams/ECE244H1_20149_621445716827Midterm... · 2018-02-03 · Page 3 of 25 Question 2. (5 marks).

Page 13 of 25

Question 9. (4 marks). Program Organization. Consider the program below, which is organized into two header files and three .cpp files. What command would you use to compile your program? Write your answer below.

A.h B.h

#ifndef A_H #define A_H class A { private: double d1; public: A(); }; #endif

#ifndef A_H #define A_H #include “A.h” class B { private: A a1; public: B(); }; #endif

#include “A.h” A::A () { }

A.cpp

#include “A.h” B::B () { }

B.cpp

#include “A.h” #include “B.cpp” int main () { A myA; B myB; return (0); }

main.cpp

Page 14: University of Toronto Faculty of Applied Science and Engineering …exams.skule.ca/exams/ECE244H1_20149_621445716827Midterm... · 2018-02-03 · Page 3 of 25 Question 2. (5 marks).

Page 14 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 15: University of Toronto Faculty of Applied Science and Engineering …exams.skule.ca/exams/ECE244H1_20149_621445716827Midterm... · 2018-02-03 · Page 3 of 25 Question 2. (5 marks).

Page 15 of 25

Line Number

Explanation of Error

Page 16: University of Toronto Faculty of Applied Science and Engineering …exams.skule.ca/exams/ECE244H1_20149_621445716827Midterm... · 2018-02-03 · Page 3 of 25 Question 2. (5 marks).

Page 16 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 17: University of Toronto Faculty of Applied Science and Engineering …exams.skule.ca/exams/ECE244H1_20149_621445716827Midterm... · 2018-02-03 · Page 3 of 25 Question 2. (5 marks).

Page 17 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 18: University of Toronto Faculty of Applied Science and Engineering …exams.skule.ca/exams/ECE244H1_20149_621445716827Midterm... · 2018-02-03 · Page 3 of 25 Question 2. (5 marks).

Page 18 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 19: University of Toronto Faculty of Applied Science and Engineering …exams.skule.ca/exams/ECE244H1_20149_621445716827Midterm... · 2018-02-03 · Page 3 of 25 Question 2. (5 marks).

Page 19 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 20: University of Toronto Faculty of Applied Science and Engineering …exams.skule.ca/exams/ECE244H1_20149_621445716827Midterm... · 2018-02-03 · Page 3 of 25 Question 2. (5 marks).

Page 20 of 25

Question 14. (5 marks). Operators. Assume C++ has a binary ! operator defined. And assume the following two statements are valid C++ statements:

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 21: University of Toronto Faculty of Applied Science and Engineering …exams.skule.ca/exams/ECE244H1_20149_621445716827Midterm... · 2018-02-03 · Page 3 of 25 Question 2. (5 marks).

Page 21 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 Includes: prog.cpp A.h, C.h A.cpp A.h, B.h B.cpp B.h B.h C.h C.cpp C.h D.cpp D.h, E.h E.cpp 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 22: University of Toronto Faculty of Applied Science and Engineering …exams.skule.ca/exams/ECE244H1_20149_621445716827Midterm... · 2018-02-03 · Page 3 of 25 Question 2. (5 marks).

Page 22 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 23: University of Toronto Faculty of Applied Science and Engineering …exams.skule.ca/exams/ECE244H1_20149_621445716827Midterm... · 2018-02-03 · Page 3 of 25 Question 2. (5 marks).

Page 23 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 24: University of Toronto Faculty of Applied Science and Engineering …exams.skule.ca/exams/ECE244H1_20149_621445716827Midterm... · 2018-02-03 · Page 3 of 25 Question 2. (5 marks).

Page 24 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 25: University of Toronto Faculty of Applied Science and Engineering …exams.skule.ca/exams/ECE244H1_20149_621445716827Midterm... · 2018-02-03 · Page 3 of 25 Question 2. (5 marks).

Page 25 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; }


Recommended