Sample E.

Remark: The exam will not be as long as this one.
Some of the questions on the exam will be similar.
1) Decide whether the following are true or false:
a) A static method/data member of a class can be called without creating an object of the class.
b) For static methods, if you write the word static for the specification part of the method, then you
cannot write it for the implementation part of the method.
c) For const methods, the word const must me written in both the specification and implementation
parts of the method.
d) A static member of a class can not call or use a non-static member of the class. But, a non-static
member of the class can call or use a static member.
e) Modifiers (such as const) do not apply to static methods.
f) The precedence or associativity of an operator cannot be changed by overloading.
g) The following operators cannot be overloaded in C++
.
.*
::
?:
sizeof
h) You can not create new operators. You can overload only existing operators (not all of them).
i) The time complexity (running time) of binary search of a one-dimensional array of N elements is
O(log2 N).
j) The insertItem and deleteItem methods for UnsortedType with arrays (unsorted lists as arrays) do
not work for SortedType with arrays (sorted lists as arrays).
k) The retrieveItem method for UnsortedType with arrays (unsorted lists as arrays) works for
SortedType with arrays (sorted lists as arrays), but it can be replaced by a more efficient version.
l) The only literal pointer constant is 0 (null pointer - points to nothing).
m) If pointer is a pointer, then pointer = 0 is the same as pointer = NULL.
n) If array is an array, then array and array[0] have the same address.
o) If array is an array and pointer is a pointer, then pointer1 = array has the same effect as pointer1 =
&array[0].
p) If pointer is a pointer, then *pointer.name is interpreted as *(pointer.name).
q) If pointer is a pointer, then (*pointer).name can be written as pointer -> name.
r) Dynamic variables are variables allocated and deallocated at execution (run) time by using the
operators new and delete. But, memory space is allocated for static variables/arrays at compilation
time).
s) Unlike static variables whose life time is the execution time of the program/function, the life time
of a dynamic variable is the period from creating it using new to deleting it using delete.
t) A dynamic variable has no name and cannot be addressed directly.
u) If pointer is a pointer pointing to a dynamic variable/array, then delete pointer does not delete the
pointer. It deletes the variable/array the pointer is pointing to.
v) The delete operator can be applied ONLY to a pointer value created by the new operator.
w) When you create dynamic variables/arrays, you must delete them when you longer use them.
Failing to do so, results in memory leak.
x) The destructor for QueueType as a dynamic array is O(1).
y) The destructor for QueueType as a linked structor is O(N), where N is the size of the queue.
z) If you have a class called MyClass and if you declare array to be an array of type MyClass, then
MyClass must have a default constructor. If not, then there will be an error.
aa) Destructors do not have parameters and cannot be overloaded.
bb) A destructor is public and has no return type
2) What is the time complexity (running time) of the following (write the answer in terms of N, where N
is the size of the input)?
a) insertItem for UnsortedType as an array.
b) insertItem for SortedType as an array.
c) insertItem for UnsortedType as a linked structure (i.e. unsorted linked list).
d) deleteItem for UnsortedType as an array.
e) deleteItem for SortedType as an array.
f) deleteItem for UnsortedType as a linked structure (i.e. unsorted linked list).
g) retrieveItem for UnsortedType as an array.
h) deleteItem for SortedType as an array.
1
i)
j)
k)
l)
m)
deleteItem for UnsortedType as a linked structure (i.e. unsorted linked list).
makeEmpty for UnsortedType as a linked structure (i.e. unsorted linked list).
makeEmpty for UnsortedType as an array.
The destructor for UnsortedType as a linked structure (i.e. unsorted linked list).
int i; sum = 0;
for (i = 0; i < N; i++)
sum = sum + A[i];
n) The destructor for
3) Write a function with one parameter only. Call the parameter size and make it of type int. Your
function should create an array myArray of type int, let myArray[i] = i / 2, and then return a pointer to
the array by a return statement. Then, write a driver program to test the function.
4) All quizzes.
5) All assignments.
6)
Let
int *p, a = 8;
long *q, b = 3;
p = &a; q = &b;
Decide whether each of the following is valid or not:
1) q = &a;
2) q = p;
3) *q = *p;
7) For the following, use templates. Write the implementation part only and include the templates line
that proceeds the method.
a) Write the Push method for StackType as a dynamic array.
b) Write the Pop method for StackType as a dynamic array.
c) Write the Push method for StackType as a linked structure.
d) Write the Pop method for StackType as a linked structure.
e) Write the destructor for StackType as a linked structure.
f) Write the destructor for StackType as a dynamic array.
g) Write the insertItem method for Unsorted Type as a linked structure. Do not make any restrictions
on the method and use the needed exception classes that we did in class.
h) Write the deleteItem method for Unsorted Type as a linked structure. Do not make any restrictions
on the method and and use the needed exception classes that we did in class.
i) Write the Enqueue method for QueueType as a dynamic array.
j) Write the Dequeue method for QueueType as a dynamic array (make it void and return the deleted
element).
k) Write the Enqueue method for QueueType as a linked structure.
l) Write the Dequeue method for QueueType as a linked structure.
m) Write the destructor for StackType as a linked structure.
n) Write the destructor for StackType as a dynamic array.
1.
Let a = -5, b = -7, and c = -9, be variables of type int and let p1, ... , p6 be pointers of type int, and
suppose that the memory addresses reserved for them are as follows:
2
Find the output of the following:
p1 = &a; p2 = &b; p3 = &c;
cout << p1 << endl << &a << endl << &p1
<< endl << *p1 << endl << a << endl
<< &*p1 << endl;
p1 = &b;
cout << p1 << endl << &a << endl << &p1
<< endl << *p1 << endl << a << endl
<< &*p1 << endl;
p3 = p2;
cout << p3 << endl << &b << endl << &p3
<< endl << *p3 << endl << &*p3 << endl;
p4 = &a; p5 = &c;
*p4 = *p5;
cout << p4 << endl << p5 << endl
<< *p4 << endl << *p5 << endl
<< a << endl << c << endl
<< &*p4 << endl << &*p5 << endl;
b = 13;
cout << *p3 << endl << *p2 << endl;
*p3 = 99;
cout << b << endl << *p2 << endl;
Note: The solution for the above question is in Lecture 5.
2. Find the output of the following for each of the following input.
#include <iostream>
#include <cmath>
using namespace std;
float f(float,float);
class ExceptionA
3
{
};
class ExceptionB
{
};
class ExceptionC
{
};
int main()
{
float x, y, z;
cout << "Enter x followed by y: ";
cin >> x >> y;
try
{
z = f(x,y);
cout << z<< endl;
}
catch(ExceptionA)
{
cout << "Division by zero.\n";
}
catch(ExceptionB)
{
cout << "A negative denominator is not allowed.\n";
}
catch(...)
{
cout << "There is something wrong.\n";
}
return 0;
}
float f(float x, float y)
{
if (y == 0)
throw ExceptionA();
else if (y < 0)
throw ExceptionB();
else if (y > 1000)
throw ExceptionC();
return (x / sqrt(y));
}
a) 3
0
b) 5
-8
c) 7
2000
3. Find the output of the following program if the input is
5 2000
If there is an error that results in the termination of the program, write down what the error is (of course in
this case there should be no output):
#include <iostream>
#include <cmath>
using namespace std;
float f(float,float);
4
class ExceptionA
{
};
class ExceptionB
{
};
class ExceptionC
{
};
int main()
{
float x, y, z;
cout << "Enter x followed by y: ";
cin >> x >> y;
try
{
z = f(x,y);
cout << z<< endl;
}
catch(ExceptionA)
{
cout << "Division by zero.\n";
}
catch(ExceptionB)
{
cout << "A negative denominator is not allowed.\n";
}
catch(...)
{
cout << "There is something wrong.\n";
}
return 0;
}
float f(float x, float y)
{
if (y == 0)
throw ExceptionA();
else if (y < 0)
throw ExceptionB();
else if (y > 1000)
throw ExceptionC();
return (x / sqrt(y));
}
5