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.
struct Widget { int side; char* front; char bottom[22]; Widget() {} Widget(int s, char* f, char *b) { side = s; front = f; strcpy(bottom, b); } };
Widget frodo(4, "red", "blue");
Widget frodo; frodo.side = 4; frodo.front = "red" strcpy(frodo.bottom, "blue");
Each function that belongs to an object can access the
object to which it belongs implicitly. So there is always
one implicit parameter, the object in which the function resides.
The implicit parameter is the one that occurs in front
of the dot when the function is used. For example, if you
write t.member(x), the implicit parameter is t. This compares
with member(t,x) for the non-object-oriented function.
#include <iostream.h> int main() { char* word; cin >> word; cout << word << endl; return 0; }
No memory is allocated for word. So the line cin >> word is using an uninitialized pointer.
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; }
*p = 8
*q = 41
*r = 41
*s = 8
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, even if you think this method can be improved upon. 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; }
No memory is allocated for the copies. Just before the
for-loop, add
cpyx = new char[xlen + 1];
cpyy = new char[ylen + 1];
But now that these arrays have been allocated, we want to deallocate
them before returning. So replace the if-statement at the
end by
bool result;
if(strcmp(cpyx, cpyy) == 0) result = 1;
else result = 0;
delete [] cpyx;
delete [] cpyy;
return result;
The final function definition is as follows.
bool sameUpper(char* x, char* y)
{
char *cpyx, *cpyy;
int xlen = strlen(x);
int ylen = strlen(y);
int i;
if(xlen != ylen) return 0;
cpyx = new char[xlen + 1];
cpyy = new char[ylen + 1];
for(i = 0; i <= xlen; i++) {
cpyx[i] = toupper(x[i]);
cpyy[i] = toupper(y[i]);
}
bool result;
if(strcmp(cpyx, cpyy) == 0) result = 1;
else result = 0;
delete [] cpyx;
delete [] cpyy;
return result;
}
There are many correct solutions. Here is one.
char* stutter(const char *s)
{
int slen = strlen(s);
char* result = new char[2*slen + 1];
for(int k = 0; k < slen; k++) {
result[2*k] = s[k];
result[2*k + 1] = s[k];
}
result[2*slen] = '\0';
return result;
}