PIC 10A sec. 3


Instructor: Ivo Dinov, Asst.Prof.

Mathematics, Neurology, Statistics


Midterm Study Guide


Exam: Fri., May 04, 2001, 12:00-12:50 PM, UCLA LS 2147



I. Part One:


1 Answer the following questions:

a)How many bits are in 1 byte? .

b)How many bytes are in 1MB?

c)Computers process data under the control of sets of instructions called computer ………….?

d)What are the  logical units of the computer?

f)What are the programs that translate high-level language programs into machine language called?


2 Fill in the blanks in each of the following sentences about the C++ environment.

a)C++ programs are normally typed into a computer using an ……….program.

b)In a C++ system, a program executes before the compiler’s translation phase begins………….

c)The program ………….combines the output of the compiler with various library functions to produce an executable image.

d)The program …………..transfers the executable image of a C++ program from disk to memory.


3 Fill in the blanks in each of the following.

a)Every C++ program begins execution at the function ……… .

b) ……..  begins the body of every function and ………… ends the body of every function.

c)Every statement ends with a …...

d)The escape sequence \n represents the character which causes the cursor to

position to the beginning of the next line on the screen.

e)The statement ……..is used to make decisions.


4 State whether each of the following is true or false .

   If false, explain why. Assume the statement using std::cout; is used.

a)Comments cause the computer to print the text after the //on the screen when the program is executed.

b)The escape sequence \n when output with cout causes the cursor to position to the beginning of the next line on the screen.

c)All variables must be declared before they are used.

d)All variables must be given a type when they are declared.

e)C++ considers the variables number and NuMbEr to be identical.

f)Declarations can appear almost anywhere in the body of a C++ function.

g)The modulus operator (%) can be used only with integer operands.

h)The arithmetic operators *,/,%,+and -all have the same level of precedence.

i)A C++ program that prints three lines of output must contain three output statements using cout .


5 Write a single C++ statement to accomplish each of the following

a)Declare the variables c ,thisIsAVariable ,q76354 and number to be of type int .

b)Prompt the user to enter an integer. End your prompting message with a colon (:)followed by a space and leave the cursor positioned after the space.

c)Read an integer from the user at the keyboard and store the value entered in integer variable age .

d)If the variable number is not equal to 7 ,print "The variable number is not equal to 7".

e)Print the message "This is a C++ program" on one line.

f)Print the message "This is a C++ program" on two lines where the first line ends

with C++.

g)Print the message "This is a C++ program" with each word of the message on a separate line.

h)Print the message "This is a C++ program"with each word separated from the next by a tab.


6 Write a statement (or comment) to accomplish each of the following

a)State that a program will calculate the product of three integers.

b)Declare the variables x ,y ,z and result to be of type int .

c)Prompt the user to enter three integers.

d)Read three integers from the keyboard and store them in the variables x ,y and z .

e)Compute the product of the three integers contained in variables x ,y and z ,and assign the result to the variable result .

f)Print "The product is "followed by the value of the variable result .

g)Return a value from main indicating that the program terminated successfully.


7 Using the statements you wrote in Exercise 6,write a complete program that calculates and displays the product of three integers. Note: you will need to write the necessary using statements.


8 Identify and correct the errors in each of the following statements , if there are any:

a)if (c <7 );

cout <<"c is less than 7 \n";

b)if (c =>7 )

cout <<"c is equal to or greater than 7 \n";



                      II. Part Two:


Multiple choice questions may have more than one correct answer. The student is to give all correct answers to multiple choice questions. True-false questions require explanation. Other questions may require an explanation.


1. Describe top down design. Give some of the advantages of top down design.


2. Which of the following are true, which are false? Why?


Top down design provides the following advantages and/or disadvantages:

   a) makes a program easier to understand

   b) makes a program harder to change

   c) makes a program easier to test and debug

   d) makes a program easier to maintain

   e) makes team programming easier


3. On every system there is a file you must include to use the math library. Show how to do this, and explain what is in this file.


4. Mark the following True or False, and explain.


   a) The header files, such as math.h, contain the libraries.

   b) The header files, such as math.h, contain declarations of the

       functions in the associated library, such as the math library.

   c) The functions in the math library are attached to your program

       by the compiler during the linking phase of compilation.

   d) On the system I use to do my homework, I have to tell the compiler

     to connect the library functions to my program.



5. Here is a complete function that purports to return one of the roots of a quadratic given the coefficients of the square, a, linear, b, and constant, c, terms. It fails to compile. Why?


   //returns one of the roots of the quadratic equation

   //a*x*x + b*x + c = 0

   double root1 (double a, double b, double c)


       return (-b + sqrt(b*b - 4 * a * c))/(2*a);



6. What if anything is wrong with the following #include <...> directives?

   a) #include < iostream.h>

   b) #include <Iostream.h>

   c) #include <iostream.h>

   d) #include <iostream>

   e) #include iostream.h


7. Matching: Which of the header files listed first below contain declarations of the function on the second list?


   A. iostream.h

   B. stdlib.h

   C. math.h


   a) ____ int abs(int);

   b) ____ double sqrt(double);

   c) ____ cout << a_double;

   d) ____ double fabs(double);

   e) ____ double pow (double base, double exponent);

   f) ____ long labs(long);

   g) ____ cout.setf(/* formatting constant(s) */);


8. Given the following include directive (to get the declaration for the pow function from the math library):


      #include <math.h>


Now make these declarations:


      double base = 2, exponent = 3, power = 4;


Which of the following are correct invocations for the pow function? If any of the following is correct, give the value assigned, and if

apparently incorrect, explain.


   a) power = pow(base, exponent);

   b) pow(power, base, exponent);

   c) pow(base, exponent) = power;

   d) base = pow(exponent, power);


9. Convert the following mathematics expressions to C++ expressions. Use the declarations provided. Use library functions only where you cannot do without. Include the appropriate header file for any library function used. Be sure you initialize any variables whose values you are using to reasonable values for the library functions you are using.


      int x, y; //declaration for a) and b)

      a) y = x3

      b) y <= |x|


      double x, y, z, area; //declaration for c) through e).

   c) z = x1.6

   d) z = area

   e) p =


10. None of the following assignments do what the programmer expects. Give, or describe the actual effect or result, and correct the code to get a result that may be reasonably expected. (You may add casts, changing double to int or int to double, etc.)


   a) double z = 9/4;

   b) int PI = 3.1415926;

   c) double C, F;

      F = (9/5) * C + 32.0;

   d) double C, F;

      F = double(9/5) * C + 32.0;


11. Write code that declares x, y, and z as double variables. Then write code that causes z to be assigned the result of x divided by

y, rounded as indicated below. Be sure to #include the header file that declares the library functions you use.


   a) round up

   b) round down

   c) round to the nearest integer. Does your code round an exact half up or down? say 2.5?


12. Each of the following lines of code purport to round the results of the division of doubles to the nearest integer value (but still of

type double). All are correct C++ code but some do not round correctly. Tell which rounds down, up, or to the nearest integer

value, or is not reasonable

Assume that math.h has been included, and that all variables have appropriate values.


   double x, y, z;

   a) z = ceil(x/y);

   b) z = ceil(x/y - 0.5);

   c) z = floor(x/y - 0.5);

   d) z = floor(x/y + 0.5);

   e) z = floor(x/y);


13. Declare (give a prototype for) a function named average_grade. This function returns  a double and has four double

arguments, test1, test2, test3, test4. Be sure to include a "prototype comment" that tells briefly what the function does.


14. Define a function named average_grade. This function returns a double and has four double arguments, test1, test2, test3,

test4. The return value should be the average, or arithmetic mean of the four arguments. Be sure to include a comment that tells

briefly what the function does.


15. Give an outline for the general form of a programmer defined function.


16. Given the function,


   // returns the sum of the arguments.

   double Sum (double a, double b)


       return a + b;



Write statement that calls to this function. The call should pass the values of doubles x and y, and assign the returned value to a

double, z.


17. In your own words discuss similarities and differences between a function and a small program.


18. What output is produced by the following program? Explain briefly.


   // file Test.cc

   #include <iostream>

   using namespace std;


   char mystery(int first_par, int second_par);

   int main()


       cout << mystery(10, 9) << "ow" << endl;

       return 0;


   // what this does is the test question is all about!

   char mystery(int first_par, int second_par)


       if (second_par >= first_par)

           return 'W';


           return 'H';



19. When you call a function there must be a match between the ________ and _________ of  arguments and parameters


20. Given the function declaration (prototype), does the compiler complain or compile if you call this using the following line? If the

compiler complains, what is the complaint?


   //if score >= min_to_pass, returns 'P' for passing,

   //else returns 'F' for failing.

   char grade (int score, int min_to_pass);

   double fscore;

   char fgrade;

   int need_to_pass;

   int main()


       //omitted code to get values for variables

       //fscore and need

       fgrade = grade(fscore, need);

       return 0;



21. What should the contents of the 'prototype comment' be? The prototype comment is the comment that the text suggests

strongly should accompany a function declaration (prototype).


22. The text compares function usage to the idea of black box. Describe in your words what a black box is.


23. In your own words, discuss procedural abstraction.


24. True or False: Every programmer must know all the details of what that programmer's team mates are doing in their projects to

do the work assigned. Why?


25. True or False: Procedural abstraction involves information hiding in that only the 'contract' between the programmer using the

function (the client) and author of a function is known to either.


26. Procedural abstraction requires the author of a function and the client (user) of that function to know and not to know certain

things. Remark on who needs to and who should not know each of the following items


   a) the requirements on the parameter values,

   b) exactly what the return value is to be,

   c) the implementation details, and

   d) the details of how the function is used by the client.


27. Which of the following variable names will likely contribute to clearly readable code. Why or why not?


   a) x

   b) xy

   c) xylene_volume

   d) length

   e) jamesage

   f) velocity

   g) acceleration

   h) u

   i) position

   j) cost_of_pizza


28. Given the declarations,


double radius, diameter;


which pairs of the following are the following produce equivalent results? Why? (The why is as important as the equivalence of the



   a) radius = diameter/2.0;

   b) radius = diameter/2;

   c) radius = diameter/double(2);

   d) All of the above produce the same results.


29. In testing code having an if or an if-else clause, describe how to construct test data. Give an example based on:


   if (x > y)



       if (u > v)





30. Answer the question at the end of this code.


   const int x = 1.23456789;

   int f(int y)


       int z;

       //body of function: int f(int)


   const int w = 9.987654321;

   int g(double u)


       int v;

       // body of function: int g(double)


   int main ()


       int m;

       double nn;

       // body of function main()

       m = g(nn);

       // ...

       m = f(x);

       return 0;



In which functions can each variable be accessed?


   a)x           e) w

   b)f           f) g

   c)y           h) v

   d)z           i) m



31. Why not make all variables non constant global variables, and avoid the bother of parameters for functions?


32. Describe the similarity and difference between a function's parameters and function's local variables.


III. Part Three:


             True-False and short answer questions require explanation.


1. Define stream, and give some examples.


2. Define object, and give some examples.


3. What are the characteristics that files have that ordinary program variables share? What are characteristics of files that are different from ordinary variables in a program?


4. True or False: The following are characteristics that a file and an int variable have in common.

 a) information can be retrieved from both

 b) both are known out side the program

 c) information can be stored in both

 d) can exist before, after, as well as during the program execution

 e) large quantities of information can be stored in either

 f) neither is known before or after a program executes, is known only during execution.


5. To use a file there are several steps that must be carried out. Give them, together with an example fetching information into a program variable. You select the types.


6. What is wrong with the following program:

            #include <fstream>

     using namespace std;


     int main()


         ifstream in_stream;

         in_stream.open(mydata );

         // other statements

         return 0;



7. What is wrong with the following program:

   #include <fstream>

   using namespace std;


   int main()


     ifstream in_stream;

     char myfile[21];

     cout << "Enter a file name to be processed: " << endl;

     cin >> myfile


     // other statements

     return 0;



8. True or False: A file in use in a program may be referred to by either the external file name or the program variable that is connected to the file in the open statement.


9. True or False: A file when ready for use in a program has two names. If true, what are they? If false, explain.


10. A programmer has read half of the lines in a file. What must the programmer do to the file to enable reading the first line a second time?


11. Name at least three member functions associated with an iostream object, and give examples of usage of each.


12. With streams, the operators << and >> have a convenient rule to tell which goes with cin and which goes with cout. State such a rule.


13. Define class, and give some examples of classes. Where are your example classes declared (what header file) and where are they defined?


14. Give the syntax for calling a member function,


where this function is a member of object Calling_Object.


15. Give code to open a file named infile.dat for reading, where the stream variable is in_stream. Show how to determine whether it is safe to proceed with read operations.  You are to terminate the program if it isn't safe to read. Be sure to include necessary #include files.


16. You have been writing a file whose external name is outfile.dat, with stream variable out_stream. You reach a point where you no longer need to send output to the file outfile.dat. How do you close these files?


17. You have been writing a file whose external name is outfile.dat, with stream variable out_stream. You reach a point where you no longer need to send output to the file outfile.dat. What are some reasons why you may need to close these files?


18. Here is a code segment that copies three integers from infile.dat to outfile.dat. What changes are necessary to make the output go to the screen? (The input is still to come from infile.dat.)

   // Problem for test. copies three int numbers between files.

   #include <fstream>

   using namespace std;


   int main()


     ifstream in_stream;

     ofstream out_stream;



     int first, second, third;

     instream >> first >> second >> third;

     out_stream << "The sum of the first 3" << endl

                << "number in infile.dat is " << endl

                << (first + second + third) << endl;



     return 0;



19. Give the include directive you need for each of the following functions. There are both library functions and member function of classes here.

There are two answers to at least one of these.

  a) exit

  b) close

  c) abs

  d) isupper

  e) sqrt

  f) fabs

  g) setf

  h) pow

  i) open


20. Declare a string variable, file_name capable of holding a 15 character file name.


21. Given the following string variable declaration and input statement.

  #include <iostream>

  using namespace std;


  char name[21];

  cout >> name;

Suppose this code segment is embedded in a correct program. What is the longest name that can be entered into the string variable name?


22. In formatting output, the following constants are used with the ios member function setf. What effect does each have?

  a) ios::fixed

  b) ios::scientific

  c) ios::showpoint

  d) ios::showpos

  e) ios::right

  f) ios::left


23. What output will be produced when the following code is executed? (Assume these lines are embedded in complete, correct programs, with

proper #include directives.)

   cout << "*";


   cout << 123

        << "*" << 123 << "*" << endl;

   cout << setw(5) << 123 << "*" << 123 << "*" << endl;


24. What output is produced by the following code, assuming these lines of code are embedded in a correct program?

   cout << "*" << setw(5) << 123;


   cout << "*" << setw(5) << 123;


   cout << "*" << setw(5) << 123 << "*" << endl;


25. What output is produced by the following code, assuming these lines of code are embedded in a correct program?

   cout << "*" << setw(5) << 123 << "*"

        << 123 << "*" << endl;


   cout << "*" << setw(5) << 123 << "*"

        << 123 << "*" << endl;



   cout << "*" << setw(5) << 123 << "*"

        << setw(5) << 123 << "*" << endl;


26. What output is sent to the file out.dat by the following code, assuming these lines of code are embedded in a correct program?

   ofstream fout;


   fout << "*" << setw(5) << 123 << "*"

        << 123 << "*" << endl;


   fout << "*" << setw(5) << 123 << "*"

        << 123 << "*" << endl;



   fout << "*" << setw(5) << 123 << "*"

        << setw(5) << 123 << "*" << endl;


27. What happens to output when data is sent to the output stream width that is wider than that set with the setw(int) manipulator, or equivalently, with cout.width(int)?


28. What is sent to screen when the following is executed, assuming that these lines of code are embedded in a correct, complete program? Explain

this behavior.

   cout << "*" << setw(3) << 123456 << "*" << endl;


29. You have to #include <iostream> and #include <iomanip> when you use a line of code such as

   cout << setw(8) << 123456 << endl;

but not when you do the exact equivalent thing, as in


   cout << 123456 << endl;

Why is this?


30. Write a function that will copy the contents of file in.dat to the file out.dat. Check for successful file opening of both in.dat and out.dat. The loop that actually does the copy should terminate on end of file.