MT 1 Review Questions 7/6/2000 Courtesy of your TAs 1. Integer anInt=new Integer(1062); Integer someInt=new Integer(1062); Integer otherInt=anInt; String s = "Bob"; String twin= "Bob"; Short x = 1062; Long y = 1062; Given the above statements, which of the following java expressions evaluate to true? anInt==someInt anInt==otherInt otherInt==someInt anInt.equals(someInt) anInt.equals(otherInt) otherInt.equals(otherInt) s==twin s.equals(twin) x==y 2. Given List Alist as follows(all pointers are pointers to list objects, not to their fields): Alist | | | |->|--|--| |--|--| |--|--| |--|--| || | --->|3 | --->|4 | --->|5 |//| ||-|--| |--|--| |--|--| |--|--| | ^ \/ | |--|--| |--|--| / |1 | --->|2 | ---/ |--|--| |--|--| Give a sequence of four or fewer java statements to transform Alist into the following structure: Alist +--------------------------| | | | | V | |->|--|--| |--|--| |--|--| |--||-| || | --->|2 | --->|3 | --->|4 || | ||-|--| |--|--| |--|--| |--|--| | ^ |-------------------------| Public Class List{ public Object Head; public List tail; } 3. Locate and correct the errors in the following segment of code. (The main method should print 90) public class ArrayMKII{ private static final N=32; private Object [] Ary = new Object [N]; public void RightRotateVariable(int N){ int last = this.N-1; Object temp=Ary[last]; for(; N>0; N--){ for(int i = last; i>0; i--){ Ary[i+1]=Ary[i]; } Ary[0]=temp; } } public static void main(String [] args){ mrAry=new ArrayMKII(); for(int i=0;i<=31; i++){ myAry[i]=new Integer(i); } myAry.RightRotateVariable(3); int j=0; for(i=0; i<3; i++){ Integer temp = myAry[i]; j+=temp.intValue(); } System.out.println(j); } } 4. Given the following method declaration: public static void foo(int[][] a) { int[]x= {2, 4, 3, 8, 5, 6}; a[1]=x; a[2][0]=4; a[2][1]=9; a[2][2]=3; a[2][3]=a[1][2]; a[1][3]=a[0][1]; } What are a, b, c, and d after the following code is executed? int[] a = {2, 5, 5, 6, 3}; int[] b = {7, 8, 1, 3, 7}; int[] c = {2, 5, 7, 1, 6}; int[][] d = {a, b, c}; foo(d); 5. Draw the box-and-pointer diagram resulting from the following: List l = new List("1", new list("2", new List("3", new List("4", null)))); List m = l.tail.tail; List n = new List("5", m); n.tail.head="6"; 6.Given the following class definitions: class ListNode { ListNode next; Object item; } class List { ListNode head; } Write a method to go inside the list class that will reverse the order of the listnodes pointed to by head. The function header should look like this: public void Reverse(); 7. Write a recursive function that prints out all bit strings of length n that do not contain consecutive zeros. A Bit String is a string that only contains 0 or 1; 8. You learned about a simple type of list in lecture. Often these lists are encapsulated in a "wrapper" type to make the programming less complicated for the end programmer. Fill in all /* fill in */ comments with your code. Areas you need to change have been marked with a -> to help you find them. Please read all comments in the code as they are important. You do not need to worry about changing the currentNode value in append or prepend. The behavior of these functions is not defined when setToBeginning or setToEnd has not been called after the list has been changed. You may add code only to an arrowed area. No sticking excess code in other methods. public class ListNode { public Object obj; public ListNode next; -> /* fill in with a constructor which takes three arguments: 1. an Object to place in obj 2. a ListNode object to place in next 3. a ListNode object to place in prev */ } public class List { public List() { head = null; tail = null; currentNode = null; } /* append should add a new node containing newObj to the end of this list make sure you deal with any special cases that arise */ public void append(Object newObj) { -> /* fill in */ } /* prepend should add a new node containing newObj to the beginning of this list make sure you deal with any special cases that arise */ public void prepend(Object newObj) { -> /* fill in */ } /* returns the obj in the ListNode that is currently being pointed to THROWS an NullCurrentNodeException if the currentNode is null */ public Object getCurrent() { if (currentNode == null) throw new NullCurrentNodeException(); return currentNode.obj; } /* advances the current ListNode pointed to THROWS an NullCurrentNodeException if the currentNode is null */ public void advanceToNext() { if (currentNode == null) throw new NullCurrentNodeException(); currentNode = currentNode.next; } /* removes the node pointed to by currentNode and returns the value stored in that node THROWS an NullCurrentNodeException if the currentNode is null */ public Object removeCurrent() { -> /* fill in */ } /* resets the current pointed to ListNode to the list head NOTE: THIS MUST BE DONE BEFORE STARTING ANY WALK-THROUGH! MAKE SURE YOUR LIST HAS BEEN INITIALIZED OR IT WILL NOT WORK! */ public void setToBeginning() { currentNode = head; } /* resets the current pointed to ListNode to the end of the list */ public void setToEnd() { currentNode = tail; } private ListNode head; // points to the first node in this list private ListNode tail; // points to the last node in this list private ListNode currentNode; // points to the current node during a walk through } 9. In lecture, the dequeue was briefly touched upon. It is a data structure that supports both the functions of a queue and a stack. Write a dequeue class using the interface below. Use the List class above as your main data structure. Feel free to write any helper functions or helper classes if you would like. You should perform reasonable error checking and throw exceptions when errors occur. You can assume a DequeueEmptyException exists. You must implement all functions specified. You will need to create some instance variables for this class as well. You can assume List, ListNode, and the Dequeue interface are in the same package. interface Dequeue { void push(Object obj); // places a new object onto the top void enqueue(Object obj); // places a new object onto the back Object popTop(); // pops off the top Object popTail(); // pops off the back Object peekTop(); // looks at the item on the top w/o popping Object peekTail(); // looks at the item on the back w/o popping } class MyDequeue implements Dequeue { ... } 10. Sentence Answer Questions: a. Kent Program claims that since interfaces are simply completely abstract classes, they never need to be used in a program. Briefly explain why he's wrong. b. Bobby Joe claims that Java is a compiled language like C/C++. Billy Sue claims it is an interpreted language like Scheme. Who's right? c. void breaker(Integer i, int j) { i = new Integer(10); j = 12; } int x = 7; int y = 9; breaker(new Integer(x), y); The Snuffuluffagus is convinced that after the previous code is executed, that x will be 10 and y will be 9. Is he right? Briefly explain in writing or drawings why. 11. Draw a box and pointer diagram after each of the following lines of code: 1.int x = 1; 2.String str = 1 + "!"; (hint: '1 + "!"' returns a string object) 12.1. How do you allocate a 3d array of type String of size 2x3x4 in Java? Call this array str1. 12.2. Draw a box and pointer diagram of this array 12.3. Suppose I want to augment the array to be of size 3x3x4, how would you do this in Java? (Using the same array of arrays of String objects of the original 2x3x4). The default string for this array is the object noString. Call this array str2. 12.4. Draw the new box and pointer diagram on top of your old one. 13 Given the following code (courtesy of Professor Yelick, w/ modifications by B. Chen): abstract class A { static public void bar() { System.out.println("Calling A.bar"); } abstract public void foo(); } class B extends A { protected int value = 0; public void foo() { System.out.println("Calling B.foo"); } } class C extends B { public void foo() { System.out.println("Calling C.foo, " + value); } } class D extends C { private int value = 1; static public void bar() { System.out.println("Calling D.bar"); } public void foo() { System.out.println("Calling D.foo(), " + value); } } For the following questions, answer, "doesn't compile", "run time error", or "ok" with what the program will print out. 1.A a1 = new A(); a1.foo(); 2.A a2 = new B(); a2.foo(); 3.A a3 = new C(); a3.foo(); a3.bar(); 4. B b4 = new D(); ((C) b4).foo(); 14. Consider the following code segment: public class Foo { public void f1(int x) { x = x + 1; System.out.println("f1's x: " + x); } public void f2(int x) { x = x + 1; f1(x); System.out.println("f2's x: " + x); } public void f3(int x) { f2(x); System.out.println("f3's x: " + x); } } For the following questions, show what the program would print under pass by value and pass by reference. Indicate which print-outs would correspond to a JAVA program. 1.f3(1); 15.1 Is true or false printed in the following block of code given the corresponding user input? // In class MyClass... public static void main (String [] args) { System.out.println (args[0] == args[1]) } % java MyClass Hi! Hi! 15.2 Can a reference type ever be cast into a primitive type? Can a primitive type ever be cast into a reference type? 15.3 In a logical AND operation E1 && E2, where E1 and E2 correspond to valid Java expressions, if E1 evaluates to false, does E2 get evaluated? 15.4 What is printed to the screen? // ... All methods are in the same class definition. static void Increment1 (int x) { x += 1; } static void Increment2 (int [] x) { x[0] += 2; } public static void main (String[] args) { int [] y = {1, 2, 3}; Increment1 (y[0]); Increment2 (y); System.out.println (y[0]); } 15.5 What is printed to the screen? // ... Both methods are in the same class definition. static void Exclamation (String s) { s = s + ``!''; // Note that a single '!' also works here but is // not used to avoid ambiguity. } public static void main (String[] args) { String original = ``Hey''; Exclamation (original); System.out.println (original); } 15.6 What is printed to the screen? // ... Within a method. int x = 0; LabelA: { LabelB: { for (;;) break; x ++; } x ++; } System.out.println (x); 16. Use the simple list class developed in lecture as a starting point: public class List { Object Head; List Tail; int NumberOfNodes; public List (Object Head, List Tail) { this.Head = Head; this.Tail = Tail; NumberOfNodes = 0; } public List (Object Head) { this (Head, null); } } Write an instance method for this list class which will break the list into two at a specified node. The original reference to the list will be left intact and will refer to the first segment of the separated list. The method will return a reference to the second segment of the separated list. The node at which the list will be broken will be specified by an integer argument to the method. Nodes will be numbered beginning at 1 for the first node. The second segment of the separated list will include the node at which the list was broken. For example, if the method is called, and a 3 is passed to it, the first two nodes will remain as the first list. The third and all remaining nodes will become a second list. Use exception handling to deal with cases where the integer argument to the method is out of bounds. Assume the NumberOfNodes variable can be used to determine the length of the list.