The exam is open book: you may consult your notes, books. But all the work should be yours.
Show all your work: you may get partial credit. Add extra sheets if
necessary.
- (10 points) Allocation of memory for variables. Given the following
Java program, specify where each variable will be allocated, and when. Write
the answers next to each variable in the program.
class Test{
static int x,y,z;
public static void main()
int a,b;
a=func(x,y,z,b);
}
int func(int a,b,c,d)
{
int x,y;
...
}
}
- (5 points) Stacks and Queues.
Give a convincing but short argument for or against using stacks to store:
- The requests sent to the printer.
- The orders received by an on-line business.
- The paper sheets as they are fed into a (real) printer.
- (20 points) Definition, allocation and access of variables in Java.
-
(5 points) Given the class definition (class variables only) for the Node of
an arbitrary tree (no restriction on the
number of children, String information, with parents):
- (5 points) Then, give basic Java code to create the tree structure below. I.e. define and
allocate nodes, and give code to link them into the tree structure. Put the code
into a default constructor.
-
(3 points) Write a Java statement to set to Alice the value of the info field
of the grand-parent of node G.
-
(3 points) Write a Java statement to change the parent of the node
H to be the node C. You should
program this according to the specifications, i.e. do not make
use of the fact that you happen to know who is the parent of the node H.
-
(4 points) Write Java statements to make all the
descendents of node C to aquire a new child each.
- (37 points) Java and C++ pointers.
Given the following C++ class definition:
class BNode{
int x;
BNode* y;
BNode* z;
};
and the following definitions of variables:
BNode a1, *a2, a3[5];
BNode* b4[10];
- (1 point each) Which of the following is a valid C++ expression? Cross
the invalid expressions.
-
a2-> y . z
-
a3[2].x ->x
-
a2->z
-
a4[2]->z->y->y.z
-
a1.y
-
a2.z
-
a3[2].x
-
a1.y.z
-
a1.y -> z
-
a1 -> z
-
a2.y -> z
-
a2->y ->y
-
a3[2].x.z
-
b4[5].y->z
-
b4[5]->y->z->z
-
b4[5]->y->z->z->x
-
a3[2]->z->y
- (10 points) Java pointers Redefine the class in Java and rewrite all the correct
statements above in Java.
- (8 points) Recursion.
Write a short recursive Java function (NOT a whole
program!!!) to compute the minimum element in a binary search tree.
The program should use the representation of a binary (search) tree as a pointer
to the root node, where a tree node is a Java structure defined as:
class TreeNode{
int data;
TreeNode left;
TreeNode right;
}
Name your function minTree. It should take as argument
the root and return an integer (the minimum value). Design the
function as a separate entity, not as part of some binary tree class.
If the recursive definition seems elusive but you have other ideas for solving
this
problem, I also accept other solutions. Warning: an iterative solution
may not be easy
to produce.
- (5 points) Stacks and recursion. Trace the stack behavior
for the following recursive function call. What is the value printed in the main function?
main()
{
int a = func(2,2);
System.out.println(a);
}
int func(int a, int b)
{
if (a==0 || b== 0) return 1;
return func(a, b-1)*func(a-1, b);
}
- (6 points) Graphs. Given the graph:
-
(2 points)Write the adjacency list representation for the graph, with each
adjacency list sorted increasingly by the vertex number.
-
(2 points) Starting from vertex A, list the vertices of the graph in the
order in which they are visited by a depth-first search algorithm.
-
(2 points) Starting at vertex A, list the vertices in the order in which
they are visited by a breadth-first search algorithm.
- (3 points) Tree Traversal. Given the following binary tree:
Write down the vertices of the tree in prefix, infix and postfix order.
- (6 points) Binary Search Trees and Sorting. Sort the list of numbers {15, 3, 12, 5, 1, 14, 2, 9, 7} using
binary search trees.
-
Step 1:construct the binary search tree.
-
Step 2: traverse the tree in the appropriate order (and specify which one).
Enjoy your winter break :-)
Ileana