Exam 3

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

Given that you have two versions of a function that are the same except that one expects some integer parameters, and the other expects a float and an integer parameter, which parameters would you change to a T in order to make this a template function? A) the parameter that is an integer in one function and a float in the other function. B) the parameter that always stays an integer C) Both parameters change. D) Neither parameter changes.

A

Given the following definition for a map, which code fragment is valid? A) map<int, string> mymap; B) mymap[10] = "hello"; C) mymap["hello"] = 3; D) mymap[3, "hello"] = 10; E) mymap.push_back(Pair(3, "hello"));

A

Given the following function template, which of the following are NOT valid calls to larger? template <class T> T larger(const T& left, const T& right) { if(left > right) return left; else return right; } A) char x[]="3", y[]="4"; cout << larger(x,y); B) int x=3, y=4; cout << larger(x,y); C) char x='3', y='4'; cout << larger(x,y); D) float x=3, y=4; cout << larger(x,y);

A

If a function will possibly throw an unhandled exception, the try block should A) encompass the function call. B) be in the function definition. C) be in the catch block. D) not be used.

A

If the following function will throw a string exception, then void myFunction( ); A) the function definition and declaration should have a throw list. B) the function should have an empty throw list. C) the function definition, but not the declaration should have a throw list. D) all of the above.

A

In which container does the position of an inserted element depend on the data, NOT the order of insertion? A) associative containers B) sequence containers C) fraternal container D) container adapters

A

The following class definition class MyError {}; A) has only a default constructor. B) has no member functions or member data. C) is illegal. D) A and B

A

Using templates in your program where warranted, is an example of A) algorithm abstraction. B) information hiding. C) data abstraction. D) polymorphism

A

Which of the following is NOT a member function of the stack adapter template? For members of stack, specify any needed arguments. A) front() B) top() C) empty() D) size() E) push()

A

Which would be the correct way to instantiate a containerClass object in your main program? template <class T> class containerClass { public: containerClass(); containerClass(int newMaxSize); containerClass(const containerClass& source); ~containerClass(); T getItem(); int getCount(); int getSize(); void addItem(T item); private: T *container; int maxSize, count; }; A) containerClass<int> myContainer; B) containerClass myContainer; C) containerClass<T> myContainer; D) containerClass myContainer<int>

A

Why can you not use the swap template function to swap two complete arrays? template <class T> void swap(T& left, T& right) { T tmp=left; left=right; right=tmp; } A) the = operator does not work for an array. B) You cannot pass an array to a function. C) The swap function does not return anything. D) tmp should be an integer.

A

Writing a template class A) allows us to write one class definition that can hold different data types. B) means we never have to write non-template classes again. C) allows us to skip the implementation of that template class. D) is illegal.

A

I have an algorithm that runs in O(N1/2), where n is the size of the problem. For N = 100, the time the algorithm runs is 1 minute. How long does the algorithm take for N=1000? A) same time B) About 3 minutes C) About 10 minutes D) You haven't given enough information. I can't tell.

B

Suppose we have the following definition: vector<int> vec, vec1; //use push_back to put 10 values into vec vector<int>::iterator p = vec.begin(); vector<int>::const_iterator q = vec.begin(); Which of the following expressions is NOT legal? Treat the effect of these as non-cumulative. A) p = vec.end (); B) *q = 1; C) q = vec1.end(); D) *p = 1;

B

The operator * is prefixed to an iterator to A) extract the element in the container to fetch its value only. B) extract the element in the container as either an l-value or an r-value. C) extract the element in the container to assign to it only. D) multiply the element in the container.

B

The parameter in the catch statement A) makes the catch block a function. B) identifies what type of exceptions are caught. C) identifies the different number of exceptions that can be caught. D) must always be an e.

B

Which of the following describes a class that would be a good candidate for conversion to a template class? A) a class which defines rational numbers B) a class which defines a new type of array C) a class which defines customers for a store D) all of the above

B

Which of the following function declaration correctly specifies that two types of exceptions are thrown? A) void f1(exception a, exception b); B) void f1( ) thaw (a,b); C) void f1( ) throw a, throw b; D) void f1( ) exception (a;b);

B

Which of the following is an incorrect declarations of iterators for STL containers? You may assume that the proper header has been included and that a using directive makes the names from namespace std available. A) vector<int>::iterator vecIterator; B) list::iterator<int> listIterator; C) list<int>::iterator listIterator; D) deque<int>::iterator dequeIterator;

B

Which of the following operations do bidirectional iterators have? A) overloaded operator- to move the place the iterator points backware by a number of elements equal to the argument B) prefix operator* to make available the container element for use as l-value or r-value C) overloaded operator+ to add an int value to the iterator to move the place the iterator points forward by the argument number of elements D) overloaded operator* to multiply the iterator by an int value to move the place the iterator points by a number of elements equal to the argument

B

Which of the following operations do random access iterators have? A) overloaded binary operator+ to move the place the iterator points forward by the amount added B) prefix operator* to make available the container element for use as l-value or r-value C) overloaded unary operator++ to move the place the iterator points forward by as many elements as the argument D) overloaded binary operator* to multiply the iterator by a double value to move the place the iterator points by a fractional number of elements equal to the double argument

B

Which of the following would be a good reason for using inherited exception classes? A) A base class exception parameter can be passed any type of exception. B) A derived class exception can be passed to an exception parameter of the base class. C) A base class exception can be passed to an exception parameter of the derived class. D) all of the above

B

You should use exception handling A) only when you use classes. B) only when you cannot handle the exception with simpler control structures. C) in all your programs. D) in every function.

B

Given a class template, how many different times can you instantiate the class? A) 0 B) as many as you need, but only one data type C) as many as you need, of any data types D) 1 for each different data type E) 1

C

Given a search template function that will look for an occurrence of target in an array of items, what is necessary for the instantiating data type to implement? A) the > operator B) the < operator C) the == operator D) the = operator

C

Given the following search function declaration, what would be the corresponding declaration for a templated search function? int search( int array[], int start, int target, int size); //pre: start is > 0, and < size //the position of the first occurrence of target at or after start is returned, or -1 is returned. A) template <class T> int search(T array[], T start, T target, T size); B) template <class T> T search(T array[], T start, T target, T size); C) template <class T> T search(T array[], int start, T target, int size); D) template <class T> int search(int array[], int start, T target, int size); E) all of the above F) none of the above

C

If class A is derived from class B, and a virtual function in class B throws an exception, then the overridden version of that function in class A must A) not throw any exceptions that the function in class B might throw. B) not throw any exceptions. C) have an exception specification that is a subset of the exception specification of the base class B. D) all of the above.

C

If you define some list class template in your program, and then declare a list of integers, 2 lists of doubles and 1 list of strings, how many different version of the template class will the compiler provide? A) 1 B) 2 C) 3 D) 4

C

Suppose we have the following definition: vector<int> vec; // use push_back to put 10 values into vec here. vector<int>::iterator itr1, itr2,itr3; itr1 = vec.begin(); itr2 = vec.begin() + 5; itr3 = vec.end(); For this iterator which of the following is incorrect? A) itr2[3] B) *iter1 C) itr3 + 3 D) itr2 - 5

C

The block of code that checks if an unusual situation or error occurs is called A) a function. B) the catch block. C) the try block. D) an error block.

C

The block of code that handles an exception is called A) the try block. B) an error block. C) the catch block. D) a function.

C

The throw statement is enclosed in A) a catch block. B) quotes. C) a try block. D) a throw block.

C

The time to find an element is the same for a set or a map. It is A) O(N2) B) O(1) C) O(log N) D) O(N) E) O(N1/2)

C

The expression, 4N2-2N+1 is A) Logarithmic ( O(log N) ) B) Linear (degree 1) C) Cubic (degree 3) D) Quadratic (degree 2)

D

The following catch statement catch(...) A) is illegal. B) catches only numeric exceptions. C) should be the first catch block if multiple catch statements are present. D) catches all exceptions.

D

When an unusual situation or error occurs, then the ________ statement is executed. A error B exiting C try D throw

D

Which of the following are valid template prefixes? A) template <class T, class Me> B) template <class Me> C) template <class T> D) all of the above E) none of the above

D

Which of the following does NOT have STL containers types? A) associative containers B) container adapters C) sequence containers D) generic functions

D

Which of the following is NOT a member function of the queue adapter template? For members of queue, specify any needed arguments. A) push() B) size() C) empty() D) top() E) front()

D

Which of the following is NOT, strictly speaking, a component of the Standard Template Library? A) iterators B) generic algorithms C) containers D) templates

D

A class that is used for exceptions is declared A) differently from other classes. B) may not have objects declared of that class. C) specialized only for exceptions. D) all of the above. E) none of the above.

E

A throw statement can throw A) a float exception. B) an integer exception. C) a bool exception. D) an exception of any data type. E) all of the above. F) none of the above.

E

Given the following class template, what changes need to be made to the getSize function? template <class T> class containerClass { public: containerClass(); containerClass(int newMaxSize); containerClass(const containerClass& source); ~containerClass(); T getItem(); int getCount(); int getSize(); void addItem(T item); private: T *container; int maxSize, count; }; int containerClass::getSize() { return maxSize; } A) Add the template prefix. B) Add the <T> before the scope resolution operator. C) Change the return type to a T. D) Nothing needs to change. E) A and B F) A, B, and C

E

Functions may potentially throw at most one exception. T/F

False

Functions that might throw an exception must have a throw list. T/F

False

In a template function definition, all parameters must be of the template class (T). T/F

False

In a template, all members must be private. T/F

False

In a try block, the throw statement is always executed. T/F

False

The catch block is a function. T/F

False

The operator * is prefixed to an iterator to insert an element in the container. T/F

False

The set container implements only iterator. T/F

False

Given the function below, which of the following are needed to change the function into a function template? int smallest( int array[], int size) { int small=0, i; for(i=0;i<size;i++) { if(array[i] < array[small]) small=i; } return small; } A) Precede the function definition with template <class T>. B) Change the type of the array to T. C) Change the type of i to T. D) Change the type of small to T. E) Change all occurrences of int to T. F) all of the above G) A and B H) A and C

G

What changes need to be made to the following class in order to change it to a template class? class containerClass { public: containerClass(); containerClass(int newMaxSize); containerClass(const containerClass& source); ~containerClass(); int getItem(); int getCount(); int getSize(); void addItem(int item); private: int *bag; int maxSize, count; }; A) Add the template prefix. B) Change the parameter type of the implicit constructor to T. C) Change the parameter type of addItem to T. D) Change the return type of getItem to T. E) Change all occurrences of int to T. F) Change the return type of getSize to T. G) A and B H) A, C and D I) A, C, D, and F

H

Classes can be defined as templates. T/F

True

In a class template implementation, every use of the class name as the name of the class should be followed by <T>. T/F

True

STL set operations are essentially insert, delete, and the query, "Is it there?". T/F

True

Templates are an example of algorithm abstraction. T/F

True

The STL containers each define iterators appropriate to the internal structure of the container. T/F

True

The following function does not throw any unhandled exceptions. void f1( ) throw ( ); T/F

True

The model for the iterator in the STL was the pointer. T/F

True


Kaugnay na mga set ng pag-aaral

The Solar System and Universal Gravitation

View Set