You are on page 1of 29

oracle test codings i wrote oracle test today,,.i couldnt copy other question ..

i copiedthe ADvance coding techniques.chk this ---------------------------Given the following code snippet: void InsertNode(tNode** node, int i){ if(*node == NULL){ *node = new tNode; (*node)->pLeft = NULL; (*node)->data = i; (*node)->pRight = NULL; SetRootNode(node); return; } else{ if(i < (*node)->data) InsertNode(&((*node)->pLeft), i); if(i > (*node)->data) InsertNode(&((*node)->pRight) , i); return; } } void Func(tNode **node){ if(*node!=NULL){ Func(&(*node)->pLeft); tNode *temp; temp = (*node)->pLeft; (*node)->pLeft= (*node)->pRight; (*node)->pRight = temp; Func(&(*node)->pRight); } } void traverse(tNode** nd){ if(*nd!=NULL){ traverse(&((*nd)->pLeft)); traverse(&((*nd)->pRight)); std::cout<<(*nd)->data<<std:: endl; } } Let the input given be 98,15,100,10,78,120,5,12,96,110 What would be the output of the following code snippet? int main(void) { tree *bT = new tree; int i = 10; int data; while(i--){ std::cout<<"Enter the node"<<std::endl; std::cin>>data; bT->InsertNode(bT-> GetRootNode(), data); }

bT->InsertNode(bT-> GetRootNode(), 97); bT->Func(bT->GetRootNode()); bT->InsertNode(bT-> GetRootNode(), 99); bT->traverse(bT->GetRootNode( )); } 98,15,78,96,97,99,10,12,5,100,120,110 110,120,100,5,12,10,99,97,96,78,15,98 100,110,120,98,5,10,12,15,78,96,97,99

None of these ------------------------------------------------------------Question Number 2 Given the following code snippet: void InsertNode(tNode** node, int i){ if(*node == NULL){ *node = new tNode; (*node)->pLeft = NULL; (*node)->data = i; (*node)->pRight = NULL; SetRootNode(node); return; } else{ if(i < (*node)->data) InsertNode(&((*node)->pLeft), i); if(i > (*node)->data) InsertNode(&((*node)->pRight) , i); return; } } void Func(tNode **node){ if(*node!=NULL){ Func(&(*node)->pLeft); tNode *temp; temp = (*node)->pLeft; (*node)->pLeft= (*node)->pRight; (*node)->pRight = temp; Func(&(*node)->pRight); } } void traverse(tNode** nd){ if(*nd!=NULL){ traverse(&((*nd)->pLeft));

traverse(&((*nd)->pRight)); std::cout<<(*nd)->data<<std:: endl; } } Let the input given be 98,15,100,10,78,120,5,12,96,110 What would be the output of the following code snippet? int main(void) { tree *bT = new tree; int i = 10; int data; while(i--){ std::cout<<"Enter the node"<<std::endl; std::cin>>data; bT->InsertNode(bT-> GetRootNode(), data); } bT->InsertNode(bT-> GetRootNode(), 99); bT->traverse(bT->GetRootNode( )); } 120,110,100,99,98,96,78,15,12,10,5 98,100,120,110,99,15,78,96,12,5,110 110,5,12,96,78,15,99,110,120,100,98 5,10,12,15,78,96,98,99,100, 110,120 -----------------------------------------------Question Number 3 Given the following code snippet: void InsertNode(tNode** node, int i){ if(*node == NULL){ *node = new tNode; (*node)->pLeft = NULL; (*node)->data = i; (*node)->pRight = NULL; SetRootNode(node); return; } else{ if(i < (*node)->data) InsertNode(&((*node)->pLeft), i); if(i > (*node)->data) InsertNode(&((*node)->pRight)

, i); return; } } void Func(tNode **node){ if(*node!=NULL){ Func(&(*node)->pLeft); tNode *temp; temp = (*node)->pLeft; (*node)->pLeft= (*node)->pRight; (*node)->pRight = temp; Func(&(*node)->pRight); } } void traverse(tNode** nd){ if(*nd!=NULL){ traverse(&((*nd)->pLeft)); traverse(&((*nd)->pRight)); std::cout<<(*nd)->data<<std:: endl; } } Let the input given be 98,15,100,10,78,120,5,12,96,110 What would be the output of the following code snippet? int main(void) { tree *bT = new tree; int i = 10; int data; while(i--){ std::cout<<"Enter the node"<<std::endl; std::cin>>data; bT->InsertNode(bT->GetRootNode(), data); } bT->Func(bT->GetRootNode()); bT->InsertNode(bT-> GetRootNode(), 99); bT->Func(bT->GetRootNode()); bT->traverse(bT->GetRootNode( )); }

5,12,10,99,96,78,15,110,120,100,98 98,100,120,110,15,78,96,99,10,12,5 5,10,12,15,78,96,98,99,100,110,120 5,10,12,15,78,96,99,98,100,

110,120 -----------------------------------------Question Number 4 Given the following code snippet: void InsertNode(tNode** node, int i){ if(*node == NULL){ *node = new tNode; (*node)->pLeft = NULL; (*node)->data = i; (*node)->pRight = NULL; SetRootNode(node); return; } else{ if(i < (*node)->data) InsertNode(&((*node)->pLeft), i); if(i > (*node)->data) InsertNode(&((*node)->pRight) , i); return; } } void Func(tNode **node){ if(*node!=NULL){ Func(&(*node)->pLeft); tNode *temp; temp = (*node)->pLeft; (*node)->pLeft= (*node)->pRight; (*node)->pRight = temp; Func(&(*node)->pRight); } } void traverse(tNode** nd){ if(*nd!=NULL){ traverse(&((*nd)->pLeft)); traverse(&((*nd)->pRight)); std::cout<<(*nd)->data<<std:: endl; } } Let the input given be 98,15,100,10,78,120,5,12,96,110 What would be the output of the following code snippet? int main(void) { tree *bT = new tree; int i = 10; int data; while(i--){ std::cout<<"Enter the node"<<std::endl; std::cin>>data; bT->InsertNode(bT-> GetRootNode(), data); }

bT->Func(bT->GetRootNode()); bT->InsertNode(bT-> GetRootNode(), 99); bT->traverse(bT->GetRootNode( )); } 98,15,78,96,99,10,12,5,100,120,110 100,110,120,98,5,10,12,15,78,96,99 110,120,100,5,12,10,99,96,78,15,98

99,96,78,15,12,10,5,98,120,110,100 ---------------------------------------------------------The subject of these questions is an unusually simple kind of binary tree, defined by these properties: Terminal nodes contain a string. Internal nodes have one or two children, called "left" and "right". Either child of an internal node may be null, but not both. Internal nodes contain no other information. By "tree" we simply mean a node and all of its descendants. A tree rooted at a node having left child A and right child B is a different tree than one rooted at a node having left child B and right child A. Here's an example, with plus signs (+) used to indicate internal nodes: + /\ / \ / \ + + / /\ / / \ / / \ "A" + "D" /\ / \ / \ "B" "C" Suppose InternalNode implements getLeft and getRight as part of its public interface to allow external code to navigate the tree. Their return type must be Node, because they may return either an internal or a terminal node. Which of the following is true: Node and TerminalNode must both provide concrete implementations of getLeft and getRight.

It is sufficient to implement getLeft and getRight in TerminalNode. It is sufficient to implement getLeft and getRight in Node. None of these options -------------------------------------------------------------------------Question Number 6 The subject of these questions is an unusually simple kind of binary tree, defined by these properties: Terminal nodes contain a string. Internal nodes have one or two children, called "left" and "right". Either child of an internal node may be null, but not both. Internal nodes contain no other information. By "tree" we simply mean a node and all of its descendants. A tree rooted at a node having left child A and right child B is a different tree than one rooted at a node having left child B and right child A. Here's an example, with plus signs (+) used to indicate internal nodes: + /\ / \ / \ + + / /\ / / \ / / \ "A" + "D" /\ / \ / \ "B" "C" Consider this implementation of toString(): abstract class Node { abstract String toString(); } class TerminalNode extends Node { String toString() { return value; } } class InternalNode extends Node { String toString() { if (null == left) return "[" + right + "]"; else if (null == right) return "[" + left + "]"; else return "[" + left + " | " + right + "]";

} } Under what conditions will this implementation distinguish (i.e. produce different strings for) two trees that have the same strings in the same sequence in their terminal nodes but different internal structures? Never Only for full trees (ones in which every internal node has two children, except possibly the parent of the rightmost terminal node) Always ------------------------------------------------------Question Number 7 The subject of these questions is an unusually simple kind of binary tree, defined by these properties: Terminal nodes contain a string. Internal nodes have one or two children, called "left" and "right". Either child of an internal node may be null, but not both. Internal nodes contain no other information. By "tree" we simply mean a node and all of its descendants. A tree rooted at a node having left child A and right child B is a different tree than one rooted at a node having left child B and right child A. Here's an example, with plus signs (+) used to indicate internal nodes: + /\ / \ / \ + + / /\ / / \ / / \ "A" + "D" /\ / \ / \ "B" "C" Should InternalNode have simple setters, as follows? void setLeft(Node nd) { left = nd; } void setRight(Node nd) { right = nd; }

Node removeRight() { Node node = right; right = null; return node; } InternalNode removeRight() { InternalNode node = right; right = null; return node; } Node removeRight() { right = null; return right; } None of these options, since under some circumstances removing the right subtree of an InternalNode would violate the specifications. ------------------------------------------------------------------Question Number 8 The subject of these questions is an unusually simple kind of binary tree, defined by these properties: Terminal nodes contain a string. Internal nodes have one or two children, called "left" and "right". Either child of an internal node may be null, but not both. Internal nodes contain no other information. By "tree" we simply mean a node and all of its descendants. A tree rooted at a node having left child A and right child B is a different tree than one rooted at a node having left child B and right child A. Here's an example, with plus signs (+) used to indicate internal nodes: + /\ / \ / \ + + / /\ / / \ / / \ "A" + "D" /\ / \ / \ "B" "C" Suppose we want to be able to grow a tree by replacing the empty left or right subtree of an InternalNode (failing if that subtree is not empty). Since this operation will fail under certain common conditions well have the method return a boolean to indicate whether or not it

succeeded, rather than throwing an exception. Which of the following best implement that idea in keeping with the specifications in the instructions? boolean addLeft(Node nd) { if (left == null) { left = nd; return true; } else return false; } and addRight similarly

boolean addLeft(Node nd) { if (nd == null) return false; else { left = nd; return true; } } and addRight similarly boolean add(Node nd) { if (nd == null && right == null) return false; else { left = nd; return true; } } and addRight similarly boolean add(Node nd) { if (nd == null) return false; else if (left == null) { left = nd; return true; } else if (right == null) right = nd; return true; } else return false; } --------------------------------------------------------Question Number 9 The subject of these questions is an unusually simple kind of binary

tree, defined by these properties: Terminal nodes contain a string. Internal nodes have one or two children, called "left" and "right". Either child of an internal node may be null, but not both. Internal nodes contain no other information. By "tree" we simply mean a node and all of its descendants. A tree rooted at a node having left child A and right child B is a different tree than one rooted at a node having left child B and right child A. Here's an example, with plus signs (+) used to indicate internal nodes: + /\ / \ / \ + + / /\ / / \ / / \ "A" + "D" /\ / \ / \ "B" "C" Consider the following implementation of count as the number of nodes in a tree? abstract class Node { abstract int count(); } class TerminalNode extends Node { int count() { return 1; } } class InternalNode extends Node { int count() { return 1 + left.count() + right.count(); } }

Which of the following statements is true? A run-time error could happen if left or right of an InternalNode were null. TerminalNode.count should return 0, not 1. InternalNode.count should not add 1 to the counts of its children.

The code is correct as shown. ---------------------------------------------Coding Skills (Advanced) Support ID: 110E229 Question Number 10 The subject of these questions is an unusually simple kind of binary tree, defined by these properties: Terminal nodes contain a string. Internal nodes have one or two children, called "left" and "right". Either child of an internal node may be null, but not both. Internal nodes contain no other information. By "tree" we simply mean a node and all of its descendants. A tree rooted at a node having left child A and right child B is a different tree than one rooted at a node having left child B and right child A. Here's an example, with plus signs (+) used to indicate internal nodes: + /\ / \ / \ + + / /\ / / \ / / \ "A" + "D" /\ / \ / \ "B" "C" Which of the following correctly defines count as the number of nodes in a tree if none of the other classes implement count? abstract class Node { int count() { return 1 + left.count() + right.count(); } }

abstract class Node { int count() { return 1 + ((left == null) ? 0 : left.count()) + ((right == null) ? 0 : right.count()); } }

A is correct, but B is not.

B is correct, but not A. Neither is correct. Both are correct. ---------------------------------------Support ID: 110E236 Skipped: Question Number 9 The subject of these questions is an unusually simple kind of binary tree, defined by these properties: Terminal nodes contain a string. Internal nodes have one or two children, called "left" and "right". Either child of an internal node may be null, but not both. Internal nodes contain no other information. By "tree" we simply mean a node and all of its descendants. A tree rooted at a node having left child A and right child B is a different tree than one rooted at a node having left child B and right child A. Here's an example, with plus signs (+) used to indicate internal nodes: + /\ / \ / \ + + / /\ / / \ / / \ "A" + "D" /\ / \ / \ "B" "C" Consider the following implementation of count as the number of nodes in a tree? abstract class Node { abstract int count(); } class TerminalNode extends Node { int count() { return 1; } } class InternalNode extends Node { int count() { return 1 + left.count() + right.count(); } }

Which of the following statements is true? A run-time error could happen if left or right of an InternalNode were null. TerminalNode.count should return 0, not 1. InternalNode.count should not add 1 to the counts of its children. The code is correct as shown. ============================== ======= Question Number 1 Given the following code snippet answer the following question. struct AVLTree { AVLTree * left; AVLTree * right; int element; int height; }; int MAX(int a, int b){ if(a>=b) return a; if(a<b) return b; } int height(AVLTree *node) { if (node == NULL) { return -1; } else { return node->height; } } AVLTree * single_rotation_with_left( AVLTree *k2) { AVLTree *k1; k1 = k2->left; k2->left = k1->right; k1->right = k2; k2->height = MAX(height(k2->left), height(k2->right)) + 1; k1->height = MAX(height(k1->left), height(k2->right)) + 1; return k1; } AVLTree * single_rotation_with_right( AVLTree *k2) {

AVLTree *k1; k1 = k2->right; k2->right = k1->left; k1->left = k2; k2->height = MAX(height(k2->left), height(k2->right)) + 1; k1->height = MAX(height(k1->right), height(k2->left)) + 1; return k1; } AVLTree *double_rotation_with_left( AVLTree *k3) { k3->left = single_rotation_with_right(k3>left); return single_rotation_with_left(k3); } AVLTree *double_rotation_with_right( AVLTree *k3) { k3->right = single_rotation_with_left(k3-> right); return single_rotation_with_right(k3) ; } void insert(int value, AVLTree **node) { if (*node == NULL) { *node = new AVLTree; if (*node == NULL) { return; } (*node)->element = value; (*node)->height = 0; (*node)->left = (*node)->right = NULL; return; } else if (value < (*node)->element) { insert(value, &((*node)->left)); if (height((*node)->left) - height((*node)->right) == 2) { if (value < (*node)->left->element) { *node = single_rotation_with_left(* node); } else { *node = double_rotation_with_left(* node); } } } else if (value > (*node)->element) { insert(value, &((*node)->right)); if (height((*node)->right) - height((*node)->left) == 2) { if (value > (*node)->right->element) {

*node = single_rotation_with_right(* node); } else { *node = double_rotation_with_right(* node); } } } (*node)->height = MAX(height((*node)->left), height((*node)->right)) + 1; } Consider an input sequence that is provided as an input to the insert method 20,5,15,9,13,2,6,12,14,15,16, 17,18,19 Let's give the root node of the resulting tree as input to the following code snippet int func(AVLTree **p) { if (*p!=0) return func (&(*p)->right) + 1; else return 0; } What would be the return value after execution of the above code snippet? 3 4 7 6 -------------------------------------------------------Question Number 2 Given the following code snippet answer the following question. struct AVLTree { AVLTree * left; AVLTree * right; int element; int height; }; int MAX(int a, int b){ if(a>=b) return a; if(a<b)

return b; } int height(AVLTree *node) { if (node == NULL) { return -1; } else { return node->height; } } AVLTree * single_rotation_with_left( AVLTree *k2) { AVLTree *k1; k1 = k2->left; k2->left = k1->right; k1->right = k2; k2->height = MAX(height(k2->left), height(k2->right)) + 1; k1->height = MAX(height(k1->left), height(k2->right)) + 1; return k1; } AVLTree * single_rotation_with_right( AVLTree *k2) { AVLTree *k1; k1 = k2->right; k2->right = k1->left; k1->left = k2; k2->height = MAX(height(k2->left), height(k2->right)) + 1; k1->height = MAX(height(k1->right), height(k2->left)) + 1; return k1; } AVLTree *double_rotation_with_left( AVLTree *k3) { k3->left = single_rotation_with_right(k3>left); return single_rotation_with_left(k3); } AVLTree *double_rotation_with_right( AVLTree *k3) { k3->right = single_rotation_with_left(k3-> right); return single_rotation_with_right(k3) ; } void insert(int value, AVLTree **node) { if (*node == NULL) { *node = new AVLTree; if (*node == NULL) { return; } (*node)->element = value;

(*node)->height = 0; (*node)->left = (*node)->right = NULL; return; } else if (value < (*node)->element) { insert(value, &((*node)->left)); if (height((*node)->left) - height((*node)->right) == 2) { if (value < (*node)->left->element) { *node = single_rotation_with_left(* node); } else { *node = double_rotation_with_left(* node); } } } else if (value > (*node)->element) { insert(value, &((*node)->right)); if (height((*node)->right) - height((*node)->left) == 2) { if (value > (*node)->right->element) { *node = single_rotation_with_right(* node); } else { *node = double_rotation_with_right(* node); } } } (*node)->height = MAX(height((*node)->left), height((*node)->right)) + 1; } Consider an input sequence that is provided as an input to the insert method 20,5,15,9,13,2,6,12,14,15,16, 17,18,19 Let's give the root node of the resulting tree as input to the following code snippet int func(AVLTree **p) { if (*p!=0) return func (&(*p)->right) + func (&(*p)->left) + 1; else return 0; } What would be the return value after execution of the above code snippet?

13 14 15 None of these ----------------------------------------Question Number 3 Given the following code snippet answer the following question. struct AVLTree { AVLTree * left; AVLTree * right; int element; int height; }; int MAX(int a, int b){ if(a>=b) return a; if(a<b) return b; } int height(AVLTree *node) { if (node == NULL) { return -1; } else { return node->height; } } AVLTree * single_rotation_with_left( AVLTree *k2) { AVLTree *k1; k1 = k2->left; k2->left = k1->right; k1->right = k2; k2->height = MAX(height(k2->left), height(k2->right)) + 1; k1->height = MAX(height(k1->left), height(k2->right)) + 1; return k1; } AVLTree * single_rotation_with_right( AVLTree *k2) { AVLTree *k1; k1 = k2->right; k2->right = k1->left; k1->left = k2;

k2->height = MAX(height(k2->left), height(k2->right)) + 1; k1->height = MAX(height(k1->right), height(k2->left)) + 1; return k1; } AVLTree *double_rotation_with_left( AVLTree *k3) { k3->left = single_rotation_with_right(k3>left); return single_rotation_with_left(k3); } AVLTree *double_rotation_with_right( AVLTree *k3) { k3->right = single_rotation_with_left(k3-> right); return single_rotation_with_right(k3) ; } void insert(int value, AVLTree **node) { if (*node == NULL) { *node = new AVLTree; if (*node == NULL) { return; } (*node)->element = value; (*node)->height = 0; (*node)->left = (*node)->right = NULL; return; } else if (value < (*node)->element) { insert(value, &((*node)->left)); if (height((*node)->left) - height((*node)->right) == 2) { if (value < (*node)->left->element) { *node = single_rotation_with_left(* node); } else { *node = double_rotation_with_left(* node); } } } else if (value > (*node)->element) { insert(value, &((*node)->right)); if (height((*node)->right) - height((*node)->left) == 2) { if (value > (*node)->right->element) { *node = single_rotation_with_right(* node); } else

{ *node = double_rotation_with_right(* node); } } } (*node)->height = MAX(height((*node)->left), height((*node)->right)) + 1; } Consider an input sequence that is provided as an input to the insert method 20,5,15,9,13,2,6,12,14,15,16, 17,18,19 In the process of inserting the above nodes how many times double_rotation_with_right is being called

3 5 2 ---------------------------------Question Number 4 The subject of these questions is an unusually simple kind of binary tree, defined by these properties: Terminal nodes contain a string. Internal nodes have one or two children, called "left" and "right". Either child of an internal node may be null, but not both. Internal nodes contain no other information. By "tree" we simply mean a node and all of its descendants. A tree rooted at a node having left child A and right child B is a different tree than one rooted at a node having left child B and right child A. Here's an example, with plus signs (+) used to indicate internal nodes: + /\ / \ / \ + + / /\ / / \ / / \ "A" + "D" /\ / \ / \

"B"

"C"

The problem with the previous definition of InternalNode.sameShape is that it calls nd.getLeft() and nd.getRight(), but only InternalNode defines those methods and nd could be a TerminalNode. The following code fixes that and will compile, but it omits the tests for null abstract class Node { abstract boolean isTerminal(); abstract boolean sameShape(Node); } class TerminalNode extends Node { boolean isTerminal() { return true; } boolean sameShape(Node nd) { return nd.isTerminal(); } } class InternalNode extends Node { boolean isTerminal() { return false; } boolean sameShape(Node nd) { return !nd.isTerminal() && left.sameShape((( InternalNode)nd).getLeft()) && right.sameShape((( InternalNode)nd).getRight()); } } If sameShape is called on an InternalNode with a non-null argument, which of the two methods could end up getting called with a null argument for certain trees? Neither would ever get called with a null argument. TerminalNode.sameShape might be called with a null argument but not InternalNode.sameShape. InternalNode.sameShape might be called with a null argument but not TerminalNode.sameShape. Both methods could get called with a null argument. -------------------------------------------------------Question Number 5 The subject of these questions is an unusually simple kind of binary tree, defined by these properties: Terminal nodes contain a string.

Internal nodes have one or two children, called "left" and "right". Either child of an internal node may be null, but not both. Internal nodes contain no other information. By "tree" we simply mean a node and all of its descendants. A tree rooted at a node having left child A and right child B is a different tree than one rooted at a node having left child B and right child A. Here's an example, with plus signs (+) used to indicate internal nodes: + /\ / \ / \ + + / /\ / / \ / / \ "A" + "D" /\ / \ / \ "B" "C" It is proposed to add a check for null at the beginning of each method, returning False if the argument is null, since a Node object would never have the same shape as null. This change is shown in the code below. Which methods will compile and work correctly now? abstract class Node { abstract boolean equals(Node); } class TerminalNode extends Node { boolean sameShape(Node nd) { return nd != null; } } class InternalNode extends Node { boolean sameShape(Node nd) { return nd != null && left.sameShape(nd.getLeft()) && right.sameShape(nd.getRight() ); } } Both TerminalNode.sameShape and InternalNode.sameShape are correct. TerminalNode.sameShape is fine the way it is but InternalNode.sameShape will not compile. TerminalNode.sameShape is fine the way it is but although TerminalNode.sameShape will compile, it is incorrect.

Neither TerminalNode.sameShape nor InternalNode.equals provides a correct solution: each either does not compile or is not correct. ----------------------------------------------Question Number 6 The subject of these questions is an unusually simple kind of binary tree, defined by these properties: Terminal nodes contain a string. Internal nodes have one or two children, called "left" and "right". Either child of an internal node may be null, but not both. Internal nodes contain no other information. By "tree" we simply mean a node and all of its descendants. A tree rooted at a node having left child A and right child B is a different tree than one rooted at a node having left child B and right child A. Here's an example, with plus signs (+) used to indicate internal nodes: + /\ / \ / \ + + / /\ / / \ / / \ "A" + "D" /\ / \ / \ "B" "C" Here is a proposed implementation of sameShape for the tree structure: abstract class Node { abstract boolean sameShape(Node nd); } class TerminalNode extends Node { boolean sameShape(Node nd) { return true; } } class InternalNode extends Node { boolean sameShape(Node nd) { return left.sameShape(nd.getLeft()) && right.sameShape(nd.getRight()) ; } } Which classes contain correctly implemented methods?

TerminalNode InternalNode A, but not B B, but not A Neither Both --------------------------Support ID: 110E22E Question Number 7 The subject of these questions is an unusually simple kind of binary tree, defined by these properties: Terminal nodes contain a string. Internal nodes have one or two children, called "left" and "right". Either child of an internal node may be null, but not both. Internal nodes contain no other information. By "tree" we simply mean a node and all of its descendants. A tree rooted at a node having left child A and right child B is a different tree than one rooted at a node having left child B and right child A. Here's an example, with plus signs (+) used to indicate internal nodes: + /\ / \ / \ + + / /\ / / \ / / \ "A" + "D" /\ / \ / \ "B" "C" One problem with the proposed implementation of sameShape is that two nodes of different types cannot have the same shape. In the following code which of TerminalNode.sameShape and InternalNode.sameShape correct this problem (though possibly leaving others uncorrected)? abstract class Node { abstract boolean isTerminal(); abstract boolean sameShape(Node); } class TerminalNode extends Node { boolean isTerminal() {

return true; } boolean sameShape(Node nd) { return nd != null && nd.isTerminal(); } } class InternalNode extends Node { boolean isTerminal() { return false; } boolean sameShape(Node nd) { return nd != null && !nd.isTerminal() && getLeft().sameShape((( InternalNode)nd).getLeft()) && getRight().sameShape((( InternalNode)nd).getRight()); } } Neither TerminalNode but not InternalNode InternalNode but not TerminalNode Both -------------------------------------------Question Number 8 The subject of these questions is an unusually simple kind of binary tree, defined by these properties: Terminal nodes contain a string. Internal nodes have one or two children, called "left" and "right". Either child of an internal node may be null, but not both. Internal nodes contain no other information. By "tree" we simply mean a node and all of its descendants. A tree rooted at a node having left child A and right child B is a different tree than one rooted at a node having left child B and right child A. Here's an example, with plus signs (+) used to indicate internal nodes: + /\ / \ / \ + + / /\ / / \ / / \

"A"

+ "D" /\ / \ / \ "B" "C"

We want to define an operation to get the list element at position N (with the first element is at position 0). Which statements are correct in the following implementation? In answering, take into account all details of the statements labelled (A) and (B). class SinglyLinkedList { InternalNode first; String nth(int n) { InternalNode nd = first; while (n-- > 0) nd = nd.getRight(); return ((TerminalNode)(nd.getLeft())) .getValue(); // (B) } } A is completely correct, but B is not B is completely correct, but A is not Neither is completely correct. Both are completely correct. ============================== Question Number 9 The subject of these questions is an unusually simple kind of binary tree, defined by these properties: Terminal nodes contain a string. Internal nodes have one or two children, called "left" and "right". Either child of an internal node may be null, but not both. Internal nodes contain no other information. By "tree" we simply mean a node and all of its descendants. A tree rooted at a node having left child A and right child B is a different tree than one rooted at a node having left child B and right child A. Here's an example, with plus signs (+) used to indicate internal nodes: + /\ / \ / \ + + / /\ / / \ / / \ "A" + "D"

// (A)

/\ / \ / \ "B" "C" In the implementation of many linear linked structures it is often useful to keep a pointer to the last node in addition to the first, so that a new node can be added at the end without having to loop through the entire structure looking for the last node. In what ways would adding a pointer to the last node improve the efficiency of this dictionary implementation? It would speed up put operations when the dictionary contains an item with the designated key. It would speed up put operations when the dictionary does not contain an item with the designated key. A but not B B but not A Both Neither --------------------------------Question Number 10 The subject of these questions is an unusually simple kind of binary tree, defined by these properties: Terminal nodes contain a string. Internal nodes have one or two children, called "left" and "right". Either child of an internal node may be null, but not both. Internal nodes contain no other information. By "tree" we simply mean a node and all of its descendants. A tree rooted at a node having left child A and right child B is a different tree than one rooted at a node having left child B and right child A. Here's an example, with plus signs (+) used to indicate internal nodes: + /\ / \ / \ + + / /\ / / \ / / \ "A" + "D" /\ / \

/ "B"

\ "C"

Under what conditions would the following either produce an error or return an incorrect result? (Assume that given a top-level InternalNode (one whose left child is also an InternalNode) next and getValue are correctly defined.) public String get(String y) throws Exception { InternalNode cur = first; while ((null != cur) && (!k.equals(getKey(cur)))) cur = next(cur); return getValue(cur); when the dictionary is empty when the dictionary contains an entry whose key is equal to k when the dictionary does not contain an entry whose key is equal to k A and B but not C A only B and C but not A It is correct for all three conditions. -----------------------------------