CSAS1111 - Practice Final Exam

Disclaimer: These are some sample problems only. They may not appear in the actual final exam, and the actual final exam may contain problems different from the ones found here.
Note: You should also refer to the previous two practice exams as preparation for the final. The questions contained there are also good candidates for the final exam.

Describe, in your own words, the meaning of the following terms: source code, machine code, compiling, linking, double,  int, char, void, largest (smallest) int or double, CPU, RAM, secondary memory, value parameter, reference parameter, array, scope of variables, class, fields, methods, 2-dimensional arrays, fstreams, opening files for input/output, white space, Strings class, methods of the Strings class, enumerated types, constants, arrays as parameters, program design, main function, if, switch, for, while, return, =, ==, ||, &&, cout, cin 

Here is a list of important tpocis we covered. For each topic, you should be able to write a small program, as well as to analyze a sample program using that concept (see below for examples):

Parameter Passing, Files, Strings, Arrays, 2-D Arrays, Enumerated Types

Basic Structures

What type must x, y, z, and w be if we assign them the values 1, 1.0, '1', and "1", respectively.

Determine the value of the indicated variable after execution of each program segment.

   int i = 4;
   while (I < 8)
      i += 3;

What is i ?

int j;
for (int i = 0; i < 5; i++)
   j = 2*i;

What is j ?

int i = 10;
int j = 4;
int k = 10 / 4;
int l = 10 % 4;

What is k and l ?

int j;
switch (i)
   case 0: j = 2*i; break;
   case 1: j = -i; break;
   default: j = -2*i;

What is j if i = 1 ? What is j if i = -1 ?

Consider the following code segment:

   if ((Balance >= 2000) && (Balance <= 3000))
      cout << "Your balance is between $2000 and $3000";
      cout << "Your interested rate will be 3.5%";
      cout << "Your balance is larger than $3000.";
      cout << "Your interest rate will be 4.5%";

Do you think this code does what the programmer intended ? If not, improve this code.

Assume that the definitions for two functions look as follows:

            int Function1(double x)
            void Function2(double x)

Give some sample code that would show how each function could be used. Also, at least one of the two function must contain a special keyword - which function and which keyword ?

Convert the following nested if statement into an equivalent switch statement.

   if (Grade == 'A') || (Grade == 'B')
   else if (Grade == 'C') || (Grade = 'D')

Parameter Passing:

If a function is defined as int What(double x, int& y, double A[]), then which variables are passed by reference, which ones are passed by value, and what type, if any, does this function return ?

Consider the following function:

void Unknown(double& x, double y, int &I, int J)
   x = x + y;
   y = x + y;
   I = I + J;
   J = I + J;
double X = 10.0, Y = 20.0;
int I = 30, J = 40;
cout << X << Y << I << J;
Which parameters are value parameters, which are reference parameters ?
Can you call the function as in Unknown(1, 2, 3, 4) ? How about Unknown(1, Z, 2, K) if Z is defined as double and K as integer ? How about Unknown(Z, Z+W, K, L) if Z and W are defined as double and K and L as integer ?
What is the output when the function Unknown is used as in the code segment below:

Consider the following functions:

   void What1(int J)
      J = 3 - J;
   void What2(int& J)
      J = 3 - J;
   int main(void)
      I = 1;
      J = 15;

What is the value of J when the main function is finished ? 

Classes and Strings:

List at least three methods of the Strings class we discussed in class, and explain in general terms what they can be used for..

What, if anything, is (are) the basic difference between arrays and classes ?
Can I define a class that contains one or more arrays ?
Can I define an array of classes ?

Use the appropriate method of the Strings class to write a small program that defines a variable of type Strings representing a name of a person  and reads the name from standard input (keyboard). Note that most people's names contains spaces.

Explain what the following functions do:

int why(Strings s1, Strings s2)
   if (s1.Position(s2) >= 0)
      return 1;
      return 0;
int when(Strings s)
   if (s[s.Length()-1] == ';';
      return 1;
      return 0;
Strings what(String s)
   while (s[s.Length()-1] == ' ')
      s = s(0,s.Length()-2);
   return s;

Note that if s is a Strings, and p1, p2 are integers, then the expression s(p1, p2) returns a Strings consisting of all characters from s with index between p1 and p2, including p1 and p2. For extra credit, explain conditions where the last function will not perform as expected and create a runtime error instead.

Files (fstream)

For sample questions about file input/output, please refer to the second practice exam.



   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];
         ARR[I] := 2 * ARR[I-1];
Recall that (I % 2) is 0 is I is even and 1 if I is odd.
What is ARR[4]:

In a program that uses arrays for handling some mathematical operations,
the following constant and types were defined:
   cont int MaxIndex = 15;
   typedef double MathArray[MaxIndex];
The main function defines variables A, B, and C of type
   MathArray A, B, C;
Write segments of C++ codes for each of the following:
Add the first and the last term in the array A together and write the result on the screen (code segment only)
Add the corresponding entries for A and B together and store the result in C (code segment only)
Write a function that takes as input an array of type MathArray and produces the index of the largest real number contained in that array.

Write a function that computes the sum of an array. The
input to the function should be an array of an arbitrary size and an integer
indicating the length of the array currently used as input. The function
should deliver a single double number that is the sum of all entries in
the array. Also write a small but complete program to test your function.
Do the same as above but for finding the product, average, minimum, or

Same as above but instead of delivering a double value as answer the
function should print each element in the array in backwards order (i.e.
starting with the last array element).The function should not return any

For further examples, please refer to the problems in the second practice

2-D Arrays

Suppose A is a 2-dimensional array of integers with 3
rows and 4 columns. How do you define this variable A in C++ ? How would
you define it and at the same time initialize it to all zero entries ?
How would you define it and at the same time initialize it to all entries
having the value 2 ?

Write a segment of C++ code that would retrieve the value of A that
is stored in row 2, column 3, and print it to the screen.

Write a segment of C++ code that would store the value -10 in the entry
in row 3, column 2.

Write a function that takes a 2-D array with 4 rows and 4 columns as
input, and delivers a single number as output. The output should be: (a)
the sum of row 2, or (b) the sum of column 3, or (c) the sum of the diagonal,
or (d) the sum of the minor diagonal, or (e) the sum of all entries in
the table.

Write a function that takes a 2-D array with 4 rows and 4 columns as
input and prints out the entire 2-D array in table form. 

Enumerated Types:

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;
         return red;
   void previous(Colors c)
      if (c == blue)
         return green;
      if (c == green)
         return red;
        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;
      return 0;
Please list what will be shown on the screen. 

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. 

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. 


The final will contain several questions that require simple, short answers
(usually true/false). For practice, refer to the prior exams and practice