View Assessment Result: Multiple Choice Quiz 4



Your performance was as follows:

1.
The purpose of function templates is to

(a) easily specify a family of operationally equivalent functions
(b) help the type-checker search for errors
(c) replace generic void* pointers in C
(d) increase the efficiency of various algorithms

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.
What is the purpose of the keyword typename?

(a) The same purpose as that of typedef
(b) To increase the efficiency of template compilation
(c) To denote a generic type
(d) To point out to the compiler that whatever follows is the name of a type

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



4.

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

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



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

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

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



6.

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

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


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

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.

If templates were removed from C++, which of the following would be true?

  1. Some algorithms could no longer be implemented.
  2. Any particular algorithm could still be implemented, but often less elegantly.
  3. Any particular algorithm could still be implemented, but the efficiency would often be catastrophically worse.


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

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



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



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 can be arbitrary, 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 must return the same type as its argument, and the templated function returns the result of calling the second input on the first input.

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



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) f(0), f(1), ..., and f(n0) are stored in x in succession.
(b) x0+f(0)+f(1)+...+f(n0-1)is stored in x.
(c) x0+f(0)+f(1)+...+f(n0)is stored in x.
(d) f(0), f(1), ..., and f(n0-1) are stored in x in succession.

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