View Assessment Result: Multiple Choice Quiz 4



Your performance was as follows:

1.

In a template definition

      template<class T>  ...

the template parameter T ranges over



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

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



2.

Consider the following template specification.

      template<int n>  ...

In use, the template parameter n can be replaced by



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

Correct answer is (d)

Your score on this question is: 0.00

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



3.

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

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



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) II and III
(b) I only
(c) I, II, and III
(d) I and II

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



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 the same for both.
(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 slightly worse for templated functions.

Correct answer is (a)

Your score on this question is: 0.00

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



6.
Which of the following is true about compilation of a templated function or class?

(a) Templates are not handled during compilation.
(b) Compilation takes much, much longer.
(c) Compilation takes less time.
(d) Compilation takes slightly longer.

Correct answer is (d)

Your score on this question is: 0.00

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



7.

What, if anything, is wrong with the following function declaration?

      template<class A, class B>  A  f( B x );


(a) The compiler cannot determine A when f is called.
(b) Nothing
(c) The compiler cannot determine B when f is called.
(d) The parameter should be passed by reference.

Correct answer is (a)

Your score on this question is: 0.00

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



8.

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 a poor choice since templates slow down sorting.
(d) It is impossible since the algorithm cannot know how to compare two instances of type T.

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



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

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 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 can be arbitrary, and the templated function returns the result of calling the second input on the first input.
(d) 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.

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



Go to top of assessment.

Total score: 60.00