C++ test 1, Exam 1 all T/F, Exam 1 Ch 2, CSCI 133 Data Structures Quiz 1, CSCI 133 - Exam 1, Candilocks

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

what syntax for preprocessor directives?

<filename> "filename"

what is the difference between preprocessor directives with "" and those with <>?

<filename> uses a predesignated library of preprocessors or headers. (standard library) "" typically searches the file that the program is executed from.

What is the difference between a copy constructor and memberwise assignment?

Copy construction takes place when a new object is being instantiated. Assignment takes place after an object already exists.

1. According to the principle of information hiding, a module should be completely isolated from other modules.

False.

1. If 5 items are added to a stack, the first item to be removed from the stack is the first item that was added to the stack.

False.

2. A stack has a first in, first out property.

False.

3. A binary search starts at the beginning of the collection of items.

False.

3. An abstract data type is another name for a data structure.

False.

3. The peek operation of the ADT stack changes the stack.

False.

4. An iterative method always calls itself.

False.

5. If the characters in a string are added to a stack one at a time, characters removed from the stack will occur in the order in which they appear in the original string.

False.

5. In practice, recursion should be used even when a problem has a simple iterative solution.

False.

7. By default, all members in a class are public.

False.

9. The binary search algorithm can be applied to an unsorted array.

False.

1. A program can use the operations of the ADT stack without knowing how the operations are implemented.

True.

1. A recursive solution solves a problem by solving a smaller instance of the same problem.

True.

10. Any instance of a subclass can be used in a program anywhere that an instance of the superclass can be used.

True.

10. The base case for a recursive solution to finding the kth smallest item in an array cannot be predicted in advance.

True.

2. An iterative solution involves loops.

True.

2. Data structures are part of an ADT's implementation.

True.

4. A program can use the operations of the ADT stack without knowing how the operations are implemented.

True.

6. An application can use the operations of an ADT without knowing how the ADT is implemented.

True.

6. When constructing a recursive solution, you should assume that a recursive call's postcondition is true if its precondition is true.

True.

6. When infix expressions are converted to postfix expressions, the operands always stay in the same order with respect to one another.

True.

7. Every recursive method must have a base case.

True.

8. A recursive solution can have more than one base case.

True.

What will prevent multiple inclusions?

#idndef - #define - #endif #pragma once #import (Xcode specific)

The *

(pointer to / dereference) operator.

The &

(reference / address of) operator

The []

*(array)

Main function is in what type of file?

.cpp

class implementation is in what type of file?

.cpp

What type of file will a class be put into?

.h

what does a class implementation file include?

A .cpp file should only need to include its own .h file.

1. Specifications indicate how to implement ADT operations, but not what the operations do

False

10. Classes that use dynamically allocated memory can depend on the compiler-generated destructor.

False

12. A recursive version of toVector is complicated and requires invocation of the copy constructor.

False

14. A link-based implementation requires less memory than an array-based implementation.

False

2. Before you can assign headPtr a value, you must first create a new Node object.

False

2. In an array based implementation of a stack, the stack can grow and shrink dynamically.

False

2. The language Algebraic Expressions is the set of strings that meets certain rules of syntax and that definition also gives the rules of the syntax.

False

4. Data structures for an ADT can be determined at any time during the process of specifying its operations.

False

4. Like an array-based implementation, a link-based implementation's insertion and removal operations will need to move data items.

False

4. The stricter a definition, the harder it is to recognize a syntactically legal expression.

False

5. A class need not validate data given by client code. According to the author, the client code should do that.

False

6. Choices made during the implementation process have no effect on execution time of your code.

False

6. Even though you have allocated memory by using new, there is no need to deallocate it with the delete command.

False

6. Subproblems that a recursive search solution generates need not reach a base case.

False

7. According to the text, when adding a first entry to an array, always place it in the first element, the element whose index is zero.

False

8. A class can have multiple destructors.

False

8. A highly cohesive module is less robust.

False

8. Little relationship exists between mathematical induction and recursion.

False

8. The client of a class can access the class's private members directly by use of a password.

False

class thingy { public: string name; void setname(char *n) {name=n;} string getname() {return name;} thingy(char *n) {setname(n);} }; What is wrong here?

Needs a default constructor.

What is a shallow copy?

Shallow copy is a bit-wise copy of an object. A new object is created that has an exact copy of the values in the original object. If any of the fields of the object are references to other objects, just the reference addresses are copied i.e., only the memory address is copied.

After the copy statement (t1=t2;), the t1 object's name property now has a value. But we have provided no code (or guidance) to determine what should happen.

The compiler provides member-wise assignment by default

class dynamicthingy { public: string *name; void setname(char *n) { name = new string(n); } string getname() {return *name;} }; What is wrong?

The short answer: memory leak! Every time setname is called, a new string object is dynamically allocated and its address is stored in the name pointer. Whatever object the name pointer may have previously pointed to is lost. Imagine multiple consecutive calls to setname:

According to our book, "C++ requires a constructor call for each object that's created..." (p. 77). So consider the following code: class thingy { public: string name; void setname(char *n) {name=n;} string getname() {return name;} }; This class definition contains no obvious constructor, yet this code: int main() { thingy t1; does not generate any errors. Question 1: Why?

The short answer: you get an default constructor for free.

1. A node can be dynamically allocated.

True

1. All programs are strings, but not all strings are programs.

True

11. The copy constructor for our bag class requires traversing the original linked chain and duplicating each node visited.

True

13. Access time is a constant for an array-based implementation.

True

15. The following method is recursive. template<class ItemType> void LinkedBag<ItemType>::fillVector(std::vector<ItemType>& bagContents, Node<ItemType>* curPtr) const { if (curPtr != nullptr) { bagContents.push_back(curPtr- >getItem()); fillVector(bagContents, curPtr- >getNext()); } // end if } // end fillVector

True

2. Generally it is unwise to define an entire class then attempt to test it

True

3. A given word, s, is a palindrome if and only if, the first and last characters of s are the same and s minus its first and last characters is a palindrome.

True

3. Generally speaking, a link-based implementation does not impose a fixed maximum size on the data structure.

True

3. In the link based implementation of the stack, both the method push and the copy constructor allocate new nodes.

True

3. The method getIndexOf should generally be declared as private.

True

4. An interface for a class that allows programmers to accomplish any reasonable task is called a complete interface.

True

4. Private data members are hidden from the client.

True

5. C++ arrays are data structures

True

5. For a link-based bag, the most convenient place to make an insertion is at the beginning of the chain.

True

5. It is possible to use one ADT to implement another ADT.

True

5. The following expression is a prefix expression + a b - c d

True

6. In both implementations of the class stack, the pop methods returned a value of type bool.

True

7. According to the author, the LIFO property of stacks seems inherently unfair.

True

7. The method clear cannot simply set ItemCount to zero.

True

7. The method toVector must "know" where the add has placed the entries.

True

7. You can use induction to prove that a recursive algorithm either is correct or performs a certain amount of work.

True

9. According to the text, you must write a destructor if your class allocates memory dynamically.

True

9. Highly coupled modules should be avoided.

True

9. The entries in a bag have no particular order.

True

11. In a recursive method that writes a string of characters in reverse order, the base case is ______. a) a string with a length of 0 b) a string whose length is a negative number c) a string with a length of 3 d) a string that is a palindrome

a.

15. When you solve a problem by solving two or more smaller problems, each of the smaller problems must be ______ the base case than the original problem. a) closer to b) farther to c) either closer to or the same "distance" from d) either farther to or the same "distance" from

a.

2. A binary search uses a ______ strategy. a) divide-and-conquer b) sequential c) determine-the-pivot d) smallest-to-largest

a.

9. In the box trace for a recursive function, a new box is created each time ______. a) the function is called b) the function returns a value c) an object is created d) an object is initialized

a.

16. A recursive method that computes the number of groups of k out of n things has the precondition that ______. a) n is a positive number and k is a nonnegative number b) n is a nonnegative number and k is a positive number c) n and k are nonnegative numbers d) n and k are positive numbers

c.

17. The midpoint of a sorted array has the index ______, where first is the index of the first item in the array, and last is the index of the last item in the array. a) first / 2 + last / 2 b) first / 2 - last / 2 c) (first + last) / 2 d) (first - last) / 2

c.

what does a class definition file include?

all the headers needed by the .cpp file.

12. Which of the following is a precondition for a method that accepts a number n and computes the nth Fibonacci number? a) n is a negative integer b) n is a positive integer c) n is greater than 1 d) n is an even integer

b.

5. The base case for a recursive definition of the factorial of n is ______. a) factorial (-1) b) factorial (0) c) factorial (n) d) factorial (n - 1)

b.

7. In the box trace, each box roughly corresponds to a(n) ______. a) recursive relation b) activation record c) base case d) pivot item

b.

1. In a recursive solution, the ______ terminates the recursive processing. a) local environment b) pivot item c) base case d) recurrence relation

c.

13. How many bases cases does a recursive binary search of a sorted array have? a) 0 b) 1 c) 2 d) 3

c.

14. The number of ways to choose k out of n things is ______. a) the number of ways to choose k - 1 out of n - 1 things b) the number of ways to choose k out of n - 1 things c) the sum of the number of ways to choose k - 1 out of n - 1 things and the number of ways to choose k out of n - 1 things d) the product of the number of ways to choose k - 1 out of n - 1 things and the number of ways to choose k out of n - 1 things

c.

6. What is fundamentally wrong with computing the Fibonacci sequence recursively? a) it has two base cases b) each call to the function results in two recursive calls c) it computes the same values over and over d) nothing

c.

8. In the box trace, each box contains all of the following EXCEPT ______. a) the values the function's arguments b) the function's local variables c) the function's execution time d) a placeholder for the value returned by each recursive call from the current box e) the value returned by the function itself

c.

10. What happens if a recursive function never reaches a base case? a) the function returns the correct value b) the function returns an incorrect value c) the function terminates immediately d) an infinite sequence of recursive calls occurs

d.

3. A ______ is a mathematical formula that generates the terms in a sequence from previous terms. a) local environment b) pivot item c) base case d) recurrence relation

d.

4. The factorial of n is equal to ______. a) n - 1 b) n - factorial (n-1) c) factorial (n-1) d) n * factorial (n-1)

d.


Ensembles d'études connexes

Logical Appeal, Argument, and Essay Structure Test

View Set

Teddy Roosevelt, Trust Busting, The Square Deal, and the PFDA

View Set

practice questions for exam 2 psy 360

View Set

MGT 371 ch9: Building effective teams and teamwork

View Set