CMSC 208 TEST 2 Name______________
April 23, 1996 Total Points: 100
A. SHORT ANSWER (35 points)
TRUE or FALSE
1. Each recursive function call produces a new activation frame.
MULTIPLE CHOICE
2. Which of the following is not considered part of a function’s
activation frame?
a. the contents of automatic local variables
b. the contents of global variables
c. the contents of formal parameters
d. a return address
e. c and d above
3. Consider the following function
void RecEm( int i )
{
if ( i < 8 ) {
i++;
RecEm(i);
cout << i << ‘ ‘;
}
}
What output is produced when RecEm is called with a parameter value of 4?
a. 4 5 6 7 8
b. 5 6 7 8
c. 8 7 6 5
d. 9 8 7 6 5
e. none of the above
4. Consider the following function
void RecEm( int i )
{
if ( i < 8 ) {
RecEm(i);
}
}
How many times is RecEm invoked by the function call RecEm(3) ?
a. 2
b. 5
c. 6
d. 7
e. This is an example of infinite recursion.
FILL IN THE BLANKS.
5. The nonrecursive part of a recursive definition is called the
.
TRUE or FALSE
6. With C++ class instances, the built-in assignment operator(=)
performs a shallow copy.
7. The new operator automatically initializes the newly allocated
data to zero.
MULTIPLE CHOICE
8. How many of these C++ operators are valid for pointer variables?
++ (unary)
[] (unary)
== (binary)
- (binary)
= (binary)
a. 1 b. 2 c. 3 d. 4 e. 5
9. Given the declarations
float* ptr1;
float* ptr2;
which of the following is a valid assignment?
a. ptr1 = ptr2;
b. *ptr1 = ptr2;
c. *ptr1 = *ptr2;
d. a and c above
e. a, b, and c above
10. Given the declaration
int* myPtr;
which of the following is a valid statement?
a. myPtr = new int*;
b. delete myPtr;
c. delete *myPtr;
d. a and c above
e. a, b, and c above
11. Given the declaration
char* charPtr;
what is the data type of the expression *charPtr?
a. char
b. char*
c. *char
d. *charPtr
e. charPtr*
12. Given the declarations
int i = 3;
int* p = &i;
which of the following statements will increment i?
a. p++;
b. (*i)++;
c. (*p)++;
d. a and c above
e. none of the above
13. Which of the following C++ class member functions is not
specifically intended to provide deep copying of class objects?
a. default constructor
b. destructor
c. copy-constructor
d. operator= function
e. a and b above
14. Which of the following situations does not implicitly invoke a C++
copy-constructor?
a. passing a class instance by value as a function parameter
b. copying one class instance to another in an assignment
statement
c. initializing one class instance to another in its declaration
d. returning a class instance as a function value
e. All of the above will invoke a copy-constructor.
15. Which of the following C++ class member functions should be
provided if class instances have members that point to dynamic
data?
a. destructor
b. copy-constructor
c. operator= function
d. a and b above
e. a, b, and c above
16. For a C++ class named MyClass, the member whose prototype is
MyClass( const Myclass& );
is which of the following?
a. a constructor
b. a destructor
c. a copy-constructor
d. an overloaded assignment operator
e. none of the above
FILL IN THE BLANKS.
17. Write the following function prototype in an equivalent way without
using square brackets []
void SomeFunc( float arr[] );
__________________________________________
18. A(n) ______________ is a C++ class member function that is implicitly
invoked when a class instance is destroyed.
19. In the expression *somePtr, the asterisk is formally known as
the ______________ operator.
20. The ______________ is the region of memory set aside for
dynamically allocated data.
21. In C++, the unary ampersand(&) operator is known as the
______________ operator.
22. A(n) ______________ is a dynamic data object that no longer is
pointed to by any other data in a program.
23. A(n) ______________ occurs when a pointer variable is left
pointing to a data object that has been deallocated.
TRUE or FALSE
24. Given only a head pointer to a singly linked list, it is more
efficient to insert a node at the front of the list than the back.
25. With a dynamically allocated singly linked list, the only
nondynamic data required is the head pointer.
26. A C++ class constructor cannot return a function value.
MULTIPLE CHOICE
27. If ptr is a variable that points to an object of type
struct NodeType {
float itemPrice;
NodeType* link;
};
then which of the following expressions correctly refers to the
itemPrice member of the object pointed to by ptr?
a. (*ptr).itemPrice
b. *ptr.itemPrice
c. ptr->itemPrice
d. a and c above
e. a, b, and c above
QUESTIONS #28-29 BELOW both use the following declarations:
struct NodeType {
int content;
NodeType* next;
};
typedef NodeType* NodePtr;
NodePtr listHead;
NodePtr somePtr;
28. Give a single C++ statement that will create a new dynamic node
on the free store and make listHead point to this new node.
29. Give a single C++ statement that will dispose of the dynamic node
that is currently pointed to by somePtr and will make that memory
available for future allocation.
TRUE or FALSE
30. The assertions included in a specification file are called abstract
assertions.
MULTIPLE CHOICE
31. Suppose that you are involved in the design review of an abstract
data type specification file. Which of the following would you
not consider to be essential to a good design?
a. The ADT is complete.
b. The ADT uses efficient algorithms.
c. The ADT includes testable preconditions.
d. The ADT operations are consistent with the abstraction.
e. a and d above.
32. Choose the best classification of ADT operation for the Push
operation that inserts one item onto a stack.
a. initial ADT-constructor
b. ADT-constructor (but not initial ADT-constructor)
c. selector
d. test
e. I/O
33. Assume that the initial value of stack s is <100, 200> .
(The top of the stack is the front of the sequence.)
What is the value of s after the following operations?
s.Pop(); s.Push(300); s.Push(400); s.Pop();
a. <400, 300, 100, 200>
b. <100, 200, 300, 400>
c. <400, 200>
d. <300, 200>
e. none of the above
FILL IN THE BLANKS.
34. The collection of all possible values of an abstract data type
is called the ______________ of the type.
35. An important part of the programming process is to translate
abstract assertions into their corresponding ______________
assertions.
B. (15 points)
Fill in the table below with equivalent forms.
Use parentheses in your infix answers (where needed) to indicate
the order of operations.
____________________________________________________________________________
| | | |
| prefix | infix | postfix |
|__________________|________________________________|________________________|
| | | |
| * + A B - C D | | |
|__________________|________________________________|________________________|
| | | |
| | | A B + C D + E - * F / |
|__________________|________________________________|________________________|
| | | |
| | ((A + B)*C-(D + E)) / (F - G) | |
|__________________|________________________________|________________________|
C. (15 points)
Consider the following recursive C++ function:
void Quiz(int n)
{
if (n > 0)
{
Quiz(n-1);
cout << n;
Quiz(n-1);
}
}
What will be the output produced by each of these calls?
Quiz(1) ______________
Quiz(2) ______________
Quiz(3) ______________
D. Writing Functions for Linked Lists (35 points)
_____ __ _____ __ ____ __ ____ ____
| -19 | |-> | 25 | |-> | 13 | |-> | 20 |NULL|
|_____|__| |_____|__| |____|__| |____|____|
Assume the declarations below have been made for a dynamically
allocated linked list like the one shown above with one external
pointer List.
struct NodeType {
int data;
NodeType* next;
};
typedef NodeType* NodePtr;
NodePtr List;
NodePtr ptr;
1. Write C++ statements to traverse the entire linked list and print
out its contents.
2. Write an integer valued function called Sum that returns the sum
of the numbers in the data members of the nodes of the linked list
pointed to by List. If the linked list is empty, Sum returns 0.
Use the prototype below
int Sum(/* in */ NodePtr);
3. Write a function called Address that returns a pointer to a node
containing the integer Value if Value is present in the linked
list with external pointer List, or returns NULL if Value is not
present in that linked list.
Use this prototype
NodePtr Address( /*in*/ int Value, /*in*/ NodePtr List);
a) non-recursive version
b) recursive version
Last updated: Nov. 4, 1999