Midterm Exam on Object Oriented Design with Problems Solution | CSE 687, Exams of Engineering

Material Type: Exam; Professor: Fawcett; Class: Object Oriented Design; Subject: Computer Engineering; University: Syracuse University; Term: Spring 2002;

Typology: Exams

Pre 2010

Uploaded on 08/09/2009

koofers-user-lcr
koofers-user-lcr 🇺🇸

10 documents

1 / 13

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CSE687 Object Oriented Design MidTerm Exam B2 Spring 2002
Midterm B2 – Instructor’s Solution
Name:_____________________________________ SUID:_____________
This is a closed book examination. Please place all your books on the
floor beside you. You may keep one page of notes on your desktop in
addition to this exam package. All examinations will be collected
promptly at the end of the class period. Please be prepared to quickly
hand in your examination at that time.
If you have any questions, please do not leave your seat. Raise your
hand and I will come to your desk to discuss your question. I will
answer all questions about the meaning of the wording of any
question. I may choose not to answer other questions.
You will find it helpful to review all questions before beginning. All
questions are given equal weight for grading, but not all questions
have the same difficulty. Therefore, it is very much to your advantage
to answer first those questions you believe to be easiest.
pf3
pf4
pf5
pf8
pf9
pfa
pfd

Partial preview of the text

Download Midterm Exam on Object Oriented Design with Problems Solution | CSE 687 and more Exams Engineering in PDF only on Docsity!

Midterm B2 – Instructor’s Solution Name:_____________________________________ SUID:_____________ This is a closed book examination. Please place all your books on the floor beside you. You may keep one page of notes on your desktop in addition to this exam package. All examinations will be collected promptly at the end of the class period. Please be prepared to quickly hand in your examination at that time. If you have any questions, please do not leave your seat. Raise your hand and I will come to your desk to discuss your question. I will answer all questions about the meaning of the wording of any question. I may choose not to answer other questions. You will find it helpful to review all questions before beginning. All questions are given equal weight for grading, but not all questions have the same difficulty. Therefore, it is very much to your advantage to answer first those questions you believe to be easiest.

  1. Implement a function that accepts an STL container and computes the average of its contents. Your function must be able to accept more than one type of container, say either a list or a vector, but you are required to show only that it is correct for a vector. template cont::value_type contAvg(const cont &c) { cont::const_iterator it; cont::value_type sum = 0; if(c.empty()) return sum; for(it=c.begin(); it!=c.end(); ++it) { sum += *it; } return (sum/c.size()); }
  1. Suppose that you are assigned the task of designing a software system to manage recording of student grades for a University. Sketch the relationships, using OMT or UML diagrams, between the 5 most important classes in your design. How would you support the Open/Closed principle in this design? You are not expected to know the details of SU’s student records process. You should simply invent a reasonable process as part of your design. The grade administration process is very stable. It has not changed much in decades. We can support OCP in our software design by providing a protocol for grade reporting and deriving a new class to represent each change that may be required for special programs or new administration rules. grade recording protocol registrar EE&CS student transcript department (^) UniversitySyracuse instructor specialized recording process uses uses
  1. Define a base class B and two derived classes D1 and D2 so that B provides a function that a client can use to create an instance of either derived class, as shown below. B should not have to know the names of the derived classes: D1 pD1 = D1::create(); // creates a D1 object D2 pD2 = D2::create(); // creates a D2 object template class B { public: virtual ~B() {} static T create() { return new T; } virtual void showSelf() { cout << "\n " << typeid(this).name(); } }; class D1 : public B { }; class D2 : public B { };
  1. Please provide the definition of a function that accepts a pointer to a function with the signature int func(X &x), invokes the function, and returns the value computed if positive. Otherwise it throws an exception with an error message. int invoker(int(*fPtr)(X &x), X &x) { int test = fPtr(x); if(test <= 0) { exception e("exception thrown: non positive result"); throw e; } return fPtr(x); }
  1. Given the code fragments below, where the ellipsis (…) represents code that has not been disclosed to you: class X { … }; class Y { public: explicit Y(const X& x); … }; what can you say about the compilation and execution of each of the following statements? Describe each of the operations that occur as this code executes. Y func(Y y) { … } X x; Y y = func(Y(x)); First Statement: This function definition will compile as long as the Y type is known to the compiler and the function body is syntactically correct code. When this function executes a copy constructor for Y is call to pass the y argument by value. This creates a temporary on the func’s stack frame that will be destroyed when the function finishes. The copy constructor is also called to return the result by value, creating another temporary. Y::~Y() is called twice to destroy the two temporary Y objects created by copying. Second Statement: To compile X needs a default constructor which, if not defined by the class, will be generated only if no other constructor is declared by the class. Otherwise compilation will fail. Execution simply calls the default X constructor. Third Statement: This statement always compiles. Since the promotion constructor Y(const X& x) is qualified by the explicit keyword, we cannot simply pass the X::x argument because the compiler will not allow implicit conversions using an explicit constructor. We must explicitly invoke the promotion constructor as shown above. The only constructor called to enter func is the promotion constructor Y(const X& x). This creates a temporary, as described above. The copy constructor Y::Y(const Y& y) is called to pass back the result by value. Two calls to the destructor Y::~Y() are made to destroy the temporary return values.
  1. Please provide the definition of a template-based class that provides a function object that that returns a string representing the type of the object that invoked it and the type of the template parameter. Show how a client will use this function. template class functor { public: std::string operator()() { string types = " invoker type: "; types += typeid(*this).name(); types += "\n template parameter type: "; types += typeid(T).name(); return types; } }; functor myTypes; cout << myTypes().c_str();
  1. Given the declarations: template class B { … private: T &t; } class C : public B<std::string> { … private: const int len; } write a constructor for B that takes a T argument. Write a constructor for C that sets len to the length of its base’s string argument. Where is C’s string argument created? You may assume there are no data members other than the ones shown. template class B { public: //// first part of answer /////// B(const T& targ) : t(targ) {} ///////////////////////////////// virtual void say() { cout << "\n B's data member = " << t; } private: const T t; }; class C : public B<std::string> { public: //// second part of answer ///////////////////////////////// C(const string &s) : B(s), len((int)s.size()) {} //////////////////////////////////////////////////////////// virtual void say() { B::say(); cout << "\n C's data member = " << len; } private: const int len; };

executive. That is, your server modules should satisfy the open/closed principle: they should be open for extension, but closed to modifications.