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: 
100 200 300
400.5   6.7    Bert Wachsmuth
#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: 
I think that I shall never see
A poem lovely as a tree
           - JOYCE KILMER (1914)
#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: 
I think that I shall never see
A poem lovely as a tree
           - JOYCE KILMER (1914)


4. Please write some code segments to solve the following tasks 

  1. You are to find out the amount of numbers as well as the sum of all numbers contained in a data file "test.dat". The data file contains an unknown numbers of integers, one integer per line. 
  2. You are to find the largest and the smallest number in a data file. The data file contains an unknown number of doubles, one double per line. 
  3. Write a program to copy one text file into another text file but in the output text files all lines should be numbered 1, 2, 3, ... with a number at the left of each line. 

5. Please write some code segments to solve the following tasks: 

  1. Write a function 'trim' that takes as argument a String and returns a String which is the same as the input String but with all leading and trailing blanks removed. 
  2. Write a function to determine if a string is a palindrome. Recall that a palindrom is a string that reads backwards the same as forward. For example, "MADAM" and "ABLE WAS I ERE I SAW ELBA" are palindromes, while "BERT WACHSMUTH" is not a palindrome. For extra credit, use recursion to implement the palindrome checking function. 
  3. One data file "last.dat" contains a list of 20 Strings reprepsenting last names,  a file "first.dat" contains 20 Strings representing fist names, and a file "middle.dat" contains 20 strings representing middle names for 20 individuals. Write a program that reads the complete names from the three files, and outputs them in the format "First M. Last", i.e. full first name, then a space, then the first letter of the middle name, then a period, then a space, then the full last name. Output could go to the screen, if you like. 

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
Create a Student class with three fields: a Strings field for the name, a double field for the GPA, and an int field for the student ID. Also add two methods:
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
Create a more complex Student class that has the above fields for name, GPA, and student ID, but it also has a field containing an array of 6  doubles, and another integer for the actual number of courses. This array is used to store the grade (in numeric format) of up to 6 courses the student takes in one semester, and the additional integer stores the actual amount of courses the student took during the semester (up to 6, therefore). Add the following methods:
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