View Assessment Result: Multiple Choice Quiz 4



Your performance was as follows:

1.
What is the purpose of class templates?

(a) To avoid the use of dangerous pointers
(b) To improve the portability of code
(c) To increase the efficiency of the class methods
(d) To easily specify a group of related classes

Correct answer is (d)

Your score on this question is: 10.00

Feedback:
   See section 1.4.1 of the course notes.
   (d) No Feedback



2.

In a template definition

      template<class T>  ...

the template parameter T ranges over



(a) only built-in types
(b) only user-defined classes
(c) classes as well as structs
(d) all types

Correct answer is (d)

Your score on this question is: 10.00

Feedback:
   See section 1.4.1 of the course notes.
   (d) No Feedback



3.

Consider the following template specification.

      template<int n>  ...

In use, the template parameter n can be replaced by



(a) int values only
(b) any type of value
(c) int values and int containers only
(d) arithmetic type values only

Correct answer is (a)

Your score on this question is: 10.00

Feedback:
   See section 1.4.1 of the course notes.
   (a) No Feedback



4.

Consider the following templated array copy function and arrays.

      template<class T>  void  copy( T a[], T b[], int n ) {
          for(int i = 0; i < n; i++ ) a[i] = b[i];
      }
      char   C[10], CC[10];
      int    I[10];
      float  F[10];

Which of the following calls to copy would produce a compile time error?



(a) copy( I, (int*)F, 10 );
(b) copy( I, F, 10 );
(c) copy( I, I, 10 );
(d) copy( C, CC, 10 );

Correct answer is (b)

Your score on this question is: 10.00

Feedback:
   See section 1.4.1 of the course notes.
   (b) No Feedback



5.
Which of the following is true about the run-time efficiency of using templated functions versus using nontemplated functions?

(a) The run-time efficiency is better for templated functions.
(b) The run-time efficiency is the same for both.
(c) The run-time efficiency is slightly worse for templated functions.
(d) The run-time efficiency is much worse than for templated functions.

Correct answer is (b)

Your score on this question is: 10.00

Feedback:
   See section 1.4.1 of the course notes.
   (b) No Feedback



6.

Consider the following outline of a templated sorting function.

      template<class T>  void  sort( T a[], int n ) { ... }

For a given sorting algorithm S, which of the following is true about using this outline to implement S?



(a) It is a poor choice since templates slow down sorting.
(b) It is impossible since the algorithm cannot know how to compare two instances of type T.
(c) It is a reasonable way to implement S.
(d) It is a poor choice since it does not work with linked lists.

Correct answer is (c)

Your score on this question is: 10.00

Feedback:
   See section 1.4.1 of the course notes.
   (c) No Feedback



7.

Consider the following outline of a templated array class.

      template<class T>  class Array { ... }

Which of the declarations below is in obvious error with respect to using such a class?



(a) Array<int> A(10,0);
(b) Array<int> A;
(c) Array<int> A(10);
(d) Array<int> A<int>;

Correct answer is (d)

Your score on this question is: 10.00

Feedback:
   See section 1.4.1 of the course notes.
   (d) No Feedback



8.

Consider the following outline of a templated array class.

      template<class T>  class Array { ... }

Which of the following declarations are in error with respect to using such a class?

  1. Array<Array<int> > A;
  2. Array<Array<int>*> A;
  3. Array<int**> A;


(a) I only
(b) II only
(c) III only
(d) None

Correct answer is (d)

Your score on this question is: 10.00

Feedback:
   See section 1.4.1 of the course notes.
   (d) No Feedback



9.

Consider the following templated function.

   template<class T> T Apply( T x, T (*ff)(T) ){ return ff(x); }

Which of the following is true concerning its use?



(a) The input function can be arbitrary, and the templated function returns a pointer to the result of calling the second input on the first input.
(b) The input function must return the same type as its argument, and the templated function returns the result of calling the second input on the first input.
(c) The input function must return the same type as its argument, and the templated function returns a pointer to the result of calling the second input on the first input.
(d) The input function can be arbitrary, and the templated function returns the result of calling the second input on the first input.

Correct answer is (b)

Your score on this question is: 10.00

Feedback:
   See section 1.4.1 of the course notes.
   (b) No Feedback



10.

Consider the following templated function.

  template<class Func>
  void mystery( Func f, int n, typename Func::result_type& x ) {
          for( int i = 0; i < n; i++ )
              x += f(i);
  }

If the first actual parameter of a call to mystery is a function object f that properly defines result_type and if n0 and x0 are the values of the second and third actual parameters, which of the following accurately describes the result of executing that call?



(a) x0+f(0)+f(1)+...+f(n0)is stored in x.
(b) f(0), f(1), ..., and f(n0) are stored in x in succession.
(c) f(0), f(1), ..., and f(n0-1) are stored in x in succession.
(d) x0+f(0)+f(1)+...+f(n0-1)is stored in x.

Correct answer is (d)

Your score on this question is: 10.00

Feedback:
   See section 1.4.1 of the course notes.
   (d) No Feedback



Go to top of assessment.

Total score: 100.00