DOC PREVIEW
Clemson CPSC 870 - Midterm Exam

This preview shows page 1-2-3 out of 8 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 8 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 8 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 8 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 8 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

CpSc 870 Fall 2005Midterm ExamOctober 20, 2005OverviewThere are 110 points on the exam. You will only be graded out of 100 points, so there is apotential for extra credit. Please write your answers on the test sheets, and of c ourse don’tforget to write and sign your name on the top sheet (at least). This is a closed-book,closed-notes, closed-neighbor exam.Best of luck!Question #1 (10 points)Provide clear, concise answers to each of the following questions.1. (3 points) Identify three semantically distinct uses of the const keyword, and de-scribe the effect of the keyword in each case.2. (2 points) Describe two semantically distinct uses of the & operator, and describethe effect of the operator in each case.3. (2 points) In class, we chose to wrap the contents of each header (.h) file with a setof preprocessor directives of the form:#ifndef SOMECLASS#define SOME CLASS 1... class declaration ...#endifDescribe the purpose of this wrapping.4. (3 points) Describe the purpose of the using statement, and identify two distinctforms of its use.Question #2 (20 points)Consider the code fragment shown in Figure 1.1. (4 points) Give the output for /*version 1*/ of main().1CpSc 870 Fall 20052. (4 points) Give the output for /*version 2*/ of main().3. (5 points) Give the output for /*version 3*/ of main().4. (7 points) Give the output for /*version 4*/ of main().Question #3 (5 points)Consider the code fragment shown in Figure 2. Although this code is syntactically correct(and will therefore compile), there are a number of memory bugs that could result inpotential runtime errors.1. (5 points) Please identify all of the memory bugs, including the line numbers wherethe errors occur.Question #4 (15 points)Consider the code fragment shown in Figure 3. The ReversableQueue interface models aqueue that can be reversed in constant time. In ReversableQueueImpl, this is achievedby maintaining a doubly-linked list. The m front pointer always references the Nodecorresponding to the front of the queue. Similarly, the m back pointer always re ferencesthe Node corresponding to the back of the queue.1. (3 points) Assuming that you do not use dummy nodes, provide the complete imple-mentation of the default constructor that would appear in the corresponding .cppfile.2. (5 points) Provide the complete implementation of the enqueue() method thatwould appear in the corresponding .cpp file. (Assume that Object provides animplementation of clone().)3. (5 points) Provide the complete implementation of the dequeue() method thatwould appear in the corresponding .cpp file.4. (2 points) Provide the complete implementation of the reverse() method thatwould appear in the corresponding .cpp file.Question #5 (7 points)Consider the code fragment s hown in Figure 4. The Account class models a simple bankaccount. Each Account object maintains the name of the account holder, and the currentdollar balance in the account.1. (7 points) Provide the implementation of an overloaded assignment operator thatwill allow you to assign one Account object to another.2CpSc 870 Fall 2005Question #6 (13 points)Consider extending the Account class hierarchy with the code fragment shown in Figure 5.The CheckingAccount class models a particular type of account: a checking account. ACheckingAccount object behaves exactly the same as an Account object, but maintainsthe total amount of fees that will eventually be assessed by the bank. (A fee assessmentdoes not affect the account balance.) Each transaction (e.g., deposit(), withdraw(), check-Balance()) results in a $.01 fee being added to the total.1. (3 points) Provide the specialized implementation of the CheckingAccount.deposit()method.2. (10 points) Provide a single implementation of the insertion operator that will allowyou to print both Account and CheckingAccount objects. Apply the streamingpattern discussed in class to print all of the relevant information. You will have toadd methods to Account and CheckingAccount. Be sure to include all relevantcode.Question #7 (30 points)Consider again the ReversableQueueImpl class shown in Figure 3, and the code frag-ments shown in Figure 6.1. (15 points) Provide the necessary implementation to apply the Iterator pattern toReversableQueueImpl. Your implementation should include changes to ReversableQueueImpl,as well as a new class that implements the Iterator interface. Your iteratorshould traverse the queue in reverse order. Be sure to include all relevant code.2. (15 points) Using the Decorator pattern, provide the implementation of a checkingwrapper for objects that derive from ReversableQueue. Your checking wrapperneed only check pre-conditions. Be sure to include all relevant code.Question #8 (10 points)Provide clear, concise answers to each of the following questions.1. (2 points) What is a pre-condition? Give an example.2. (2 points) What is a post-condition? Give an example.3. (2 points) What is an abstraction function? Give an example.4. (2 points) What is a representation invariant? Give an example.5. (2 points) What is a constraint? Give an example.3CpSc 870 Fall 20051 #include <iostream>2 using std::cout; using std::endl;3 class Foo {4 public:5 Foo()6 { cout << "Foo::default" << endl; }7 Foo(const char*const s)8 { cout << "Foo::conversion" << endl; }9 Foo(const Foo& other)10 { cout << "Foo::copy" << endl; }11 ˜Foo()12 { cout << "Foo::destructor" << endl; }1314 void method1(const Foo& other) const {}15 void method2(Foo other) const {}1617 Foo& operator=(const Foo& other)18 {19 cout << "Foo::operator=" << endl;20 return(*this);21 }22 Foo operator+(const Foo& other) const23 {24 cout << "Foo::operator+" << endl;25 Foo retVal;26 return(retVal);27 }28 };2930 void main() /*version 1*/31 { Foo f1, f2("a"), f3 = "b"; }3233 void main() /*version 2*/34 { Foo f1[2], f2 = f1[0]; }3536 void main() /*version 3*/37 {38 Foo f1, f2;39 f1.method1(f2);40 f1.method2(f2);41 f1.method1("a");42 }4344 void main() /*version 4*/45 {46 Foo f1, f2;47 f1 = f2 + f2;48 f1 = f2 + "a";49 }Figure 1: Code Fragment for Question #24CpSc 870 Fall 20051 int*foo1()2 {3 int i = 1;4 return(&i);5 }67 void someMethod()8 {9 int*p1,*p2,*p3;10 p1 = new int;11 p2 = new int;12 p1 = p2;13*p3 = 5;14 p3 = foo1();15 delete(p1);16 delete(p2);17 delete(p3);18 foo1();19 }Figure 2: Code Fragment for Question #35CpSc 870 Fall 20051 #include "Object.h"23 class ReversableQueue4 {5 public:6 virtual void enqueue(const Object& item) = 0;7 virtual Object*dequeue() = 0;8 virtual int getLength() const =


View Full Document

Clemson CPSC 870 - Midterm Exam

Documents in this Course
Load more
Download Midterm Exam
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Midterm Exam and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Midterm Exam 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?