Solutions are in blue. The answers to the multiple choice questions are
also in bold.
- A dangling pointer is
- a pointer to memory that has been deleted.
- a pointer to memory that is no longer in use.
- a pointer to memory that also has other pointers to it.
- a pointer that was created by using new.
- If Node is a type, then expression new Node returns a value of type
- Node
- Node*
- Node**
- Node&
- The C++ keyword used to return memory to the pool of available memory is
- new
- free
- release
- delete
- The run-time stack is characterized by
- automatic allocation and automatic deallocation of memory
- automatic allocation and explicit deallocation of memory.
- explicit allocation and automatic deallocation of memory.
- explicit allocation and explicit deallocation of memory.
Remark: The run-time stack is the area of memory where local
variables of functions are placed. Local variables are allocated
automatically when a function starts, and they are deallocated
automatically when the function returns.
- The heap is characterized by
- automatic allocation and automatic deallocation of memory
- automatic allocation and explicit deallocation of memory.
- explicit allocation and automatic deallocation of memory.
- explicit allocation and explicit deallocation of memory.
Remark: The heap is the area of memory where memory
that is allocated using new is located. Memory in the
heap is allocated explicitly using new, and deallocated
explicitly using delete.
- Which of the following will copy the null-terminated string that is in array src into
array dest?
- dest = src;
- dest == src;
- strcpy(dest, src);
- strcpy(src, dest);
- A pointer is a memory address. Suppose that pointer variable p holds address 1000,
and that p is declared to have type int*. Suppose that an int is 4 bytes long. What
address is represented by expression p+2?
- 1002
- 1004
- 1008
- None. This expression is not allowed.
- When passing a two-dimensional array as a parameter, which of the following must be
given along with the formal parameter?
- The physical number of columns (the second index).
- The physical number of rows (the first index).
- The logical number of columns (the second index).
- The logical number of rows (the first index).
- Which of the following allocates a new array A in the heap, holding 10 bytes?
- char A[10];
- int A[10];
- char* A = new char[10];
- int* A = new int[10];
- Which of the following is true if null-terminated strings A and B are the same string?
- strcmp(A,B) != 0
- strcmp(A,B) == 0
- A = B
- A == B
- What is the tail of list [6,2]?
- 6
- 2
- [6]
- [2]
- What is printed by the function fourteen when it is run?
void fourteen()
{
int* s;
int* p = new int;
int* q = p;
int* r = new int;
*p = 8;
s = p;
*q = 25;
*r = *s;
q = r;
cout << "*p = " << *p << endl;
cout << "*q = " << *q << endl;
cout << "*r = " << *r << endl;
cout << "*s = " << *s << endl;
}
*p = 25
*q = 25
*r = 25
*s = 25
Give a statement to allocate an array of 40 characters in the
heap, and another statement to deallocate that same array.
char* arr = new char[40];
...
delete[] arr;
Suppose that structure type Fountain and variable rain are
defined as follows.
struct Fountain
{
char* name;
int nums[30];
};
Fountain rain;
Write a statement that stores number 55 at index 3 of the nums array of rain.
rain.nums[3] = 55;
Using the same type fountain of the preceding question, suppose
that variable shower is defined by
Fountain* shower;
shower = new Fountain;
Write a statement that stores 55 at index 3 in nums array of
the Fountain object pointed to by shower.
shower->nums[3] = 55;
- Using the list abstraction presented in class (operations head, tail, cons and
emptyList) write a function allPositive(L) that returns 1 if all members of list L are
positive numbers, and returns 0 otherwise. Do not violate the abstraction by making
assumptions about the list implementation. Only use the functions that are provided.
bool allPositive(List L)
{
if(isEmpty(L)) return 1;
else if(head(L) <= 0) return 0;
else return allPositive(tail(L));
}
- Write a function matrixAdd(A,B,C) that takes as parameters three 10x10 arrays A, B
and C. Each of those arrays holds integers. Function matrixAdd should set each member
of array C to the sum of the corresponding members of A and B. For example, C[1][2]
should be set to A[1][2] + B[1][2].
void matrixAdd(double A[10][10],
double B[10][10],
double C[10][10])
{
int i,j;
for(i = 0; i < 10; i++) {
for(j = 0; j < 10; j++) {
C[i][j] = A[i][j] + B[i][j];
}
}
}
Write a function makeUpper that takes a null-terminated string
as a parameter, and produces a copy of that string, but with all letters
made upper case. The copy should be allocated in the heap. You can
use function toupper(c), which produces c if c is not a lower case letter,
and produces the upper case equivalent of c when c is a lower case letter.
For example toupper('a') = 'A' and toupper('*') = '*'.
char* makeUpper(char* s)
{
int s_len = strlen(s);
char* result = new char[s_len + 1];
int k;
for(k = 0; k <= s_len; k++) {
result[k] = toupper(s[k]);
}
return result;
}