CS2 Exam 2

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

In the following search function for a linked list (using the Node and NodePtr as defined in the text), why is there code to check if here is NULL? NodePtr search(NodePtr head, int target) { nodePtr here = head; if(here == NULL) { return NULL; } else { while( here->data != target && here->link != NULL) { here = here->link; } if(here->data == target) { return here; } else { return NULL; } } }

To check if the list is empty

Classes can be defined as templates.

True

If you write a linked list class, then you should implement the destructor, copy constructor, and the assignment operator

True

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

True

T or F: It is possible to have nontype parameters; a nontype parameter can have a default argument and the nontype parameter is treated as const.

True

Templates are an example of algorithm abstraction

True

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

True

If you have a class template declared and you instantiate it in you program twice (once with an integer, once with a string), how many versions of the class does the compiler create?

Twice

Stack, queue and priority-queue are container _____ .

adapters

What should you list in the pre-condition of a template function?

any functionality that the instantiating class must implement (like <) and what must be true before the function executes

When you define a class as a template, then that class can contain ________ data type.

any well-defined

"Given the following stack declaration, which of the following function definitions would correctly implement the destructor? struct StackFrame { char data; StackFrame *link; }; typedef StackFrame* StackFramePtr; class Stack { public: Stack( ); Stack(const Stack& a_stack); ~Stack( ); void push(char the_symbol); char pop( ); bool empty( ) const; private: StackFramePtr top; }; " "

char* next; while(!empty()) next = pop();

A ______________________________ constructor is a single argument constructor that turns objects of other types (including built-in types) into objects of a particular class.

conversion

head.item is the same as (head).item.

false

As with static data members of non-template classes, static data members of template-classes must also be initialized at ______ namespace scope.

global

"Given the following declarations, which statement would allocate memory for the first item in the list? struct Node { int item; Node* link; }; typedef Node* NodePtr; NodePtr head; "

head = new Node;

Given the following declarations, how would you know if head is pointing to an empty list? struct NodeType { int number; NodeType* next; }; NodeType* head; //some other code here

if(head==null)

Given a linked list (using the code from the book) and assuming there are at least two nodes in the list, which of the following sets of statements would implement a function to return and remove the last item in the list?

int temp; NodePtr here, there; here = head; while(here->link != NULL) there = here; here = here->link; there->link = NULL; temp->here = data; delete [] data; return temp;

If you need to pass a class template (named myClass) function an object of the class as a value parameter, then the type of the parameter is

myClass<T>

Class templates are also called ______ types.

parameterized

A stack is a specialized type of list.

true

Given a pointer to an object p declared: pExampleClass* p = new ExampleClass(); There are two ways of dereferencing the pointer and calling a method named fun. One way is: p->fun(); The second is:

**Answer** (*p).fun();

Provide first compiler directives used to prevent the redefinition of a class when the header file is included more than once.

**Answer** //At the top of the Interface #ifndef FILENAME_HPP #define FILENAME_HPP //at the bottom #endif

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

**Answer** A) a class which defines a new type of array **Reason** B: Rational numbers would only be be one type: cstring "a/b" C: Customers would only be one type: cstring

A using directive that is at the start of the file A) applies to the entire file. B) hides all other namespace directives. C) is a syntax error. D) applies only to the first block.

**Answer** A) applies to the entire file. **Reason** using std::cin; put in file once never has to be done again.

Which of the following are valid declarations for an assignment operator for a class named myClass? Correct Answer A) void operator = (const myClass& source); B) void friend operator = (const myClass& source); C) void friend operator = (myClass& left, const myClass& source); D) void operator = (myClass& left, const myClass& source);

**Answer** A) void operator = (const myClass& source); **Reason** B and C should have "friend to the left of void D is not correct for left side/rightside dynamic of = operator.

When would you want to make a function a function template? a. when the implementation details of the function are independent of the data type(s) of the parameters. b. all functions should be function templates c. when two different functions have different implementation details d. only when two functions have the same type of parameters

**Answer** A. When the implementation details of the function are independent of the data types of the parameters. **Reason** You should make a function a function template when you can change the datatype to anything and the algorithm in the function definition will execute the same way.

All the code between #ifndef MYCLASS_H and #endf is ____________ if MYCLASS_H is defined. a. skipped b. executed c. compiled d. debugged

**Answer** A. skipped

Using template functions is an example of ___________ abstraction.

**Answer** Algorithm

STL ________ are functions that perform such common data manipulation operations such as searching, sorting, and comparing elements (or entire containers)

**Answer** Algorithms

In order to make a user-defined ADT available that is defined in the file myfile.h, you would A) #include myfile.h B) #include "myfile.h" C) #include <myfile.h> D) #include <myfile>

**Answer** B) #include "myfile.h" **Reason** A: has no quotation marks C. uses <> D: has no ".h" and uses <>

Connecting the application and implementation files together to form an executable file is called A) compiling. B) linking. C) assembling. D) debugging.

**Answer** B) Linking

The arrow operator (->) specifies A) is known as NULL. B) a member of a struct or class that is pointed to by a pointer variable. C) the pointer member only of a struct or a class that is pointed to by a pointer variable. D) less than or equal.

**Answer** B) a member of a struct or class that is pointed to by a pointer variable. **Reason** Obviously not A or D. It is not C because "pointer member" would mean that you could use -> to access members of classes/structs that are of pointer*; which is not true.

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 multiply the iterator by an int value to move the place the iterator points by a number of elements equal to the argument D) overloaded operator+ to add an int value to the iterator to move the place the iterator points forward by the argument number of elements

**Answer** B) prefix operator* to make available the container element for use as l-value or r-value (Dereferencer) **Reason** A: Pointer arithmetic is not supported by bidirectional iterators. You can't "Access values Randomly". C: No iterator supports mulitiplication D: Same reason as A) **Other operators not supported by bidirectional but supported by random access iterators** indexing [], <, >, <=, >=, +=, -=

8. 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 = 1; B) *q = 1; C) p = vec.end (); D) q = vec1.end();

**Answer** B. q*=1 **Reason** q is a const_iterator so it cannot be used to modify the value it points to.

When a dynamic array with a class for a base type is declared, which constructor is called? A) the copy constructor B) the default constructor C) an explicit constructor D) the destructor

**Answer** B. the default constructor

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) *iter1 B) itr2[3] C) itr3 + 3 D) itr2 - 5

**Answer** C) itr3 + 3 **Reason** itr3 equals a ptr to the end of the vector, so adding 3 would put you out of range.

To add an item to a stack, we call the ________ function. A) top B) pop C) push D) empty

**Answer** C) push

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(int array[], int start, T target, int 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(T array[], T start, T target, T size); E) All of these F) None of these

**Answer** C) template <class T> T search(T array[], int start, T target, int size); **Reason** All the variables should be type T. Except start and size which will always be int

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

**Answer** C. 3 **Reason** 1 for int 1 for double (no repeat for the 2) 1 for string

The identifier used in the #ifndef directive should be a. the name of the class in upper case letters b. Your name in upper case letters c. The file name in uppercase letters (with an _ instead of a .) d. whatever you want it to be

**Answer** C. The file name in uppercase letters (with an _ instead of 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 myContainer; b. containerClass<T> myContainer; c. containerClass<int> myContainer; d. containerClass myContainer<int>

**Answer** C. containerClass<int> myContainer;

The file that contains the main portion of your program is called a. the implementation file b. the interface file c. the application file d. the specification file

**Answer** C. the application file

If you need to insert an element in the front of a list with N nodes (and move the other elements back one place), how many nodes do you have to move? A) 1 B) N C) N-1 D) 0

**Answer** D) 0 **Reason** To insert an element (newNode) in the front of a list. You only need to change the head to the equal newNode and the pointer to the next node (next) to the original head. newNode->next = head; head = newNode;

Give the following class template, what changes need to be made to the default constructor definition? A) Add the template prefix. B) Change the type of the dynamic array allocation to T. C) Add the <T> following the class name before the scope resolution operator. D) All of these E) None of these

**Answer** D) All of these **Reason** A: The template prefix is the "template <class T>" right above the function definition. It is required when a function is not inlined. B: The dynamic array could be any type C: The function header for a class D: For template class constructor the header format is "myClass<T>::myClass()"

In the following function template, what must be TRUE in order to use the function with a given data type? A) The data type must be a pre-defined data type. B) The data type must be numeric. C) The data type must be character based. D) The data type must have a < operator defined for it.

**Answer** D) The data type must have a < operator defined for it. **Reason** A, B, and C are not constraints on template functions. D: The < operator must be overloaded to work with an array

A namespace is Incorrect Response A) All of these. B) using std. C) used to distinguish between identical names. D) a collection of name definitions and used to distinguish between identical names E) a collection of name definitions.

**Answer** D) a collection of name definitions and used to distinguish between identical names **Reason** Using std is just means using standard library and does not make it a namespace.

If you have a class defined in separate files, and change the main program, which files need to be re-compiled? A) all files B) the application and the implementation C) the interface D) the application E) the implementation

**Answer** D) the application **Reason** Implementation File: only needs to be compiled once. Even if their is a change in the application file or a different application file used. Interface: No need to compile it. It is #include in implementation and application files.

If you have a class defined in separate files, and change the way a member function is defined (the body of the function), which files need to be re-compiled? A) the application and the implementation B) the application C) the interface D) the implementation E) all files

**Answer** D) the implementation **Reason** The phrasing "change the way a member function is defined" is equivalent to saying "changing the implementation file". Since that file is changed it must be re-compiled.

In order to hide functions that are defined in the implementation file, they should be part of the ________ namespace. A) global B) class C) std D) unnamed

**Answer** D) unnamed

Which file name will end in a .h? a. Implementation b. Application c. All input files d. Interface File e. A and B

**Answer** D. Interface File

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

**Answer** D. extract the element in the container as either l-val or an r-val

iven 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

**Answer** D. the == operator **Reason** Comparison between two array elements will require an overloaded == operator

The process used to resolve which virtual function to call at run-time is called ________________ binding.

**Answer** Dynamic

We use the #ifndef, #define, and #endif a. to prevent multiple definitions of a class b. when we use separate files c. whenever we use a class d. none of the above e. A and B

**Answer** E. A and B

Which file name will end in a .cpp? a. Implementation File b. Application File c. All input files d. Interface File e. A and B

**Answer** E. Application and Implementation files

We generally set a pointer variable to NULL a. to signify that the pointer does not point to any memory b. to signify the end of a list c. because we want all our pointers to always point to NULL d. never e. A and B f. A and C

**Answer** E. both A and B

If you define a function template, then the compiler will create a separate function definition for every data type that exists.

**Answer** False

T or F: Friendship can be extended to a template

**Answer** False **Reason** A template is not a class or struct so friendship cannot be extended to it

T or F: Keywords class and typename as used with a template type parameter specifically mean "any user-defined class type."

**Answer** False **Reason** It can also be predefined class types ie int, double, char... ect.

If you have a node pointer named head, which is pointing to a valid node, how do you access the member variable in the node named item? a. head.item b. *head.item c. (*head).item d. head->item e. all of the above f. A or B g. C or D

**Answer** G) C or D (*head.item) and head->item

If there is a NodePtr named toDelete whose value points to a valid node in the list, which of the following statements would remove the node that follows toDelete from the list and return that memory to the freestore? a. toDelete -> link = toDelete -> link ->link; delete toDelete-> link; b. tmp = toDelete -> link;toDelete -> link = tmp -> link;delete tmp; c. tmp = toDelete -> link; toDelete -> link = toDelete->link->link; delete tmp; d. All of the above e. none of the above f. A and B g. B and C

**Answer** G. B and C **Reason** A: It is not using a temporary variable during the swap so it doesnt work toDelete -> link = toDelete -> link ->link; delete toDelete-> link; B: //Puts the link of toDelete in tmp tmp = toDelete -> link; //Puts the link to node after toDelte into link toDelete -> link = tmp -> link; delete tmp; C: tmp = toDelete -> link; toDelete -> link = toDelete->link->link; delete tmp;

_______ are objects used to access elements within the STL containers. These objects are often used as if they are pointers to walk through the elements of a container but they hold extra state information sensitive to the particular container.

**Answer** Iterators

A stack can be implemented using a ________.

**Answer** Linked List or Array. **Reason** Array is for a static stack and a linked list is for a dynamic stack.

The STL class template ______ is implemented as a linked list and provides an efficient implementation for insertions and deletions anywhere.

**Answer** List

If several classes are generated from a single class template with a single static data member, do all of the specializations share a single copy of the static date member?

**Answer** No

Using quotes rather than angle brackets to include a header files tells the compiler to look in the _______________ for the file.

**Answer** Same path as the current source file

The C++ code template <class T> is called the ______________________

**Answer** Template Prefix

"Why will the following code NOT compile? namespace ns1 { void print(); void display1(){}; } namespace ns2 { void print(); void display2(){}; } int main() { using namespace ns1; using namespace ns2; display1(); display2(); print(); return 0; }

**Answer** The print(); call is ambiguous **Reason** ns1 and ns2 both a function print(); And both ns1 and ns2 are declared with the block scope of main. The compiler doesn't know which print function is being called

If your program defines a class template, then the compiler will generate a class for each different data type for which it is instantiated.

**Answer** True

T or F: The name of a type parameter can be used only once in the formal type parameter list of the template definition.

**Answer** True **Reason** template <class T1, class T1> will not compile. its must be: template <class T!, class T2>

Can a local variable in a function be defined with the same name as a global variable? If so, how does the programmer syntactically differentiate between the two?

**Answer** Yes, by accessing the global variable using ::

The keyword public is a(n) _________________.

**Answer** access specifier

Apart from constructors, the operations for a queue listed in the text are ________ and ________.

**Answer** add, remove; empty

You can define a stack that will hold _____

**Answer** any valid data type

What best describes the variable p in this context? int* const p = new int;

**Answer** constant pointer to non-constant data

Given a linked list (using the code from the book) and assuming there are at least two nodes in the list, which of the following sets of statements would implement a function to return and remove the last item in the list? "

**Answer** int tmp; NodePtr here, there; here=head; while(here->link != NULL) { there = here; here = here ->link; } there->link = NULL; tmp=here->data; delete here; return tmp;

One of the fundamental principles of good software engineering is to separate ____________ from implementation. This makes it easier to modify programs.

**Answer** interface

Given a class template named listClass, declare a listClass object named myList that can hold strings.

**Answer** listClass<double> myList; ***Don't forget the <type(i.e int, double, char..)>

In order to create a namespace called student, you use

**Answer** namespace student { //code goes here }

STL ________ containers include vector, list, and deque

**Answer** sequence

Each object of a class has its own copy of all the data members of the class. In certain cases all objects of a class should share only one copy of a variable. A____________________ class variable is used for these and other reasons.

**Answer** static

_________________ member functions are the only member functions that do not receive this as an implicit first argument.

**Answer** static

Who or what generates classes from template classes?

**Answer** the compiler

Writing a template class a. allows us to skip the implementation of that template class b. allows us to write one class definition that can hold different data types c. means we never have to write non-template classes again d. is illegal

B. Allows us to write one class definition that can hold different data types.

Which of the following member functions is NOT common to the sequential containers (vector, list, deque)? A) begin() B) rbegin() C) rend() D) push_front() E) front()

D. push_front()

Given a class template, how many different times can you instantiate the class? a. 0 b. 1 c. 1 for each different data type d. as many as you need, but only one data type e. as many as you need, of any data types

E. AS many as you need, of any data types.

A class template may not use dynamic memory allocation.

False

In a class template, all members must be private

False

You may not have overloaded friend operators in a class template

False

You can directly access the nth node of a linked list

False, you must traverse the list until you reach the nth node.

"What is wrong with the following definition of headInsert? struct Node { int item; Node* link; }; typedef Node* NodePtr; void headInsert(NodePtr& head, int data) { NodePtr tmp = new Node; tmp->item = data; head->next = tmp; tmp->next = head->next; } NodePtr head; headInsert(head, 4); "

If there were any nodes following head they are now lost.

In a node type named MyNode, which of the following correctly declares a pointer to a node of that type?

MyNode *ptr;

In a linked list as defined in the text, the pointer in the last node in the list should always point to ________.

NULL ** all caps

All classes should be converted to templates

No

Given a linked list (using the code from the book), which of the following sets of statements would implement a function to return the last item in the list? "

NodePtr here; here=head; while(here->link != NULL) { here = here ->link; } return here->data;

Given the following function declaration void insert( NodePtr afterMe, int num); //PRE: afterMe points to some node in the nonempty list //POST: A new node containing num is inserted after afterMe. void insert(NodePtr afterMe, int num) { // which of the following function definitions correctly implement this //function? }" "

NodePtr tmp=new Node; tmp-> data = num; tmp-> link = afterMe -> link; afterMe -> link = tmp;

If you have a class template declared and you instantiate it in you program twice (both times it is instantiated with an integer), how many versions of the class does the compiler create?

Only One time

"What happens if you have two lists (list1, list2), no assignment operator in your class, and attempt to execute the following statement? list1 = list2; "

Since there is no assignment operator list1 will become a bitwise copy of list2

The arrow operator (->) combines the actions of which two operators?

The dereferencing operator "*" and the dot operator "."


Ensembles d'études connexes

4 - Enhanced Entity..Relationship and UML Modeling

View Set