This is a closed book test.
You may use one 8.5x11 sheet of prepared notes, written on both sides.
You have 90 minutes. Answer all of the questions.
- Write a clearly legible T to the left of each of the
following statements that is true, and a clearly legible F to
the left of each that is false.
- A null-terminated string of length 30 occupies 30 bytes.
- If Node is a type, then expression new Node[15]
returns a pointer of type Node*.
- The heap is an area of memory that can change size while
a program runs.
- Most dynamic storage allocation is done in the
static area of memory.
- Storing a value at a memory address that is a
dangling pointer can cause some minor problems, but the program
usually recovers from them quickly.
- Normally, function implementations are not placed in a
header file.
- If a C++ program does not return memory to the free
space pool when it is done with the memory, then the system
will periodically sweep through memory and return unused
memory to the free space pool automatically.
- If variables A and B each have type char*, then
test A == B will have value 1 whenever the strings
pointed to by A and B have exactly the same characters
in them
- A dangling pointer is pointer to memory that has
been deleted.
- A hash table is an excellent tool to help to sort an array.
- All data structures have a logical size that is determined
when the program is compiled.
- You are allowed to add an integer to a pointer in C++.
- An abstract data type hides the way in which it is
implemented.
- Which of the following are part of an abstract data type
specification? (Select all that apply.)
- A type.
- Prototypes for functions in the abstract data type.
- A description of how the type is represented.
- Implementations of the functions.
- Conceptual meanings of the functions.
- Suppose that the following structure definition is given.
struct Widget
{
int side;
char* front;
char bottom[22];
Widget(int s, char* f, char *b)
{
side = s;
front = f;
strcpy(bottom, b);
}
};
- Show how to create a new widget called frodo with
side = 4, front = "red" and bottom = "blue". Use the constructor.
- Show how to create the same widget frodo without using
the constructor.
- When doing object-oriented programming, you typically find
that functions have one less parameter than do corresponding
functions that are used in procedural (non-object-oriented) computing.
For example, a classical function to test whether a value x is present
in a tree t has two parameters, x and t, but an
object-oriented function has only one parameter, x.
Explain why there is one less parameter.
- The following C++ program is run. It causes a segmentation
fault. Why?
#include <iostream.h>
int main()
{
char* word;
cin >> word;
cout << word << endl;
return 0;
}
- What does the following function print when it is run?
void test()
{
int* s;
int* p = new int;
int* q = p;
int* r = new int;
*r = 17;
s = r;
*r = 41;
*q = *s;
p = s;
*r = 8;
r = q;
cout << "*p = " << *p << endl;
cout << "*q = " << *q << endl;
cout << "*r = " << *r << endl;
cout << "*s = " << *s << endl;
}
- The following function is supposed to test whether
two strings are the same, provided upper
and lower case letters are treated as equal. That is, 'a'
and 'A' should be considered the same letter. It is
supposed to return 1 if the two strings are the same (when case
is ignored) and 0 when they are not the same. For example,
sameUpper("abc", "ABC") = 1, but sameUpper("abc", "AbD") = 0.
This function works by
copying each string, replacing lower case letters by upper case
letters, and then using library function strcmp to tell whether
the resulting strings are the same. (strcmp(s,t) returns
0 if strings s and t are equal, -1 if s comes before
t in alphabetical order, and 1 if s comes after t in
alphabetical order.) The function also uses function
toupper, which converts a lower case letter to upper case, and
returns all other characters unchanged. So toupper('a') =
'A' and toupper(':') = ':'.
There is a serious mistake in this function. Explain what
the mistake is, and rewrite the function to avoid the mistake.
You might need to make more than one change, but try to keep the
spirit of the method instead of choosing a completely different method.
Make sure the external behavior of the function is correct.
bool sameUpper(char* x, char* y)
{
char *cpyx, *cpyy;
int xlen = strlen(x);
int ylen = strlen(y);
int i;
if(xlen != ylen) return 0;
for(i = 0; i <= xlen; i++) {
cpyx[i] = toupper(x[i]);
cpyy[i] = toupper(y[i]);
}
if(strcmp(cpyx, cpyy) == 0) return 1;
else return 0;
}
- Write a function called stutter that takes a null-terminated
string s as a parameter and produces, in newly allocated memory, the
string that results from repeating each character in s twice.
For example, stutter("abaac") = "aabbaaaacc". It should return a
pointer to the memory where the new string is stored.