Change Contents of the Bubble
View this PageEdit this PageUploads to this PageHistory of this PageHomeRecent ChangesSearchHelp Guide

Practice Exam 2-Spring 2008

Part I: Variable Scoping
Answer the questions about the following code:
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);
  }
}

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



Questions-PracticeExam2-VariableScoping-Spring08

Part II: Binary Tree
The following classes are for your reference:
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;}
}

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


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?



Write a recursive method inside of the BinaryTree class that will print out a preorder traversal of the Binary Tree.








Questions-PracticeExam2-BinaryTree-Spring08

Part III: Short Answer
Answer the following question in clear and concise sentences.

1. What is a discrete event simulation?




2. What is a continuous simulation?




3. FlightSim is an interface class and JumboJet is a new class that uses the FlightSim interface. Using the space below, write the class header for JumboJet that states this relationship.




4. What are abstract methods? What must you do with them when extending an abstract class?




5. What the difference between a stack and a queue? What are their respective insertion and removal methods?




6. What is a token and what is a delimiter?




Questions-PracticeExam2-ShortAnswer-Spring08

Part IV: Polymorphism
Joel's Three Questions
Consider the following code and answer the questions.
public abstract class Cereal{
  protected int calories;
  public Cereal(int calories){ this.calories = calories; }
  public abstract void floatOrSink();
  public void soakMilk(){ System.out.println("Slop, Slop."); }
  public void nutritionFacts(){ System.out.println("I have " + calories + " calories per serving"); }
}

public class GeneralMills extends Cereal{
  public GeneralMills(int calories){ super(calories); }
  public void saySlogan(){ System.out.println("Box Tops for Education."); }
  public void floatOrSink(){ System.out.println("Maybe?"); }
  public void soakMilk(){
    super.soakMilk();
    System.out.println("Glug, Glug.");
  }
}

public class Kellogg extends Cereal{
  public Kellogg(int calories){ super(calories); }
  public void floatOrSink(){ System.out.println("Maybe?"); }
  public void saySlogan(){ System.out.println("Only the best."); }
}

public class Cheerios extends GeneralMills{
  public Cheerios(){ super(80); }
  public void saySlogan(){ System.out.println("Uh-oh, Cheerios!"); } 
}

public class LuckyCharms extends GeneralMills{
  public LuckyCharms(){
    super(110);
    saySlogan();
  }
  public void saySlogan(){ System.out.println("They're Always After Me Lucky Charms!"); }
  public void floatOrSink(){ System.out.println("Me marshmellows float."); }
  public void soakMilk(){
    super.soakMilk();
    System.out.println("Not good, the Irish are lactose intolerant.");
  }
}

public class FrostedFlakes extends Kellogg{
  public FrostedFlakes(){ super(100); }
  public void saySlogan(){ System.out.println("They're great!"); }
  public  void floatOrSink(){ System.out.println("I sink"); }
  public void soakMilk(){
    super.soakMilk();
    System.out.println("Mmm..does a body good.");
  }
}
 
public class RiceKrispies extends Kellogg{
  public RiceKrispies(){ super(90); }
  public void saySlogan(){ System.out.println("What do your Rice Krispies say to you?"); }
  public void soakMilk(){
    super.soakMilk();
    System.out.println("AHHHHHHHHHH, we are drowning!!");
  }
  public void floatOrSink(){ System.out.println("Goodness, this exam is hard."); }
  public void speak(){ System.out.println("Snap, Crackle, Pop"); }
}


1. Which of the following objects are valid?

a. GeneralMills myCereal = new GeneralMills();
b. Cereal myCereal = new Cereal();
c. Kellogg myCereal = new Kellogg(200);
d. LuckyCharms myCereal = new LuckyCharms();
e. FrostedFlakes myCereal = new FrostedFlakes(100);
f. Cereal myCereal = new RiceKrispies();
g. GeneralMills myCereal = new Cheerios();
h. GeneralMills myCereal = new FrostedFlakes();
i. LuckyCharms myCereal = new RiceKrispies();


2. Assume we have initialized an object, RiceKrispies myCereal = new RiceKrispies(); Will the following statements compile and what will get executed when they are called?

a. myCereal.saySlogan();



b. myCereal.soakMilk();



c. myCereal.floatOrSink();



d. myCereal.speak();



e. myCereal.nutritionFacts();



f. ((RiceKrispies) myCereal).speak();



g. ((Kellogg) myCereal).saySlogan();




h. ((LuckyCharms) myCereal).saySlogan();




3. Assume we have initialized an object, Cereal myCereal = new RiceKrispies(); Will the following statements compile and what will get executed when they are called?

a. myCereal.saySlogan();



b. myCereal.soakMilk();



c. myCereal.floatOrSink();



d. myCereal.speak();



e. myCereal.nutritionFacts();



f. ((RiceKrispies) myCereal).speak();



g. ((Kellogg) myCereal).saySlogan();



h. ((LuckyCharms) myCereal).saySlogan();





Questions-PracticeExam2-Polymorphism-Spring08

Part V: Random generators
Using Math.random() or the Random class, construct a method to solve the following problem.
public class Student {
	private String name;
	private int grade;
	
	public Student(String name){this(name, -1);}
	public Student(String name, int grade){
		this.name = name;
		this.grade = grade;}
	
	public String getName() {return name;}
	public void setName(String name) {this.name = name;}
	public int getGrade() {return grade;}
	public void setGrade(int grade) {this.grade = grade;}
	public String toString(){return "Name: " + name + " Grade: " + grade;}
}

public class StudentNode {
	private Student data;
	private StudentNode next;
	
	public StudentNode(Student data){this(data, null);}
	public StudentNode(Student data, StudentNode next) {
		this.data = data;
		this.next = next;}

	public Student getData() {return data;}
	public void setData(Student data) {this.data = data;}
	public StudentNode getNext() {return next;}
	public void setNext(StudentNode next) {this.next = next;}
	public String toString(){return data.toString();}
}

import java.util.Random;
public class StudentREMOVEDst {
	private Random gen = new Random();
	private StudentNode head;
	public StudentREMOVEDst(StudentNode head) {this.head = head;}
	public StudentNode getHead() {return head;}
	public void setHead(StudentNode head) {this.head = head;}
	
	public void add(StudentNode student){
		if (size() == 0) 	head = student;
		else	last().setNext(student);}
	
	public StudentNode last(){
		if (size() == 0)	return null;
		StudentNode current = head;
		while (current.getNext() != null)
			current = current.getNext();
		return current;}
	
	public int size(){
		int size = 0;
		StudentNode current = head;
		while (current != null){
			size++;
			current = current.getNext();}
		return size;}
	
	public String toString(){
		String ret = "";
		StudentNode current = head;
		while (current != null){
			ret = ret + current.toString() + "\n";
			current = current.getNext();
		}
		return ret;}
}


Brian is assigning grades to his students in his 1316 class. He has decided to construct a linked list of students StudentREMOVEDst to make grading assignments easier. Using a StudentREMOVEDst he wants to:
- randomly pick half of his students to assign a score of 100 (if the size is odd, round down). You cannot simply assign the first half or the second half to receive 100, because it’s too suspicious.
Write this method for Brian. Remember not to randomly pick the same student twice (two students may have the same name)!

public void randomlyAssignGrades()


















Questions-PracticeExam2-RandomGenerators-Spring08

Part VI: State Diagrams
State transition diagrams are a common way to represent the logic required to control systems. They consist of the following components (fill in the blanks from the word list below)

1. State normally represented by a(n) [ANSWER HERE] .

2. Transition normally represented by a(n) [ANSWER HERE] connecting [ANSWER HERE] s.

3. Transitions should be labeled with the [ANSWER HERE] that [ANSWER HERE] (s) the transition

Word list: class, state, trigger, object, prevent, circle, arrow or event.

Consider a simple traffic light system for a side road connecting into a main highway. The lights should be green for the main highway until a sensor on the side road indicates a car is there. Then, the main lights should begin cycling from orange to red 2 seconds for each shift, followed by a 5 sec delay before the side road shifts to red/orange then green. After 20 sec, the reverse cycle takes place turning the main lights back to green with the same timing. The system should prevent side road traffic from again requesting service for 2 minutes.

In the space below, draw the state transition diagram for this control system. Label the states and transitions.

















Student1068

UML Diagrams [Spring08_PracticeExam2_UML.pdf]

Questions-PracticeExam2-UML-Spring08

Link to this Page