CS 1112 Practice Exam 1
1. Please answer, in your own words, the following questions:
what are strings, objects, fstream object ? | |
what are the features of an object, or class ? | |
what do the keywords private and public indicate ? | |
if cc is a character variable and f an input stream, what is the difference between using f >> cc and f.get(cc) | |
please list at least three different methods of the Strings class, and explain briefly what they do | |
if S is a Strings variable, what is the difference between using cin >> S and S.GetLine(cin) |
2. Please answer true or false to the following questions:
it is possible to have an array of objects | |
it is possible to include an array as a field for an object | |
strings are always 1024 characters long | |
when you open a file to read from, the file is automatically erased | |
objects can have fields of different types | |
arrays can have elements of different types |
3. Please list the output of the following little programs.
#include <iostream.h> #include <fstream.h> int main(void) { fstream f; int a,b; double x,y; char cc, dd, ee; f.open("test.dat",ios::in); cin >> a; cin >> cc; cin >> b; cin >> x; cin >> y; cin >> dd; cin >> ee cout << a << cc << b << x << y << dd << ee; return 0; } |
Data File "test.dat" contains the following lines: |
#include <iostream.h> #include <fstream.h> int main(void) { fstream in("infile.dat", ios::in); char cc; in >> cc; while (!in.eof()) { cout << cc; in >> cc; } in.close(); } |
Data File "infile.dat" contains the following lines: |
#include <iostream.h> #include <fstream.h> int main(void) { fstream in("infile.dat", ios::in); char cc; in.get(cc); while (!in.eof()) { cout << cc; in.get(cc); } in.close(); } |
Data File "infile.dat" contains the following lines: |
4. Please write some code segments to solve the following tasks
5. Please write some code segments to solve the following tasks:
6. Please write code segments to solve the following tasks. Also, for each of the classes below, provide a small test program that checks every method at least once in a complete program.
Create a Length class with two fields, one double for the number and one character for the scale ('M' for meters, 'I' for inchches). Also include the following methods in your class: |
void input(void) - prompts the user for the number and scale and stores the answers in the corresponding fields | |
double toInches(void) - converts the length to inches | |
double toMeters(void) - converts the length | |
void display(void) - to display the information in the current object |
void input(void) - prompts the user for the information and stores the answers in the corresponding fields. All answers should initially be entered into a Strings variable and converted to the correct type, if necessary, to avoid crashing your program due to invalid user input. | |
void display(void) - to display the information in the current object |
void input(void) - as before, but now also asks for the number of courses and then for the grade (numerically) for each course. Note that this method should not ask for the GPA as input (see below) | |
void display(void) - as before, but also shows the number of courses and the total GPA | |
void compute(void) - to compute the GPA based on the courses taken that semester. You can assume that all courses carry the same weight so that the GPA is simply the average of the course grades |