Print Your Name: Page 1 of 8 Signature: Aludra Loginname: CSCI 102L - Data Structures Midterm Exam #1 Fall 2011 (10:00am - 11:12am, Wednesday, October 5) Instructor: Bill Cheng Problems Problem #1 (24 points) Problem #2 (25 points) Problem #3 (20 points) Problem #4 (16 points) Problem #5 (15 points) Total (100 points) ( This exam is closed book, closed notes, closed everything. No cheat sheet allowed. No calculators, cell phones, or any electronic gadgets. Will adhere to Basic Rules on the Reviews lecture slides. To be eligible for regrades, you must write your entire exam in ink. )
Problem 1. Memory (12 points) Page 2 of 8 (6 points) The figure below shows the code and the corresponding call stack. void foo(int *y) { (*y) += 100; int main() { int x = 102; foo(&x); You are here foo() calls main() stack 0x28cc40 y 0x28cc64 x addresses Let s say that your program has just finished executing the (*y) += 100; statement in foo() and foo() has not returned yet. What would be the numeric values of variables x in main() and y in foo() at this instance of time? Please note that &x is 0x28cc64 and &y is 0x28cc40 (please do not ask what this means because you are suppose to know). You can express your answer in either decimal or hex, whichever you think is more convenient for you. x: y: (6 points) The figure below shows the code and the corresponding call stack. void bar(int b) { b += 10; int main() { int a = 102; bar; You are here bar() calls main() stack 0x28cc40 b 0x28cc60 a addresses Let s say that your program has just finished executing the b += 10; statement in bar() and bar() has not returned yet. What would be the numeric values of variables a in main() and b in bar() at this instance of time? Please note that &a is 0x28cc60 and &b is 0x28cc40 (please do not ask what this means because you are suppose to know). You can express your answer in either decimal or hex, whichever you think is more convenient for you. a: b:
Problem 2. I/O Streams (16 points) Page 3 of 8 (6 points) Let x be a string whose content is unknown and let n be declared as an int. Please write a code segment to use stringstream to extract an integer from x and store the value into n. Please do not perform any error checking in your code. You may assume that everything else (e.g., #include and using statements) have been setup properly. (10 points) Please complete the code segment below to accomplish the following. In every iteration of the for loop, prompt the user to enter a value for x (this is done for you already). Then read an integer from the commandline (i.e., cin). Check if the reading operation is successful (please do not use the exception mechanism). If it is successful, break out of the for loop. Otherwise, print an error message to cerr and ask the user to try again, clear the error in cin, discard up to 1,000 character unless a \n character is encountered in cin s buffer, and go back to the top of the for loop. When you get out of the for loop, print the value of x (this is done for you already). You may assume that everything outside of the for loop (e.g., #include and using statements) have been setup properly. int x = (-1); for (;;) { cout << "Enter x: "; cout << "You entered: " << x << endl;
Problem 3. Struct & Classes (12 points) Page 4 of 8 (4 points) The implementation of getarea() method of the Rectangle class looks like the following (you can assume that the code shown as... are all correct): double Rectangle::getArea() const {... What s the meaning of the keyword const in the above code? (4 points) If you tried to compile the following code, you would get a compiler error saying that Gismo::xRef and Gismo::yRef are uninitialized reference member. class Gismo { private: int& xref; int& yref; public: Gismo(int& x, int& y) { xref = x; yref = y; ; Error Why are you getting this error message? You may assume that everything else have been setup properly. (c) (4 points) Continuing from part, how would you fix that single line of code? Please only give a single line of code to get rid of the compiler error and have xref and yref initialized properly. Please note that you will receive at most 2 points if you just give a correct description without correct code.
Problem 4. Exception Handling & Namespaces (14 points) Page 5 of 8 (4 points) Can you name a couple of things that are bad/ugly about C++ exceptions? (10 points) Please complete the code segment below to accomplish the following. Before the for loop is entered, setup cin to throw the iostream::failure exception (this is done for you already). In every iteration of the for loop, prompt the user to enter a value for x (this is done for you already). Then read an integer from the commandline (i.e., cin). If this caused an iostream::failure exception to be thrown, you need to catch it, print an error message to cerr and ask the user to try again, clear the error in cin, discard up to 1,000 character unless a \n character is encountered in cin s buffer, and go back to the top of the for loop. If an integer is successfully read into x, you need to break out of the for loop. When you get out of the for loop, print the value of x (this is done for you already). You may assume that everything outside of the for loop (e.g., #include and using statements) have been setup properly. int x = (-1); cin.exceptions(iostream::failbit); for (;;) { cout << "Enter x: "; cout << "You entered: " << x << endl;
Problem 5. Operator Overloading (16 points) Page 6 of 8 (8 points) Let the Point class be defined as follows: class Point { public: int x; // no getter and setter since this is public int y; // no getter and setter since this is public Point() { x = 0; y = 0; Point(int newx, int newy) { x = newx; y = newy; ; Let s say that you would like the following code to print p1 at (10,20), p2 at (30,40) : Point p1(10,20), p2(30,40); cout << "p1 at " << p1 << ", p2 at " << p2 << endl; Please write a function to overload cout s << operator so that it would work with any valid Point object. (8 points) Continuing from part and assuming that your implementation was correct, if you would like the following code to print p3 at (98,76), p4 at (54,32) if you entered 98 76 54 32 at the commandline when prompted: Point p3, p4; cout << "Enter p3 and p4: "; cin >> p3 >> p4; cout << "p3 at " << p3 << ", p4 at " << p4 << endl; Please write a function to overload cin s >> operator so that it would work with any valid Point object. Please do not perform any error checking in your code.
Problem 6. Composition & Inheritance (14 points) Page 7 of 8 (4 points) What does the keyword protected mean in a class definition? (6 points) Let the Shape and Triangle classes be defined as follows: class Shape { public: Point center; // no getter and setter since this is public Shape() { center.x = center.y = 0; // default constructor Shape(int newx, int newy) { center.x = newx; center.y = newy; void print() { std::cout << "Shape at " << center << std::endl; ; class Triangle : public Shape { public: int base; // no getter and setter since this is public int height; // no getter and setter since this is public INCOMPLETE Triangle() { base = height = 0; // default constructor Triangle(int b, int h, Point& center)... void print() { std::cout << "Triangle at " << center << std::endl; ; The Point class was defined in an earlier exam question. You may assume that everything else have been setup properly. Please finish the implementation for the constructor of the Triangle class. In order to receive any credit, you must stick to the class definition above. Please make sure that every member in the Triangle class and its superclass are initialized to correct values. (c) (4 points) Continuing from part, why wouldn t the 2nd line of code below compile? Shape s(100,200); // okay Triangle t2 = s; // will not compile, what s wrong?
Problem 7. Dynamic Objects & Polymorphism (16 points) Page 8 of 8 (4 points) What s bad about the code below? Please be as specific as possible. int *bar() { int x = 5; return &x; (4 points) When you have a dynamically allocated array of objects (not array of pointers), why is it necessary to use delete[] instead of just delete to destroy the array? (c) (4 points) What is the Rule of Three? Please be as specific as possible. (d) (4 points) What makes a class an Abstract Base Class? Please be as specific as possible.