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.

COMP322 - Introduction to C++

University of Toronto

University of Toronto

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

CSC1322 Object-Oriented Programming Concepts

University of Toronto

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

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.

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

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

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

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

Definition Matching (10 Points)

Operator overloading: extra examples

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

CMSC 202 Midterm Exam 1 Fall 2015

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

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

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

The University of Nottingham

A506 / C201 Computer Programming II Placement Exam Sample Questions. For each of the following, choose the most appropriate answer (2pts each).

C++_ MARKS 40 MIN

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

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

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

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

Review Questions for Final Exam KEY

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

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

ComS 228 Exam 1. September 27, 2004

Review Questions for Final Exam

True or False (12 Points)

CSE 333 Midterm Exam Nov. 3, 2017 Sample Solution

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)

ECE 461 Fall 2011, Final Exam

CS 11 C++ track: lecture 1

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

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

Short Notes of CS201

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

Midterm Review. PIC 10B Spring 2018

Implementing an ADT with a Class

Introduction to Programming using C++

CSCE 110 PROGRAMMING FUNDAMENTALS

For Teacher's Use Only Q No Total Q No Q No

CS201 - Introduction to Programming Glossary By

Function Overloading

C++ 8. Constructors and Destructors

CSC 309/404 Section 901/910 Spring 2017 Midterm Exam Due: May 7 (Sun) 2015, 11:59 pm

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

1. Which of the following best describes the situation after Line 1 has been executed?

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

Object Oriented Design

Pointers, Dynamic Data, and Reference Types

Ch02. True/False Indicate whether the statement is true or false.

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

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

CS24 Week 3 Lecture 1

True or False (15 Points)

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

Lecture 8: Object-Oriented Programming (OOP) EE3490E: Programming S1 2017/2018 Dr. Đào Trung Kiên Hanoi Univ. of Science and Technology

Solved Exercises from exams: Midterm(1)

CS201 Latest Solved MCQs

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

True or False (14 Points)

ECE 462 Fall 2011, Second Exam

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

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

Lab 2: Pointers. //declare a pointer variable ptr1 pointing to x. //change the value of x to 10 through ptr1

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

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

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

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

EINDHOVEN UNIVERSITY OF TECHNOLOGY Department of Mathematics and Computer Science

ECE Fall 2017, Third Exam

CE221 Programming in C++ Part 1 Introduction

Midterm Practice Exam

CS201- Introduction to Programming Current Quizzes

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

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

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

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

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

Lab # 02. Basic Elements of C++ _ Part1

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.

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

Student Number: Computer Science 211b Final Examination. 28 April hours

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

CPE Summer 2015 Exam I (150 pts) June 18, 2015

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

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

1 Short Answer (5 Points Each)

CS 2604 Homework 1 C++ Review Summer I 2003

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

Class. SSE2034: System Software Experiment 3, Fall 2018, Jinkyu Jeong

Abstract Data Types. Different Views of Data:

Constructor - example

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

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 18

Question 1. (10 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 18

Question 2. (10 marks). Functions and Objects. (a) 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. (b) Consider a non-member function called NoAction, which takes a single object of type Time and returns a single object also of type Time. You may assume that the class Time is correctly implemented and that Time.h is included. Which of the following implementations of this function is correct? Indicate your answer by placing an X in the appropriate column in the table. Time NoAction(Time & t) { Time temp; temp = t; return (t); Time NoAction(Time & t) { Time temp; temp = t; return (temp); Time & NoAction(Time & t) { Time temp; temp = t; return (t); Time & NoAction(Time & t) { Time temp; temp = t; return (temp); Implementation Correct? Has a problem? Time NoAction(Time & t) { Time* temp = new Time(); *temp = t; return (temp); Time & NoAction(Time & t) { Time* temp = new Time(); *temp = t; return (temp); Page 3 of 18

Question 3. (10 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 not need all the rows of the table. Should there be multiple errors on a line, use multiple entries in the table. You can list the errors in any order. 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 18

Line Number Explanation of Error Page 5 of 18

Question 4. (8 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 18

(a) How many times is a constructor of class Foo 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 times is a constructor called in the execution of main? (d) How many times is the destructor called between points A and B in the code? (e) How many times is the destructor called in the execution of main? Page 7 of 18

Question 5. (6 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 18

Question 6. (6 marks). Constructors and Stringstreams. 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, assume no errors in s 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 18

Question 7. (13 marks). Arrays and Objects. Consider the following definition of the class Resistor, which is similar to the one you used in one of your lab assignments. Assume it has been correctly implemented. class Resistor { private: double resistance; string name; int endpointnodeids[2]; public: Resistor(); // First constructor Resistor(double resistance_); // Second constructor ~Resistor(); string getname() const; double getresistance() const; void setresistance (double resistance_); void print (); ; (a) (1 mark). Write a C++ declaration to declare an array called A of 20 Resistor objects. (b) (1 mark). Which constructor is used to initialize each of the array elements in the declaration above? Write the name of the constructor (see class definition above), or NONE if no constructor is invoked. (c) (2 marks). Given an integer variable n, write C++ code to dynamically allocate an array called da of n Resistor objects. (d) (1 mark). Which constructor is used to initialize each of the array elements in the declaration in part (c) above? Write the name of the constructor (see class definition above), or NONE if no constructor is invoked. Page 10 of 18

(e) (2 marks). Given an integer variable n, write C++ code to dynamically allocate an array of n pointers to Resistor objects. Call the array dpa. (f) (3 marks). Write C++ code to dynamically allocate n Resistor objects and have each object pointed to by an element of the array declared in part (e) above. Objects pointed to by even-indexed elements should be initialized using the default constructor. Objects pointed to by odd-indexed elements should be initialized using the second constructor with the resistor value set to the index value. Hint: use the modulus operator %. (g) (3 marks). Write C++ code to de-allocate the objects and array allocated in parts (e) and (f) above so that no memory leaks exist. Page 11 of 18

Question 8. (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 (see figure below). 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 12 of 18

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 13 of 18

Question 9. (8 marks). Operator Overloading. 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 ; How many operator+ functions does Joe have to define and implement? How many of those can be a member of the class? Page 14 of 18

Question 10. (10 marks). Classes and Objects. Consider the following declaration and implementation of the class Complex in the file Complex.h. 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; Now consider the implementation of the class in Complex.cpp. #include Complex.h #include <iostream> using namespace std; Complex::Complex() { real = new float; *real = 0.0; imag = new float; *imag = 0.0; Complex::Complex(float r, float i) { real = new float; *real = r; imag = new float; *imag = i; float Complex::getReal() const {return (*real); float 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 15 of 18

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()); int 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 16 of 18

Question 11. (6 marks). Constructors. Consider the following class definition: #include B.h // Contains the declaration of class B class A { private: B b; B *bp ; int value ; : public: : Write a copy constructor that does a deep copy. Include both the constructor s header and its body. Page 17 of 18

Question 12. (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 18 of 18