Exam 2 - Practice (Answers)


This exam contains questions that may appear in similar form in the actual exam. However, this does not imply that only questions of the type mentioned here will be part of the actual exam,  or that the actual exam will include questions from this practice test. Also, this practice exam contains more questions that the actual exam.


1. Please state, in your own words, the meaning of the following terms:

array, 2-dimensional array, class, methods, fields, opening files for input, opening a file for output, the Strings class, typedef, enum, white-space, EOF, EOL, file position pointer

2. What do the following operations do, in your own words:

If f is of type fstream, explain:

what does f.open(???) do, and what parameters are required
what does the return value of f.fail() indicate ?
what does the return value of f.eof() indicate ?
if x is of type double, what action does f >> x result in ?
if x is of type double, what action does f << x result in ?

If s is of type Strings:

what does f.Position(???) do, and what input parameters are required ?
what does the return value of s.Length() indicate ?
what does s[1] return ?
what does s.GetLine(cin) do ?
what does s.GetLine(f) do, if f is of type fstream ?
what is the difference between s.GetLine(cin) and cin >> s, if anything

why are arrays used as parameters in functions always passed by reference ?

3. Please answer the following questions:

two-dimensional arrays used as parameters in a function are always passed by reference
a variable of type Strings can contain a maximum of 512 characters
a two-dimensional array of doubles with 3 rows and 4 columns can contain 12 double value
every array index in C++ always starts at 0
you can not define arrays of more than 2 dimensions in C++
you can always replace multiple nested if statements with a corresponding switch statement
you can always replace a switch statement with corresponding multiple nested if statements

4. Some functions dealing with arrays are defined as follows:
 

int mystery(int a[], int N)
{
      int answer = 0;
     for (int i = 0; i < N; i++)
         answer += a[i];
      return answer;
}
int mystery1(int a[], int b[], int N)
{
   int answer = 0;
   int count = 0;
   while ((count < N) and (answer == 0))
   {
      if (a[count] != b[count])
         answer = 1;
      count++;
   }
   return answer;
}

 

int mystery2(int a[], int b[], int N)
{
   int k = N - 1;
   for (int i = 0; i < N; i++)
   {
      if (b[k] != a[i])
         return 0;
      k--;
   }
   return 1;
}
int mystery3(int a[], int N)
{
     int k = N-1;
     for (int i = 0; i < k; j++)
     {
        if (a[k] != a[i])
           return 0;
         k--;
     }
      return 1;
}

For the following questions, suppose that A is an array containing the values {1, 2, 3, 4, 5}.

What is the output of mystery(A, 5) ?
What must B contain so that the function mystery1 returns 1 ?
What must B contain so that the function mystery2 returns 1 ?
What must B contain so that the function mystery3 returns 1 ?

 


5.  In this question you are asked to create programs dealing with file input and output.

If a data file "test.dat" contains an unknown number of doubles, one number per line, create a program that computes the sum of those numbers
If a data file "test.dat" contains an unknown number of doubles, one number per line, create a program that copies all number to a second file named "test.out".
If a data file "test.dat" contains an unknown number of doubles, one number per line, create a program that finds the largest and the smallest number, and writes those two values to a second file "maxmin.dat".
Write a program that opens a text data file and counts the number of lines in that data file.


6. If A is a 2-dimensional array of integers with N rows and N columns, then write some code that accomplishes the following: 

retrieve the value of A that is stored in row 2, column 3, and print it to the screen.
store the value -10 in the entry in row 3, column 2.
find the sum of the numbers in column 0.
find the product of the numbers in the last row
find the sum of the numbers along the main, or major, diagonal
find the sum of the numbers along the minor diagonal
find the largest value in the 2-dimensional array
find the smallest value in the 2-dimensional array
print out the entire array, reproducing as best as possible the "table" character of the array

 

7. Suppose a file "InFile" contains the following lines:

I think that I shall never see
A poem lovely as a tree

   - JOYCE KILMER (1914)

and that a program begins with

#include <iostream.h>
#include <fstreamh>

int main(void)
{
   fstream InStream("InFile", ios::in);
   char cc;

What oputput will be produced by each of the following code fragments ?

a)  InStream >> cc;
    while (!InStream.eof())
    {
      cout << cc;
      InStream >> cc;
    }
    InStream.close();
b)  InStream.get(cc);
    while (!InStream.eof())
    {
       cout << cc;
       InStream.get(cc);
    }
    InStream.close();

8. Assume that "InStream" has been opened as a connection to an input file, and that the following declarations are in effect:
 

int N1, N2, N3;
double R1, R2, R3;
char C1, C2, C3, C4;

Suppose further that the input file contains the following data:
 

123  45.6
X78 -909.8 7
-65 $  432.10

List the values that are assigned to each of the variables in the input list, or explain why an error occurs, if necessary:
 

a) InStream >> N1 >> C1 >> C2 >> R1 >> R2>> N2 >> N3 >> C3;
b) InStream >> C1 >> N1 >> R1 >> R2 >> N2 >> C2 >> C3 >> R3
c) InStream >> N1 >> R1 >> C1 >> C2 >> C3 >> N2 >> C4;


9. Suppose the start of a program looks as follows:

const int SIZE = 20;
typedef double Array[SIZE];

Then create functions that accomplish the following:

a) a function that takes as input an Array, and displays it on the screen, one number per line.
b) a function that takes as input an Array, and returns the product of the numbers in that array
c) a function that takes as input an Array, and returns the maximum value of the numbers in the array
d) a function that takes as input an Array, and returns the index of the maximum value of the numbers in the array
f) a function that takes as input an Array, as well as two integers
e) a function that takes as input two arrays A and B, and returns - in an appropriate way - the sum of all corresponding entries of A and B, i.e. it computes A + B;
f) a function that takes as input two arrays A and B, and returns - in an appropriate way - the dot product of the two arrays A and B (as you recall, the dot product of A and B is the sum of  all values in the product A * B)


10. Consider the following code segment:

int ARR[10]; 
ARR[0] = 1;
ARR[1] = 2; 
for (int i = 2; i < 10; i++) 
{ 
   if ( (I % 2) = 1 ) 
      ARR[I] = ARR[I-1] + ARR[I-2]; 
   else 
      ARR[I] := 2 * ARR[I-1]; 
}

(recall that (I % 2) is 0 is I is even and 1 if I is odd) .  What is the value ofARR[4] ?

11. Consider the following program:

        enum Colors = {red=1, green=10, blue=20, invalid=-1};

        void next(Colors c)
        {
           if (c == red)
              return green;
           if (c == green)
              return blue;
           else
              return red;
        }
 
        void previous(Colors c)
        {
           if (c == blue)
              return green;
           if (c == green)
              return red;
           else
             return blue;
        }

        void print(Colors c)
        {
           if (c == Red)
              cout << "red";
           if (c == Green)
              cout << "green";
           if (c == Blue)
              cout << "blue";
        }

        int main(void)
        {
           Colors r = Red, b = Blue;
           cout << r;
           print(r);
           print(next(previous(r));
           return 0;
        }

     Please list what will be shown on the screen.


12.  How would you define a new datatype 'Boolean' that represents the two values 'true' and 'false' ? Define this 'Boolean' datatype in a C++ statement, then write a small function IsPositive that takes as input an integer and delivers the boolean value 'true' if the input value was bigger than zero, and 'false' if the input value was negative or equal to zero.


13. Define a new enumerated datatype 'DayOfWeek' that has values Mon, Tue, Wed, ... , Sun, InvalidDay. Write the functions PrintDay, GetDay, and NextDay that would print out the correct value of a 'DayOfWeek' variable as a string, that would get input from the user to set a variable of type 'DayOfWeek' to the correct value, and that would advance the current value of a variable of type 'DayOfWeek' to the next day of the week, if possible.