CSCI 159: exam practice questions
These are just meant as a way to get some practice answering
exam-style questions with pen/paper. I'd encourage students
to attempt them with minimal references to their notes, but
for the final exam you will be permitted one double-sided
sheet of notes.
There is one question on each side of the page.
After you've attempted the question, a good way to evaluate your answer
while also getting additional practice is to coding and compiling/running
the answer you came up with.
Question 1: Basic pointers
For each of the cout statements below, show the values of x and y
and explain why they have those values.
int x, y;
y = 32;
x = 5;
int *ptr1;
int *ptr2;
ptr1 = &x;
(*ptr1) = 10;
x++;
cout << x << "," << y << endl;
ptr2 = ptr1;
ptr1 = &y;
y = 45;
cout << x << "," << y << endl;
(*ptr1) = 99;
(*ptr2) = (*ptr1) - 5;
cout << x << "," << y << endl;
Question 2: Arrays, new and delete[]
Write a main routine that does the following in the sequence shown:
- gets an integer size from the user (no error checking required),
- allocates one array of doubles of
that size and one array of ints of that size (using new for each of the allocations),
- fills the array of ints with values the user supplies (again no error checking required),
- fills the array of doubles with the square roots of the corresponding ints
(e.g. if our arrays were mydoubles and myints then mydoubles[i] gets the square root of myints[i]),
- deletes the array of ints,
- prints the elements from the array of doubles,
- deletes the array of doubles