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:
2. What do the following operations do, in your own words:
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:
8. Assume that "InStream" has been opened as a connection to an input file,
and that the following declarations are in effect:
9. Suppose the start of a program looks as follows:
10. Consider the following code segment:
(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.