Top Banner
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;
32
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Oracle

oracle test codingsi 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 be98,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);   }

Page 2: Oracle

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));

Page 3: Oracle

       traverse(&((*nd)->pRight));       std::cout<<(*nd)->data<<std::endl;   }} Let the input given be98,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)

Page 4: Oracle

, 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 be98,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,

Page 5: Oracle

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 be98,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);   }

Page 6: Oracle

   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 binarytree, 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 adifferent tree than one rooted at a node having left child B and rightchild A. Here's an example, with plus signs (+) used to indicate internalnodes:                                +                              / \                             /   \                            /     \                           +       +                          /       / \                         /       /   \                        /       /     \                      "A"      +      "D"                              / \                             /   \                            /     \                          "B"     "C" Suppose InternalNode implements getLeft and getRight as part of itspublic interface to allow external code to navigate the tree. Theirreturn type must be Node, because they may return either an internalor a terminal node. Which of the following is true:  Node and TerminalNode must both provide concrete implementations ofgetLeft and getRight.  

Page 7: Oracle

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 binarytree, 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 adifferent tree than one rooted at a node having left child B and rightchild A. Here's an example, with plus signs (+) used to indicate internalnodes:                                +                              / \                             /   \                            /     \                           +       +                          /       / \                         /       /   \                        /       /     \                      "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 + "]";

Page 8: Oracle

   }} Under what conditions will this implementation distinguish (i.e.produce different strings for) two trees that have the same strings inthe same sequence in their terminal nodes but different internalstructures?  Never  Only for “full” trees (ones in which every internal node has twochildren, except possibly the parent of the rightmost terminal node)  Always------------------------------------------------------- Question Number 7 The subject of these questions is an unusually simple kind of binarytree, 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 adifferent tree than one rooted at a node having left child B and rightchild A. Here's an example, with plus signs (+) used to indicate internalnodes:                                +                              / \                             /   \                            /     \                           +       +                          /       / \                         /       /   \                        /       /     \                      "A"      +      "D"                              / \                             /   \                            /     \                          "B"     "C" Should InternalNode have simple “setters”, as follows? void setLeft(Node nd) {   left = nd;} void setRight(Node nd) {   right = nd;} 

Page 9: Oracle

 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 theright subtree of an InternalNode would violate the specifications.------------------------------------------------------------------- Question Number 8 The subject of these questions is an unusually simple kind of binarytree, 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 adifferent tree than one rooted at a node having left child B and rightchild A. Here's an example, with plus signs (+) used to indicate internalnodes:                                +                              / \                             /   \                            /     \                           +       +                          /       / \                         /       /   \                        /       /     \                      "A"      +      "D"                              / \                             /   \                            /     \                          "B"     "C" Suppose we want to be able to grow a tree by replacing the empty leftor right subtree of an InternalNode (failing if that subtree is notempty). Since this operation will fail under certain common conditionswe’ll have the method return a boolean to indicate whether or not it

Page 10: Oracle

succeeded, rather than throwing an exception. Which of the followingbest implement that idea in keeping with the specifications in theinstructions?  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

Page 11: Oracle

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 adifferent tree than one rooted at a node having left child B and rightchild A. Here's an example, with plus signs (+) used to indicate internalnodes:                                +                              / \                             /   \                            /     \                           +       +                          /       / \                         /       /   \                        /       /     \                      "A"      +      "D"                              / \                             /   \                            /     \                          "B"     "C" Consider the following implementation of count as the number of nodesin 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 werenull.  TerminalNode.count should return 0, not 1.  InternalNode.count should not add 1 to the counts of its children. 

Page 12: Oracle

 The code is correct as shown.---------------------------------------------- Coding Skills (Advanced)Support ID: 110E229Question Number 10 The subject of these questions is an unusually simple kind of binarytree, 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 adifferent tree than one rooted at a node having left child B and rightchild A. Here's an example, with plus signs (+) used to indicate internalnodes:                                +                              / \                             /   \                            /     \                           +       +                          /       / \                         /       /   \                        /       /     \                      "A"      +      "D"                              / \                             /   \                            /     \                          "B"     "C" Which of the following correctly defines count as the number of nodesin 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. 

Page 13: Oracle

 B is correct, but not A.  Neither is correct.  Both are correct.-----------------------------------------Support ID: 110E236Skipped: Question Number 9 The subject of these questions is an unusually simple kind of binarytree, 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 adifferent tree than one rooted at a node having left child B and rightchild A. Here's an example, with plus signs (+) used to indicate internalnodes:                                +                              / \                             /   \                            /     \                           +       +                          /       / \                         /       /   \                        /       /     \                      "A"      +      "D"                              / \                             /   \                            /     \                          "B"     "C" Consider the following implementation of count as the number of nodesin 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();   }}

Page 14: Oracle

  Which of the following statements is true?  A run-time error could happen if left or right of an InternalNode werenull.  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){

Page 15: Oracle

 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)     {

Page 16: Oracle

       *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 insertmethod20,5,15,9,13,2,6,12,14,15,16,17,18,19Let's give the root node of the resulting tree as input to thefollowing 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 codesnippet?  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)

Page 17: Oracle

       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;

Page 18: Oracle

   (*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 insertmethod20,5,15,9,13,2,6,12,14,15,16,17,18,19Let's give the root node of the resulting tree as input to thefollowing 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 codesnippet? 

Page 19: Oracle

 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;

Page 20: Oracle

 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

Page 21: Oracle

     {       *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 insertmethod20,5,15,9,13,2,6,12,14,15,16,17,18,19In the process of inserting the above nodes how many timesdouble_rotation_with_right is being called     3  5  2-----------------------------------Question Number 4 The subject of these questions is an unusually simple kind of binarytree, 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 adifferent tree than one rooted at a node having left child B and rightchild A. Here's an example, with plus signs (+) used to indicate internalnodes:                                +                              / \                             /   \                            /     \                           +       +                          /       / \                         /       /   \                        /       /     \                      "A"      +      "D"                              / \                             /   \                            /     \

Page 22: Oracle

                          "B"     "C" The problem with the previous definition of InternalNode.sameShape isthat it calls nd.getLeft() and nd.getRight(), but only InternalNodedefines those methods and nd could be a TerminalNode. The followingcode 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 nullargument for certain trees?  Neither would ever get called with a null argument.  TerminalNode.sameShape might be called with a null argument but notInternalNode.sameShape.  InternalNode.sameShape might be called with a null argument but notTerminalNode.sameShape.  Both methods could get called with a null argument.---------------------------------------------------------Question Number 5 The subject of these questions is an unusually simple kind of binarytree, defined by these properties:    Terminal nodes contain a string.

Page 23: Oracle

   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 adifferent tree than one rooted at a node having left child B and rightchild A. Here's an example, with plus signs (+) used to indicate internalnodes:                                +                              / \                             /   \                            /     \                           +       +                          /       / \                         /       /   \                        /       /     \                      "A"      +      "D"                              / \                             /   \                            /     \                          "B"     "C" It is proposed to add a check for null at the beginning of eachmethod, returning False if the argument is null, since a Node objectwould never have the same shape as null. This change is shown in thecode 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 butInternalNode.sameShape will not compile.  TerminalNode.sameShape is fine the way it is but althoughTerminalNode.sameShape will compile, it is incorrect.

Page 24: Oracle

  Neither TerminalNode.sameShape nor InternalNode.equals provides acorrect solution: each either does not compile or is not correct.----------------------------------------------- Question Number 6 The subject of these questions is an unusually simple kind of binarytree, 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 adifferent tree than one rooted at a node having left child B and rightchild A. Here's an example, with plus signs (+) used to indicate internalnodes:                                +                              / \                             /   \                            /     \                           +       +                          /       / \                         /       /   \                        /       /     \                      "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? 

Page 25: Oracle

   TerminalNode   InternalNode  A, but not B  B, but not A  Neither  Both--------------------------- Support ID: 110E22EQuestion Number 7 The subject of these questions is an unusually simple kind of binarytree, 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 adifferent tree than one rooted at a node having left child B and rightchild A. Here's an example, with plus signs (+) used to indicate internalnodes:                                +                              / \                             /   \                            /     \                           +       +                          /       / \                         /       /   \                        /       /     \                      "A"      +      "D"                              / \                             /   \                            /     \                          "B"     "C" One problem with the proposed implementation of sameShape is that twonodes of different types cannot have the same shape. In the followingcode which of TerminalNode.sameShape and InternalNode.sameShapecorrect this problem (though possibly leaving others uncorrected)? abstract class Node {   abstract boolean isTerminal();   abstract boolean sameShape(Node);} class TerminalNode extends Node {   boolean isTerminal() {

Page 26: Oracle

       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 binarytree, 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 adifferent tree than one rooted at a node having left child B and rightchild A. Here's an example, with plus signs (+) used to indicate internalnodes:                                +                              / \                             /   \                            /     \                           +       +                          /       / \                         /       /   \                        /       /     \

Page 27: Oracle

                      "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 arecorrect in the following implementation? In answering, take intoaccount 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();                  // (A)       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 binarytree, 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 adifferent tree than one rooted at a node having left child B and rightchild A. Here's an example, with plus signs (+) used to indicate internalnodes:                                +                              / \                             /   \                            /     \                           +       +                          /       / \                         /       /   \                        /       /     \                      "A"      +      "D"

Page 28: Oracle

                              / \                             /   \                            /     \                          "B"     "C" In the implementation of many linear linked structures it is oftenuseful to keep a pointer to the last node in addition to the first, sothat a new node can be added at the end without having to loop throughthe entire structure looking for the last node. In what ways wouldadding a pointer to the last node improve the efficiency of thisdictionary implementation?     It would speed up put operations when the dictionary contains anitem with the designated key.    It would speed up put operations when the dictionary does notcontain 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 binarytree, 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 adifferent tree than one rooted at a node having left child B and rightchild A. Here's an example, with plus signs (+) used to indicate internalnodes:                                +                              / \                             /   \                            /     \                           +       +                          /       / \                         /       /   \                        /       /     \                      "A"      +      "D"                              / \                             /   \

Page 29: Oracle

                            /     \                          "B"     "C" Under what conditions would the following either produce an error orreturn an incorrect result? (Assume that given a “top-level”InternalNode (one whose left child is also an InternalNode) next andgetValue 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 equalto k  A and B but not C  A only  B and C but not A  It is correct for all three conditions.-----------------------------------