CSC 213 Final with Solution, Fall 2005
Notre Dame University–Louaize · Program Design and Data Abstraction II · 13 pages · with solution
Showing the first 10 of 13 pages. Download the PDF to see all of it.
More CSC 213 files
- Final with Solution · Spring 2008Final exam · with solutionView
- Final · Spring 2008Final examView
- Lecture 1 · Spring 2013LabView
- Lecture 1 · Spring 2013LabView
- Problems Assignment 1 · Fall 2012ProblemsView
- Problems Assignment 2 · Fall 2012ProblemsView
- Midterm · Spring 2008ExamView
- Useful File Operator Precedence and Associativity · Fall 2007FileView
Show the text of this final exam
Notre Dame University CSC 213 — Final
Faculty of Natural And Applied Sciences
Name:_
94
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.
ct ©:
c7. T (4):
CT @:
20 F:
10.1) F
~~
An exception that is not caught is ignored. Thus the program continues its execution
normally.
: catch(...) is used to catch any exception type.
: throw with no arguments rethrows the same exception type.
: The Seekg() and seekp() functions are used to reposition the “file position
pointer”.
: The insertion operator <<” can be used with both sequential and random-access
files.
: Data can be modified in a random access file without destroying other data in the
file.
A stack is first-in, first-out data structure
A derived class pointer can point to an object of the base class without the need for
casting.
Derived class can provide their own implementation of a base class virtual function.
But if they do not, the base class implementation is used.
: Any destructor in a class hierarchy can be declared virtual.
1/12
PART II - 7 PROBLEMS (35 Points)
A) What is the output of the following program when it is executed? (5 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:
catch(...) {
cout << "Value 0, 1, 2 are caught\n";
}
"<< v << endl;
}
void catch_Zero(int v) {
cout << "One is caught\n"; ~
} Value ts: ©
void catch_Two(int v) {
cout << "Two is caught\n"; \ :
} ght\ Value we 4
| Value wl a '
Vo hae Comalat nm a |
2. What is the output of the following program when it is executed? (5 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- 12
void main() {
foo(10);
wy VO
try { Rawk ow
foo(-1); "
} Vatae wy ne
catch(char * s) {
cout << "Devide by zero\n";
} No ton do
catch(int v) {
cout << "No can do!\n";
}
}
3. What is the output of the following program when it is executed? (5 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;
ofObj . 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 *>( &fs), sizeof (finals) );
cout << fs.vOne <<endl << fs.vTwo << endl;
ifObj . read (reinterpret_cast<char *>( &fs),sizeof(finalS) );
cout << fs.vOne <<endl << fs.vTwo << endl;
}
a3
wo ‘ 001/3-12
a
4. Given a data file 'TEST.DAT" with 6 records of different integer values
What is printed following the execution if this program? (5 Points)
#include <iostream.h>
#include <fstream.h>
class myFile {
public:
myFile(char * nm) {
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; C1 - 4% -\0
};
void main() {
myFile mf("test.dat");
cout << mf.gt() << "- "; $3 - 4% ~
cout << mf.gt() << " - ";
cout << mf.gt() «<< "- "; H
mf .mk() ; '
mf.ps (0); H
cout << mf.gt() << "- "; \ ;
mf.go(); . ~
cout << mf.gt() << endl;
5. What is the output of the following program when it is executed? (5 Points)
#include <iostream.h>
template <class T> gant oy
class arrc { ewe
public: ~
arrC(int sz) { © <4@)
SzV = sz;
arr = new T[sz];
void f1(T);
void ot() const;
private:
int szvV;
T inv;
T *aryr;
};
001/4-12
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 << arrfi] << " ";
}
void main() {
arrC<double> dArr (5); Vk, ne) EN a iN EN ™
arrC<int> iArr(6); \o \ Voy WOON \s \\ \
darr.£1(0.7); ay) ia) ay .9) Qs.
dArr.ot();
cout << endl;
iArr.£1(90);
iArr.ot();
6. What is the output of the following program when it is executed? (5 Points)
#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;
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;
}
001/5- 12
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 (£1 > 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 fl;
};
void main() {
brd tt(1, 5, 10.4);
dg mx(7, 20, 20.3);
anml *aP, &aR=mx; TZ eamanslt
mx.sng(); 5
tt.a2b(); Lund Sram A bo &
aP = &tt; -
aP->inf(); 10.4
aP->a2b();
aP->sng(); ZL wele Deon At
aR.inf();
aR.a2b(); Cc \
aR.sng(); Ki week,
} do 20.3
7
LD tonasl
001/6- 12
7. What is the output of the following program when it is executed? (5 Points)
#include <iostream.h>
class dnc {
public:
dnc(int i=0) { st(i);}
void st(int i) {val=i;}
int gt() const {return val; }
private:
int val;
}i
class abc {
public:
abC(int nb=2) { ae
sz = nb; “2%
comP = new dnC [nb];
void prt (int x=10) const {
if (x < sz)
cout << comP[x] .gt()
else
cout << "Noting to print...";
<< "4;
}
void fl(int i=10) {
for (int j=sz; j >=0; j--)
comP [j] .st(j+i);
}
private:
int sz;
dnc *comP;
abc
obj
obj
obj
obj
obj
obj (6); i
-prt(1); j
.£1(100) ; |
-prt (6);
-prt (3);
-prt (0);
Na (26\
wf ot)
toS
bol
bor
}; -
void main() { © NoPriea ye pent, _, los YOO
001/7 - 12
PART III - THREE PROBLEMS (10 +17 +18 Points)
1. Write a program to read an integer number from a keyboard, and then to search for
it in a sequential data file called "value.dat" that contains records of one integer
field.
The program should also write to the screen the word ‘FOUND’ or the word
‘NOT-FOUND' depending on the results of the search.
Note: Use the space bellow to write your answer. You may use the back of the page if necessary.
x cnlule Zoltar. W>
RH inlado < LX reo wy
m onddde Laura. &
ho
001/8- 12
2. Having a random data file named " salary.dat" that contains information about a
company’s employees and their monthly salaries. The file records have the
following structure: | Employee Id (an integer.),
Employee first name (maximum of 20 characters.),
Employee family name (maximum of 20 characters.),
Employee monthly salary (a double).
The company has decided to increase the monthly salary of all employees by a
percentage of 5.3%.
Write a C++ program that will adjust the monthly salary information in the
“salary.dat” file to meet the company’s salary increase decision.
Implement all the necessary error check to earn full grades.
Note: Use the space bellow to write your answer. You may use the back of the page if necessary.
nip aoe emploat ie.
\odudi Liat ren WS 3
res aeoy
# nclade < wh ®ns VS Gov lank f9);
dvulle
\
‘. merle o. (o, Wr ts Soe) * yp fey
001/9- 12
©: Rod (eintiepret_conk Cohn 2) (A bixk\) cy (CNY.
3. Given the following geometric categories, with these definitions:
Car
Truck
Vehicle
Sedan (a kind of sports car)
Wagon (a kind of carriage for example to sell on it vegetables)
Knowing that all object have names, maximum speed and number of
passengers (the driver is included), moreover they have two member function
print the name and move:
a) Write the deepest possible inheritance hierarchy
b) Write only the following member functions:
* Constructors for each class
* printName to print the name of the form
* move to go forward ot backward or left or right in meters
" Overloaded operator << to print the data members value of the object
c) Declare static objects in main and call the appropriate functions
d) Declare dynamic objects in main and call the appropriate functions to
prove the inheritance as well as polymorphism.
Note: Use the space bellow to write your answer. You may use the back of the page if necessary.
Kinclude Citar WD
& inhade Lr¥rns.: hy
» inchoate CJ iam in, Pp wN\
clowns voice
$ grind DMX ews & epucalone P£4 ( Arun Ay vowed.
proteked :
Chor nom \ine! .
~s
Whie Comoe Pa, uk by uke) {
001/10 - 12