CSC 212 Final with Solution, Spring 2008

Notre Dame University–Louaize · Program Design and Data Abstraction I · 6 pages · with solution

All CSC 212 files
CSC 212 Final with Solution, Spring 2008, page 1

Stuck on a question? A tutor can walk you through it.

CSC 212 Final with Solution, Spring 2008, page 2 CSC 212 Final with Solution, Spring 2008, page 3 CSC 212 Final with Solution, Spring 2008, page 4 CSC 212 Final with Solution, Spring 2008, page 5 CSC 212 Final with Solution, Spring 2008, page 6

More CSC 212 files

Show the text of this final exam
Notre Dame University CSC 212 — Final Faculty of Natural And Applied Sciences PART I - 10 SHORT QUESTIONS (20 Points: 2 points for each Question ) For the following ten questions circle the letter “T” or the letter “F” to indicate whether or not each of the statements is true or false, respectively. 1. T @ : An exception that is not caught is ignored. Thus the program continues its execution normally. 2.0 F : catch(...) is used to catch any exception type. 3.0 F : throw with no arguments rethrows the same exception type. AG) F : The Seekg() and scekp() functions are used to reposition the “file position pointer”. 5. T «) : The insertion operator "<<" can be used with both sequential and random-access files. 6@ F : Data can be modified in a random access file without destroying other data in the file. 7.7 @ : A stack is first-in, first-out data structure 8. T OQ : A derived class pointer can point to an object of the base class without the need for casting. re) F : Derived class can provide their own implementation of a base class virtual function. But if they do not, the base class implementation is used. 10. F : Any destructor in a class hierarchy can be declared virtual. T F je an > | Ir x 7 “7 T Name: ID number: ool/ 1-1 PART II - 7 PROBLEMS (35 Points) 1. What is the output of the following program when it is executed? (_ Points) #include <iostream.h> void main() { try { for(int val=0; val<6; val++) { cout << "Value is: " << val << endl; if ( val > 1) throw (val) ; } } catch(int v) { cout << "Value caught is: " << v << endl; } catch(...) { cout << "Value 0, 1, 2 are caught\n"; } } void catch _Zero(int v) { cout << "One is caught\n"; } void catch_Two(int v) { cout << "Two is caught\n"; } Value is: 0 % Value is: 1 } Value is: 2 Value caught is: 2 —>) 4 2. What is the output of the following program when it is executed? (_ Points) #include <iostream.h> void bar(int j) { if (j < 0) throw(j); cout << "Result is: " << 100 / j << endl; } void foo(int i) { bar (i); cout << "Value is:" << i << endl; } 001/2-2 void main() { £00 (10); try { foo(-1); } catch(char * s) { cout << "Devide by zero\n"; } : catch(int v) { cout << "No can do!\n"; } A Result is: 10 — Value is:10 — 4 No can do! > 4 - 3. What is the output of the following program when it is executed? (_ Points) #include <iostream.h> #include <fstream.h> struct finals { int vOne; double vTwo; }; void main() { finals fs; ofstream ofObj ("final.dat", ios::binary) ; for (int i=1; i <=10; i++) { fs.vOne = i; fs.vTwo = i + 0.5; of0bj . write (reinterpret_cast<const char*>(&fs) , sizeof (finalS)) ; } ofObj.close(); ifstream ifObj("final.dat", ios::ate|ios::binary) ; ifObj .seekg (sizeof (finalS),ios: :beg) ; ifObj . read (reinterpret_cast<char *>( &£s) , sizeof (finalS) ); cout << fs.vOne <<endl << fs.vTwo << endl; ifObj . read (reinterpret_cast<char *>( &fs) , sizeof (finals) ) ; cout << fs.vOne <<endl << fs.vTwo << endl; _ 0 (py) , WWN ND ul 001/3-3 Given a data file "TEST.DAT" with 6 records of different integer values What is printed following the execution if this program? (Points) 87 #include <iostream.h> 43 #include <fstream.h> 10 class myFile { 23 public: 65 myFile(char * nm) { 12 ifo.open(nm,ios::in) ; } void mk() { bm = ifo.tellg()};} void ps(long mk) { ifo.seekg(mk, ios: :beg) ;} void go() { ps(bm);} int gt() { int i; ifo >> i; return i; } private: ifstream ifo; long bm; }i void main() { myFile mf("test.dat") ; cout << mf.gt() << "- '" cout << mf.gt() << " - "; cout << m£.gt() << " - "; mf .mk () ; mf .ps (0) cout << mf.gt() << "- "; mf.go(); cout << m£.gt() << endl; } J ; 4 87 - 43 - 10 - 87 - 23 4. What is the output of the following program when it is executed? (_ Points) #include <iostream.h> template <class T> class arrc { public: arrC(int sz) { szV = sz; arr = new T[sz]; } void f£1(T); void ot() const; private: int szV; T inv; T *arr; }; 001/4-4 template <class T> void arrC<T>::f1(T in) { inV = in; for (int i=0; i < szV; i++) arr[i] = i + inv; } template <class T> void arrC<T>::ot() const { for (int i=0; i < szV; i++) cout << arr[i] «<< "" } void main() { arrC<double> dArr(5); arrC<int> iArr (6); dArr.£1(0.7); dArr.ot (); cout << endl; iArr.£1(90); iArr.ot(); 0.7.1.7 2.7 3.7 4.7 —)» 90 91 92 93 94 95 #include <iostream.h> class anml { private: int wt; double ht; public: anml(int w=0, double h=0.0) { wt = w>0 ?w: 0; ht = h>0.0?h: 0.0; A 4* , } virtual void a2b() { cout << "Run or Fly from A to B\n"; } virtual void sng() { cout << "I cannot\n"; } virtual void inf () { cout << wt << " " << ht << endl; } 5 ao 5. What is the output of the following program when it is executed? (_ Points) 001/5-5 class dg : public anml { public: dg(int a, int w, double h) : anml(w,h) { ag = a; } void a2b() { cout << "I Run from A to B\n"; } void inf () { anml::inf(); cout << ag << endl; } private: int ag; }i class brd : public anml { public: brd(bool f, int w, double h) : anml(w,h) { fl = f; } void a2b() { if (f1 > 0) cout << "I walk from A to B\n"; else cout << "I Fly from A to B\n"; } void sng() { cout << "Skweek\n"; } private: int £1; }; void main() { brd tt(1, 5, 10.4); dg mx(7, 20, 20.3); anml *aP, &aR=mx; mx.sng(); tt.a2b(); aP = &tt; aP->inf(); aP->a2b(); aP->sng(); aR.inf(); aR.a2b(); aR.sng(); } I cannot Os I walk from A to B — 5 10.4 I walk from A to B I Run from A to B I cannot 001/6 -6