View Assessment Result: Multiple Choice Quiz 4



Your performance was as follows:

1.

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



2.

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, F, 10 );
(b) copy( I, I, 10 );
(c) copy( C, CC, 10 );
(d) copy( I, (int*)F, 10 );

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



3.

Consider the following templated array copy function and arrays.

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

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

  1. copy( F, C, 10 );
  2. copy( I, C, 10 );
  3. copy( C, F, 10 );


(a) I
(b) II
(c) III
(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



4.

Consider the following templated swap function and data types.

      template<class T>  void  swap( T& a, T& b ){
          T tmp = a; a = b; b = tmp;
      }
      char   c1, c2;
      int    i1, i2;
      float  A[10];

Which of the following calls to swap produces a compile time error?

  1. swap( i1, c2 )
  2. swap( c1, i2 );
  3. swap( A[5], A[2] );


(a) I, II, and III
(b) I and II
(c) II and III
(d) I only

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.

Consider the following templated swap function.

      template<class T>  void  swap( T& a, T& b ){
          T tmp = a; a = b; b = tmp;
      }

Determining the right choice of type parameter T occurs at which of the following times?



(a) Run time only
(b) In part at compile time, and in part at run time
(c) Link time
(d) Compile time only

Correct answer is (d)

Your score on this question is: 0.00

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



6.
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 slightly worse for templated functions.
(b) The run-time efficiency is much worse than for templated functions.
(c) The run-time efficiency is better for templated functions.
(d) The run-time efficiency is the same for both.

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



7.

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

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



8.

Consider the following outline of a templated array class.

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

Which of the following is true about using this outline to implement a general container class?



(a) It is impossible since the algorithm cannot know how to copy an instance of type T.
(b) It is a poor choice since templates slow down compilation.
(c) It is a poor choice since the type matching will slow down access at runtime.
(d) It is a reasonable way to implement such a class.

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 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);
(b) Array<int> A(10,0);
(c) Array<int> A<int>;
(d) Array<int> A;

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



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: 90.00