Self Test 3

  1. Which of the following is a correct declaration for the C library square root function?

    1. double sqrt(int x);
    2. double sqrt(float x);
    3. float sqrt(int);
    4. float sqrt(float);
    5. double sqrt(double);
    next prev start
  2. What is the return value and type if the C math library function, pow is called with these arguments: pow(2,3)

    1. 0 (int)
    2. 8 (int)
    3. 9 (int)
    4. 8.0 (double)
    5. 9.0 (double)
    next prev start
  3. The function main calls function compoundInterest. Both functions are in the same file. Which one of the following has a incorrect placement of the declaration/definition of compoundInterest?

    1. 
          1	#include <iostream>
          2	#include <cmath>
          3	using namespace std;
          4	
          5	int main()
          6	{
          7	  double amount;
          8	  int mnths;
          9	  double rate;
         10	
         11	  cout << "Enter the initial amount: ";
         12	  cin >> amount;
         13	  cout << "Enter the number of months: ";
         14	  cin >> mnths;
         15	  cout << "Enter the annual rate (e.g. for 6.5% enter 6.5): ";
         16	  cin >> rate;
         17	
         18	  cout << "In " << mnths << " months, the value will be $";
         19	  cout.setf(ios::fixed);
         20	  cout.setf(ios::showpoint);
         21	  cout.precision(2);
         22	  cout << compoundInterest(mnths, rate, amount) << endl;
         23	
         24	  return 0;
         25	}
         26	
         27	double compoundInterest(int months, double annualRate, double initialAmt)
         28	{
         29	  double monthlyRate = annualRate / 1200.0;
         30	  double result;
         31	
         32	  result = pow(1.0 + monthlyRate, months) * initialAmt;
         33	
         34	  return result;
         35	}
      
    2. 
          1	#include <iostream>
          2	#include <cmath>
          3	using namespace std;
          4	
          5	double compoundInterest(int months, double annualRate, double initialAmt)
          6	{
          7	  double monthlyRate = annualRate / 1200.0;
          8	  double result;
          9	
         10	  result = pow(1.0 + monthlyRate, months) * initialAmt;
         11	
         12	  return result;
         13	}
         14	
         15	int main()
         16	{
         17	  double amount;
         18	  int mnths;
         19	  double rate;
         20	
         21	  cout << "Enter the initial amount: ";
         22	  cin >> amount;
         23	  cout << "Enter the number of months: ";
         24	  cin >> mnths;
         25	  cout << "Enter the annual rate (e.g. for 6.5% enter 6.5): ";
         26	  cin >> rate;
         27	
         28	  cout << "In " << mnths << " months, the value will be $";
         29	  cout.setf(ios::fixed);
         30	  cout.setf(ios::showpoint);
         31	  cout.precision(2);
         32	  cout << compoundInterest(mnths, rate, amount) << endl;
         33	
         34	  return 0;
         35	}
      
    3. 
          1	#include <iostream>
          2	#include <cmath>
          3	using namespace std;
          4	
          5	double compoundInterest(int, double, double);
          6	
          7	
          8	int main()
          9	{
         10	  double amount;
         11	  int mnths;
         12	  double rate;
         13	
         14	  cout << "Enter the initial amount: ";
         15	  cin >> amount;
         16	  cout << "Enter the number of months: ";
         17	  cin >> mnths;
         18	  cout << "Enter the annual rate (e.g. for 6.5% enter 6.5): ";
         19	  cin >> rate;
         20	
         21	  cout << "In " << mnths << " months, the value will be $";
         22	  cout.setf(ios::fixed);
         23	  cout.setf(ios::showpoint);
         24	  cout.precision(2);
         25	  cout << compoundInterest(mnths, rate, amount) << endl;
         26	
         27	  return 0;
         28	}
         29	
         30	double compoundInterest(int months, double annualRate, double initialAmt)
         31	{
         32	  double monthlyRate = annualRate / 1200.0;
         33	  double result;
         34	
         35	  result = pow(1.0 + monthlyRate, months) * initialAmt;
         36	
         37	  return result;
         38	}
      
    4. 
          1	#include <iostream>
          2	#include <cmath>
          3	using namespace std;
          4	
          5	int main()
          6	{
          7	  double compoundInterest(int months, double annualRate, double initialAmt);
          8	  double amount;
          9	  int mnths;
         10	  double rate;
         11	
         12	  cout << "Enter the initial amount: ";
         13	  cin >> amount;
         14	  cout << "Enter the number of months: ";
         15	  cin >> mnths;
         16	  cout << "Enter the annual rate (e.g. for 6.5% enter 6.5): ";
         17	  cin >> rate;
         18	
         19	  cout << "In " << mnths << " months, the value will be $";
         20	  cout.setf(ios::fixed);
         21	  cout.setf(ios::showpoint);
         22	  cout.precision(2);
         23	  cout << compoundInterest(mnths, rate, amount) << endl;
         24	
         25	  return 0;
         26	}
         27	
         28	double compoundInterest(int months, double annualRate, double initialAmt)
         29	{
         30	  double monthlyRate = annualRate / 1200.0;
         31	  double result;
         32	
         33	  result = pow(1.0 + monthlyRate, months) * initialAmt;
         34	
         35	  return result;
         36	}
      
    next prev start
  4. What is the error in this function which is supposed to calculate the average of the three float values passed in as paramters?

    
        1	float avg(float a, float b, float c)
        2	{
        3	  return a + b + c/3;
        4	}
    
    1. The divisor should be 3.0 instead of 3.
    2. Line 3 should be: return(a+b+c)/3;
    3. Line 3 should be: return(a+b+c/3.0);
    4. The value to return in a return statement can only be a single variable or a constant. It cannot be any other expression.
    next prev start
  5. Which of the following is not a correct declaration of the function avg from the previous problem?

    1. float avg(float a, float b, float c);
    2. float avg(float x, float y, float z);
    3. float avg(float& a, float& b, float& c);
    4. float avg(float, float, float);
    next prev start
  6. A function named sort3 has three double parameters. If it is called with three variables: sort3(a,b,c), then after the call, it is required that a <= b and b <= c even if this was not the case before the call.

    Which one of the following is false?

    1. sort3 should use call by value and be declared like this: void sort3(double, double, double);
    2. sort3 should use call by reference and be declared like this: void sort3(double, double, double);
    3. sort3 should use call by value and be declared like this: void sort3(double&, double&, double&);
    4. sort3 should use call by reference and be declared like this: void sort3(double&, double&, double&);
    next prev start
  7. The function should return a value that is 1 more than the value passed to it. However, it contains an error. What is the error?

    
        1	int incr(int x)
        2	{
        3	  int x;
        4	  x++;
        5	  return x;
        6	}
        7	
        8	
    
    1. Line 1 should be int f(x)
    2. Line 3 should be deleted.
    3. Line 4 should be ++x
    4. The parameter should be passed by reference.
    next prev start
  8. The rand function returns a pseudo random integer between 0 and RAND_MAX. Assume the value of RAND_MAX is 32767. Which one of the following is a possible output of the program below?

    
        1	#include <iostream>
        2	#include <cstdlib>
        3	using namespace std;
        4	
        5	int next(unsigned int n)
        6	{
        7	  int r;
        8	  r = rand() % n;
        9	  return r;
       10	}
       11	
       12	int main()
       13	{
       14	  for(int i = 0; i < 5; i++) {
       15	    cout << next(10) << " ";
       16	  }
       17	  cout << endl;
       18	  return 0;
       19	}
    
    1. 8 8 3 5 10
    2. 10 10 10 10 10
    3. 9 4 7 5 3
    4. 32767 32767 32767 32767 32767
    5. 38 4753 7 550 6475
    next prev start
  9. If the program in the previous problem is run several times,
    1. the output for any two runs will be the same.
    2. the output for two runs will most likely not be the same, but could occasionally be the same.
    3. the output for any two runs will most likely be the same, but could occasionally be different.
    4. the output for two runs will never be the same.
    next prev start
  10. Line 14 has been added to the code below.
    
        1	#include <iostream>
        2	#include <cstdlib>
        3	using namespace std;
        4	
        5	int next(unsigned int n)
        6	{
        7	  int r;
        8	  r = rand() % n;
        9	  return r;
       10	}
       11	
       12	int main()
       13	{
       14	  srand(735);
       15	  for(int i = 0; i < 5; i++) {
       16	    cout << next(10) << " ";
       17	  }
       18	  cout << endl;
       19	  return 0;
       20	}
    

    If this modified program is run several times,

    1. the output for any two runs will be the same.
    2. the output for two runs will most likely not be the same, but could occasionally be the same.
    3. the output for any two runs will most likely be the same, but could occasionally be different.
    4. the output for two runs will never be the same.
    next prev start
  11. Which is the correct statement about the following program?
    
    
        1	#include <iostream>
        2	using namespace std;
        3	
        4	void f(int& x, int& y)
        5	{
        6	  int tmp;
        7	  if ( x < y ) {
        8	    tmp = x;
        9	    x = y;
       10	    y = tmp;
       11	  }
       12	}
       13	
       14	int main()
       15	{
       16	  int a = 10;
       17	  int b = 5;
       18	  f(b,a);
       19	  cout << "a = " << a << ", b = " << b << endl;
       20	  return 0;
       21	}
    
    1. Compiler error, function f should have a return statement.
    2. Compiler error, the header of function f should be void f(int x, int y).
    3. The output is: a = 5, b = 10
    4. The output is: a = 10, b = 5
    next prev start
  12. What is the output of the following program?

    
        1	#include <iostream>
        2	using namespace std;
        3	
        4	int f(int n, int k);
        5	
        6	int main()
        7	{
        8	  cout << f(8, 0) << endl;
        9	
       10	  return 0;
       11	}
       12	
       13	int f(int n, int k)
       14	{
       15	  if ( n > 1 ) {
       16	    return f(n/2, k+1);
       17	  } else {
       18	    return k;
       19	  }
       20	}
    
    1. 8
    2. 4
    3. 3
    4. 2
    5. 1
    next prev start
  13. What is the output of the following program?

    
        1	#include <iostream>
        2	using namespace std;
        3	
        4	int f(int n);
        5	
        6	int main()
        7	{
        8	  cout << f(8) << endl;
        9	
       10	  return 0;
       11	}
       12	
       13	int f(int n)
       14	{
       15	  int k = 0;
       16	  while(n > 1) {
       17	    n /= 2;
       18	    k++;
       19	  }
       20	  return k;
       21	}
    
    1. 8
    2. 4
    3. 3
    4. 2
    5. 1
    next prev start
  14. A header file named myutils.h to declare a pair of user defined functions is shown below. What should be changed?

    
        1	#ifdef MYUTILS_H
        2	#define MYUTILS_H
        3	
        4	int next(int);
        5	void init();
        6	
        7	#endif
    
    1. Line 4 should provide a name for next's formal parameter.
    2. Line 5 is missing the parameter list for function init.
    3. Lines 1, 2, and 7 should each be terminated with a semicolon.
    4. Line 1 #ifdef should be #ifndef.
    5. No changes are needed.
    next prev start
  15. What line causes a compiler error in the following program?

    
        1	#include <iostream>
        2	using namespace std;
        3	
        4	void swap(const int& x, const int& y);
        5	
        6	int main()
        7	{
        8	  int a = 5, b = 10;
        9	  swap(a,b);
       10	  cout << a << endl;
       11	  cout << b << endl;
       12	  return 0;
       13	}
       14	void swap(const int& x, const int& y)
       15	{
       16	  int tmp = x;
       17	  x = y;
       18	  y = tmp;
       19	}
    
    1. Lines 4 and 14
    2. Line 8
    3. Line 9
    4. Line 16
    5. Lines 17 and 18
    next prev start
  16. For the C library function int abs(int); which one of the following statements is correct?
    1. The precondition is: The argument passed to abs should be any int value.

      The postcondition is: The return value will be a non-negative int value which is equal to the parameter value passed in if that is already non-negative or else the negative of the parameter value.

    2. The precondition is: The return value will be a non-negative int value which is equal to the parameter value passed in if that is already non-negative or else the negative of the parameter value.

      The postcondition is: The argument passed to abs should be any int value.

    3. The precondition is: The argument passed to abs should be any int value except for the most negative int value.

      The postcondition is: The return value will be a non-negative int value which is equal to the parameter value passed in if that is already non-negative or else the negative of the parameter value.

    4. The precondition is: The return value will be a non-negative int value which is equal to the parameter value passed in if that is already non-negative or else the negative of the parameter value.

      The postcondition is: The argument passed to abs should be any int value except for the most negative int value.

    next prev start