Computer Science 2610
Fall 2000
Practice questions for quiz 2

  1. If function t is called, what is printed? Be very careful with this question.
    	void r(int x, int y, int& z)
    	{
    	  cout << "r: x = " << x << " y = " << y 
                << " z = " << z << endl;
    	  x = x + 1;
    	  z = y + x;
    	}
    	 
    	void t()
    	{
    	  int a, b, x;
    	  a = 20;
    	  b = 25;
    	  x = 250;
    	  r(a,b,x);
    	  r(x,a,b);
    	  cout << "t: a = " << a << " b = " << b 
    		 << " x = " << x << endl;
         }
    

    Answer: ________________________________________________________

  2. What is the value of mystery(4)?
    	int mystery(int n)
    	{
    	  if(1 == n) return 1;
    	  else return 3*mystery(n-1);
    	}
    

  3. What is the value of whatisthis(3)?
            int whatisthis(int n)
            {
              if(n == 0) return 1;
              else return whatisthis(n-1) + whatisthis(n-1);
            }
    

  4. You would like to open a file called "mydata.txt" for reading. Write a declaration that will create a object that can be used to read from that file, in a way similar to the way the object cin is used to read from the standard input.

  5. Suppose that variable n has been declared to have type int. You would like to read one integer from file "mydata.txt", using the object created for the preceding problem, and put that integer into variable n. Write a statement that will do that.

  6. You would like to open a file called "myout" for writing. Write a declaration that will create an object that can be used to write to that file, in a way similar to the way the object cout is used to write to the standard output.

  7. Suppose that variable n has been declared to have type int. You would like to write the value of n in file "myout", using the object created for the preceding problem. Write a statement that will do that.

  8. Consider the structure definition
         struct Plant
         {
           int root;
           double leaf;
         };
    
    Write C++ statements that will create an object called bush of type Plant, and will set bush so that its root variable holds 12 and its leaf variable holds 90.0.