Self Test 4

  1. Which of the following correctly declares an array a such that the initial value of a[2] is 10 and a[3] is 0?
    1. int a[4] = {5,10,15};
    2. int a[4] = {5,10,15,0};
    3. int a[] = {1,5,10};
    4. int a[4] = {1,5,10};
    next prev start

  2. Which line in the code below has a correct declaration of an array?
        1	int main()
        2	{
        3	  const int n = 50;
        4	  int m = 50;
        5	  double a[];
        6	  char b[n];
        7	  float c[m];
        8	  const int d[50];
        9	  ...
       10	}
    
    1. line 5
    2. line 6
    3. line 7
    4. line 8
    next prev start

  3. The function computeSum below is supposed to compute and return the sum of the n elements in the array a. What should cond at line 4 and expr at line 5 be?
        1	int computeSum(int a[], int n)
        2	{
        3	  sum = 0;
        4	  for(int i = 0; /* cond */; i++) {
        5	    sum += /* exp */;
        6	  }
        7	  return sum;
        8	}
    
    1. cond: i <= n and expr: a[n]
    2. cond: i < n and expr: a[n]
    3. cond: i <= n and expr: a[i]
    4. cond: i < n and expr: a[i]
    next prev start

  4. What is the output of the following?
        1	int main()
        2	{
        3	  int a[] = {10,12,14,16,18};
        4	
        5	  for(int i = 1; i < 5; i += 2) {
        6	    cout << a[i] << ",";
        7	  }
        8	  cout << endl;
        9	  return 0;
       10	}
    
    1. 10,14,
    2. 12,16,
    3. 10,12,14,
    4. 10,14,18,
    next prev start
  5. An input file, "data.txt" contains the following:

    1 2
    3 4
    5
    

    Which of the following would correctly read all the integers, store them in the array nums and print "5 values were read and stored"?

    1.     1	int main()
          2	{
          3	  int nums[50];
          4	  int n;
          5	  ifstream ifs("data.txt");
          6	  if (ifs.fail()) {
          7	    cout << "Unable to open input file data.txt" << endl;
          8	    exit(0);
          9	  }
         10	  n = 0;
         11	  while( !ifs.eof() ) {
         12	    ifs >> nums[n++];
         13	  }
         14	  cout << n << " values were read and stored." << endl;
         15	  return 0;
         16	}
      
    2.     1	int main()
          2	{
          3	  int nums[50];
          4	  int n;
          5	  ifstream ifs("data.txt");
          6	  if (ifs.fail()) {
          7	    cout << "Unable to open input file data.txt" << endl;
          8	    exit(0);
          9	  }
         10	  n = 0;
         11	  ifs >> nums[n];
         12	  while( !ifs.eof() ) {
         13	    n++;
         14	    ifs >> nums[n];
         15	  }
         16	  cout << n << " values were read and stored." << endl;
         17	  return 0;
         18	}
      
    3.     1	int main()
          2	{
          3	  int nums[50];
          4	  int n;
          5	  ifstream ifs("data.txt");
          6	  if (ifs.fail()) {
          7	    cout << "Unable to open input file data.txt" << endl;
          8	    exit(0);
          9	  }
         10	  n = 0;
         11	  ifs >> nums[n];
         12	  while( !ifs.eof() ) {
         13	    ifs >> nums[n];
         14	    n++;
         15	  }
         16	  cout << n << " values were read and stored." << endl;
         17	  return 0;
         18	}
      
    4.     1	int main()
          2	{
          3	  int nums[50];
          4	  int n;
          5	  ifstream ifs("data.txt");
          6	  if (ifs.fail()) {
          7	    cout << "Unable to open input file data.txt" << endl;
          8	    exit(0);
          9	  }
         10	  n = 0;
         11	  int tmp;
         12	  ifs >> tmp;
         13	  while( !ifs.eof() ) {
         14	    nums[n] = tmp;
         15	    ifs >> tmp;
         16	  }
         17	  cout << n << " values were read and stored." << endl;
         18	  return 0;
         19	}
      
    next prev start
  6. A function to print the values in an array of n doubles is declared as:

    void print(double a[], int n);
    

    Which line in the code below has a correct call to this print function?

        1	int main()
        2	{
        3	  double a[5]= {1.0, 1.5, 2.0, 2.5, 3.0};
        4	  double b[5]= {1.0, 1.5, 2.0, 2.5, 3.0};
        5	  double c[5]= {1.0, 1.5, 2.0, 2.5, 3.0};
        6	  double d[5]= {1.0, 1.5, 2.0, 2.5, 3.0};
        7	
        8	  print(a[]);
        9	  print(b[5]);
       10	  print(c[], 5);
       11	  print(d, 5);
       12	}
    
    1. line 8
    2. line 9
    3. line 10
    4. line 11
    next prev start
  7. How should the variable str be declared?

        1	int main()
        2	{
        3	  // declaration of str goes here
        4	  char a[] = "Hello";
        5	  char b[] = "World";
        6	
        7	  strcpy(str, a);
        8	  strcat(str, " ");
        9	  strcat(str, b);
       10	}
       11	
    
    1. char str[];
    2. char str[10];
    3. char str[11];
    4. char str[12];
    next prev start
  8. What is the output of the following code?

        1	int main()
        2	{
        3	  char str[] = "minimum";
        4	
        5	  str[3] = '\0';
        6	  cout << str << endl;
        7	  return 0;
        8	}
    
    1. minimum
    2. min0mum
    3. min mum
    4. min
    5. mi
    next prev start
  9. Which declaration of str would cause no compilation error and would guarantee the output to be ABC?

        1	int main()
        2	{
        3	  // Declaration of a here
        4	
        5	  cout << a << endl;
        6	  return 0;
        7	}
    
    1. char str[3] = {'A', 'B', 'C'};
    2. char str[4] = {'A', 'B', 'C' };
    3. char str[3] = {'A', 'B', 'C', '\0'};
    4. char str[] = { "A", "B", "C", 0};
    next prev start
  10. What is the output of this program?

        1	int main()
        2	{
        3	  char nm1[] = "cat";
        4	  char nm2[] = "cabinet";
        5	  char nm3[] = "cab";
        6	  char nm[20] = "catalogue";
        7	
        8	  if ( strcmp(nm1,nm2) < 0 ) {
        9	    if ( strcmp(nm2, nm3) < 0 ) {
       10	      strcpy(nm, nm3);
       11	    } else {
       12	      strcpy(nm, nm2);
       13	    }
       14	  } else {
       15	    if ( strcmp(nm1, nm3) < 0 ) {
       16	      strcpy(nm, nm3);
       17	    } else {
       18	      strcpy(nm, nm1);
       19	    }
       20	  }
       21	  cout << nm << endl;
       22	
       23	  return 0;
       24	}
    
    1. cat
    2. cabinet
    3. cab
    4. catalogue
    next prev start
  11. The function computeBonus is overloaded as shown below.

        1	double computeBonus(int val)
        2	{
        3	  double result = val;
        4	  return 2*result/100.0;
        5	}
        6	
        7	double computeBonus(float val)
        8	{
        9	  double result = val;
       10	  return 2*result;
       11	}
       12	
       13	int main()
       14	{
       15	  int n = 12345;
       16	  double x = 12345.0;
       17	
       18	  cout << computeBonus(n) << ", " << computeBonus(x) << endl;
       19	  return 0;
       20	}
    

    Which of the following are correct?

    1. Output is: 246.9, 24690
    2. Output is: 24690, 24690
    3. Output is: 246.9, 246.9
    4. Output is: 24690, 246.9
    5. Compilation error: Overload resolution fails.
    next prev start
  12. A class Date is defined below. Which line has a syntax error?

        1	class Date
        2	{
        3	public:
        4	  int y;
        5	  int m;
        6	  int d;
        7	  Date();
        8	  Date(int yr, int mth, int day);
        9	}
    
    1. line 1
    2. line 3
    3. line 7
    4. line 8
    5. line 9
    next prev start
  13. Which data members of the class A below are private?

        1	class A
        2	{
        3	  int w;
        4	public:
        5	  int x;
        6	private:
        7	  int y;
        8	  int z;
        9	};
    
    1. only y
    2. only y and z
    3. only w and y
    4. all except x
    next prev start
  14. For the class B defined below, which lines in main will cause a compiler error?

        1	class B
        2	{
        3	private:
        4	  int x;
        5	public:
        6	  int y;
        7	  int z;
        8	};
        9	
       10	int main()
       11	{
       12	  B b;
       13	  b.x = 5;
       14	  x = 5;
       15	  b.y = 10;
       16	  b.z = 15;
       17	
       18	}
    
    1. line 13 only
    2. line 14 only
    3. line 13 and line 14 only
    4. all lines 13 - 16
    next prev start
  15. Which one of the following is false?

    1. The purpose of a class constructor is to initialize the data members of the class.
    2. The return type of a constructor must be void.
    3. The name of a class constructor must be the same as the class name.
    4. A class can have more than one constructor.
    next prev start
  16. For the class Employee definition and main routine below, what is the output?

        1	class Employee
        2	{
        3	  char nm[20];
        4	  int id;
        5	public:
        6	  Employee()
        7	    {
        8	      strcpy(nm, "missing");
        9	      id = 999999;
       10	
       11	    }
       12	  Employee(char name[], int empId)
       13	    {
       14	      strcpy(nm, name);
       15	      id = empId;
       16	    }
       17	  int getId() { return id; }
       18	  const char * getName() { return nm; }
       19	};
       20	
       21	int main()
       22	{
       23	  Employee e1, e2;
       24	  Employee e3("Al", 555111);
       25	
       26	  e2 = Employee("Joan", 561222);
       27	  cout << e1.getId() << " "
       28	       << e2.getId() << " "
       29	       << e3.getId() << " " << endl;
       30	
       31	  return 0;
       32	
       33	}
    
    1. 999999 999999 555111
    2. 999999 561222 561222
    3. 561222 555111
    4. 999999 561222 555111
    next prev start
  17. The class definition of Employee shown above is revised to show only the member function declarations (not their definitions).

    
        1   class Employee
        2   {
        3     char nm[20];
        4     int id;
        5   public:
        6     Employee();
        7     Employee(char name[], int empId);
        8     int getId();
        9     const char * getName();
       10   };
    

    File Employee.cpp should contain the definitions of the member functions. In that file, the definition of getId() should be:

    1. int getId() { return id; }
    2. int Employee.getId() { return id; }
    3. Employee::int getId() { return id; }
    4. int Employee::getId() { return id; }
    next prev start
  18. If A is a class and an array is declared by:

     A arr[10];
    

    which of the following is false?

    1. class A must have a have a default constructor.
    2. The following code will swap the elements in arr[i] and arr[j].
        int tmp;
        tmp = arr[i];
        arr[i] = arr[j];
        arr[j] = tmp;
      
    3. A function f returns an int. The function f also has two parameters: an array of elements of type A and the number of elements in the array. The function f could be correctly declared like this:
      int f(A a[], int n);
      
    4. If A is a class and a and b are variables of type A, then the assignment: a = b; will copy each data member of b into the corresponding data member of a.
    next prev start
  19. A Date is given as

    
    class Date
    {
    public:
      int yr;
      int mth;
      int dy;
      Date();
      Date(int y, int m, int d);
    };
    

    Write a function

     int cmp(const Date& d1, const Date&);
    

    that

    1. returns a negative value if Date d1 comes before d2
    2. returns 0 if Date d1 is equal to d2
    3. returns a positive value if Date d1 comes after d2
  20. Write a function:

    bool arrayEquals(int a[], int b[], int n);
    

    that returns true if the first n values in array a are equal to the corresponding values in array b.

  21. Write a function:

    double avg(int a[], int n);
    

    that computes the average of the n integer values in the array a.

  22. Write a function:

    int numGreater(int a[], int n, int value);
    

    that returns how many of the n numbers in the array a are > value.

  23. Given this Date class:

        1	class Date
        2	{
        3	  int yr;
        4	  int mth;
        5	  int d;
        6	public:
        7	  Date()
        8	  {
        9	    yr = 2005;
       10	    mth = 1;
       11	    d = 1;
       12	  }
       13	  Date(int year, int month, int day)
       14	  {
       15	    yr = year;
       16	    mth = month;
       17	    d = day;
       18	  }
       19	  int getYear();
       20	  int getMonth();
       21	  int getDay();
       22	};
    

    write a declaration of a variable today of type Date initialized with today's date.