Spring08_PracticeExam2

Spring08_PracticeExam2 - CS1316 Spring 2008 Practice Exam 2...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
CS1316 Spring 2008 Practice Exam 2 NAME: ___________________________________ PRISM ID:_______________________________ Part I: Variable Scoping Answer the question s about the following code: What is printed when the following code is run in Dr. Java’s interactions pane? Remember to include all prints and errors. TestReview aReview = new TestReview(5, "cs1316"); aReview.printWord(3); aReview.method2(); public class TestReview { private int speed; public String word = "George"; int numLetters = 0; public TestReview (int speed, String word) { this.speed = speed; word = word; System. out .println("the variable 'word' is: " + word); } public void printWord (int numLetters) { System. out .println("the variable 'word' is: " + word.substring(0, numLetters)); } public void method2 () { int speed = 42; System. out .println("speed = " + speed); System. out .println("this.speed = " + this.speed); System. out .println("numLetters = " + numLetters); } }
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
CS1316 Spring 2008 Practice Exam 2 NAME: ___________________________________ PRISM ID:_______________________________ Part II: Binary Tree The following classes are for your reference: public class BinaryTree { private BSTNode root; public BinaryTree(BSTNode root){this.root = root;} public void add(BSTNode node){add(root, node);} public void add(BSTNode current, BSTNode node){ if (node.getData() < current.getData()){ if (current.getLeft() == null) current.setLeft(node); else add(current.getLeft(), node); } else{ if (current.getRight() == null) current.setRight(node); else add(current.getRight(), node); } } public void inOrder(){inOrder(root);} public void inOrder(BSTNode current){ if(current.getLeft() != null) inOrder(current.getLeft()); System. out .println(current); if(current.getRight() != null) inOrder(current.getRight()); } } public class BSTNode { int data; private BSTNode right; private BSTNode left; public BSTNode(int data){ this.data = data; right = null; left = null; } public int getData() {return data;} public void setData(int data) {this.data = data;} public BSTNode getRight() {return right;} public void setRight(BSTNode right) {this.right = right;} public BSTNode getLeft() {return left;} public void setLeft(BSTNode left) {this.left = left;} public String toString(){return "DATA: " + data;} }
Background image of page 2
CS1316 Spring 2008 Practice Exam 2 NAME: ___________________________________ PRISM ID:_______________________________ Part III: Binary Tree (continued) Insert each integer from following array: [32, 17, 25, 34, 46, 22, 81, 53] in order into a Binary Tree. (Hint: You should start from 32 and work your way right through the array). What is the preorder traversal of this tree?
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page1 / 10

Spring08_PracticeExam2 - CS1316 Spring 2008 Practice Exam 2...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online