Computer Science 3510
Summer 2000
Practice questions for final exam
This is a closed book test. You may use two sheets of prepared
notes. You have 90 minutes. Answer all of the questions.
The final will have multiple-choice questions, small programming
questions and a few other kinds of questions, such as problems
on binary search trees. The short-essay questions on this practice
exam will prepare you for the final.
- What is a dangling pointer?
- What are the consequences of using a dangling pointer in a program?
- Why is it necessary for a C++ programmer to understand his or
her program at a physical level of detail?
- How do you deallocate a variable or array that is allocated
by each of the following statements?
- int x;
- int* x = new int;
- int x[10];
- int* x = new int[10];
- What is a memory leak?
- Explain the difference between the interface and the
implementation of a function. What belongs to the interface?
What belongs to the implementation?
- Same as the preceding question, but for an abstract data
type rather than a function.
- What is the usual consequence to a programmer
of violating an abstraction?
How does it affect the ease of programming?
- How do you compare two strings to see if they are equal?
- How can you copy null-terminated string A into array B?
- Where is the smallest value in a binary search tree located?
The largest value? A value that is close to the middle? How can
you find the value that immediately follows the value at the root?
- What does an object contain?
- Which objects can make use of a private variable or function in
an object?
- In what parts of a program can a protected variable or
function of a class be used?
- Explain the purpose of a wrapper class.
- Suppose that s is a stack object of class IntStack.
Write a statement to pop s and put the popped value in variable x.
- What do C++ templates provide? Where would they
most likely be used?
- What is the purpose of a destructor in a class?
- What does a copy contructor do? When it is called?
What is a prototype for the copy constructor for class Widget?
- To within a constant factor, how long do each of the
following take?
- Look up a value in a balanced binary search tree that has
n values in it.
- Insert a value into a binary search tree that has n values
in it.
- Compute the length of a linked list where the length is n.
- Add a value to the front of a linked list with n things in it.
- Sort a linked list using Lsort, where the list has n members.
- Push a value onto a stack of size n.
- Write a function called reverse that takes a null-terminated
string s and returns null-terminated string that consists of s
written backwards. For example, if s is ``foo'' then reverse(s)
returns string ``oof''. The result string should be put into
newly allocated memory. String s should not be modified.
char* reverse(char *s)
- Suppose that you insert 20 into the following tree using
the algorithm that does rotations to keep the tree height-balanced.
What is the resulting tree? Be sure to do the rotations correctly.
49
/ \
/ \
26 75
/ \ \
/ \ \
16 42 85
/ \
/ \
12 19
- Types TreeNode and Tree are shown below. They are used
in a classical implementation of binary search trees. Write
a function called largestEven that takes a binary search tree T
and returns the largest even number that occurs in T.
If T does not have any even numbers in it, then largestEven(T)
should return -1.
Presume that T contains no negative
numbers. (This requirement is part of the contract.)
struct TreeNode {
int data;
TreeNode* left;
TreeNode* right;
TreeNode(int x, TreeNode* L, TreeNode *R)
{
data = x;
left = L;
right = R;
}
};
typedef TreeNode* Tree;
int largestEven(Tree T)
{
- Write a function called flip that computes a mirror image
of a binary tree, using the types of the preceding problem.
The mirror image is obtained by flipping the tree over, or
by holding it up to a mirror. The mirror image of a binary
search tree has values in descending order from left to right, rather
than in ascending order.
- You are using an object-oriented implementation of a queue of
integers.
(The class is called Queue.)
The operations that you can do on a queue are to remove(), insert(x),
and test isEmpty(). You are writing outside the queue
implementation. Write a function that performs a peek in the queue.
That is, it returns what is at the front of the queue, without modifying
the queue. It is allowed to modify the queue temporarily, as long
as the queue is put back the way it was. Presume that the queue
has something in it.
int peek(Queue q)