CS 307 – Midterm 1 – Spring 2009 1 Points off 1 2 3 4 Total off Net Score CS 307 – Midterm 1 – Spring 2009 Your Name____________________________________ Your UTEID __________________________________ Circle yours TA’s name: Todd Guhan Xiuming(aka David) Instructions: 1. Please turn off or silence your cell phones. 2. There are 4 questions on this test. 3. You will 2 hours to complete the test. 4. You may not use a calculator or any other electronic devices while taking this test. 5. When code is required, write Java code. 6. When writing a method, assume the preconditions of the method are met. 7. When writing a method you may add helper methods if you wish. 8. When you complete the test show the proctor your UTID and give them the test and any scratch paper. Please leave the room quietly. 1. (2 points each, 30 points total) Short answer questions. Place your answers on the attached answer sheet. For code sample state the output. If the code would cause a syntax error answer "syntax error". If it would cause an exception answer "exception". If it would result in an infinite loop answer "infinite loop". A. What is the output of the following client code? public int methodA(int x, int y){ x++; y--; return x * y; } // client code int c = 4; int d = 5; System.out.println( methodA(c, d) + " " + d ); B. What is output by the following code? int[] listOne = {3, 2}; int[] listTwo = {2, 2}; listTwo[0]++; System.out.println( listOne == listTwo);CS 307 – Midterm 1 – Spring 2009 2 C. What is the output of the following client code? public void methodC(int[] list){ list[0]++; list = new int[3]; } // client code int[] data = {2, 1}; methodC(data); System.out.println( Arrays.toString(data) ); // prints out elements D. What is the output of the following client code? It uses method_c from part C. // client code int[] data2 = new int[0]; methodC(data2); System.out.println( Arrays.toString(data2) ); // prints out elements E. Consider the following method header: // pre: str != null public boolean allVowels(String str){ What were the two ways presented in class for checking the preconditions of methods such as str != null? F. What is output by the following code? int x = 4; int y = 9; int[][] table = new int[x * x][y / x]; System.out.println( table[3].length );CS 307 – Midterm 1 – Spring 2009 3 For questions G – O consider the following classes and interfaces. public interface Movable{ public int getMoveDistance(); } public class GroundUnit{ private String name; private int value; public GroundUnit(){ this("gen", 10); } public GroundUnit(String n, int v){ name = n; value = v; } public void upgrade(){ name = name + "I"; value += 10; } public String toString(){ return name + " " + value; } public int getNum(){ return value; } } public class Tank extends GroundUnit implements Movable{ private int moveValue; public Tank(String name, int value, int move){ super(name, value); moveValue = move; } public int getMoveDistance(){ return moveValue; } public void upgrade(){ super.upgrade(); moveValue += 2; } public int getNum(){ return moveValue; } }CS 307 – Midterm 1 – Spring 2009 4 G. State if the following declarations are valid or invalid (meaning they cause a syntax error). 1 point each Movable m1 = new GroundUnit(); // 1 Movable m2 = new Movable(); // 2 H. State if the following declarations are valid or invalid (meaning they cause a syntax error). 1 point each Movable m3 = new Tank("Max", 10, 5); // 1 Tank t1 = new GroundUnit("Van", 10); // 2 I. State if the following declarations are valid or invalid (meaning they cause a syntax error). 1 point each Object obj = new Tank("Rex", 5, 5); // 1 Movable m4 = new GroundUnit("Net", 50); // 2 J. What is the output of the following code? GroundUnit g1 = new GroundUnit(); g1.upgrade(); System.out.println( g1 ); K. What is the output of the following code? Tank t2 = new Tank("Max", 5, 10); t2.upgrade(); System.out.println( t2 ); L. Briefly explain why the following code does not compile. Tank t3 = new Tank(); System.out.println( t3 + " " + t3.getMoveDistance() ); M. What is the output of the following code? GroundUnit g2 = new Tank("Snoopy", 10, 4); System.out.println( g2.getNum() );CS 307 – Midterm 1 – Spring 2009 5 N. What is the output of the following code when method n2 is called? public void n1(GroundUnit g){ g.upgrade(); g.upgrade(); } public void n2(){ GroundUnit gVar = new GroundUnit("Tex", 5); n1(gVar); System.out.println(gVar); } O. Briefly explain why the following code does not compile. Movable m5 = new Tank("Star", 2, 4); m5.upgrade();CS 307 – Midterm 1 – Spring 2009 6 2. Implementing classes. (20 Points) Create a class to model integers of an arbitrary magnitude. The Java int data type is limited to the range -231 to 231 - 1, approximately -2 billion to positive 2 billion. The Java long data type is limited to -263 to 263 - 1, approximately -9.2 x -1018 to 9.2 x -1018. In this question you will implement part of a class to model arbitrarily large integers similar to the Java BigInteger class. • Declare a LargeInt class. • The class will use a native array of ints to store the digits of an arbitrarily large decimal integer. • The class will store all digits as positive values between 0 and 9 inclusive. • Much like the IntList class we developed in lecture the LargeInt class may have extra capacity. When constructing a LargeInt include 5 extra elements in the array of ints that stores the digits. Include a private class constant to store this value. • Because the internal array may have extra capacity it is necessary to track the number of digits of the LargeInt. • The class must have a way of storing whether the LargeInt is positive or negative. • The integer 0 is a special case. For this question we will assume 0 is a positive number. When creating the array to store zero there will be 6 elements all equal to 0. • Create the class so that the least significant digit (the ones place) is stored in element 0 of the internal array. Thus the number -1735 would be stored as follows: 5 3 7 1 0 0 0 0 0 isPositive: false numDigits: 4 • The constructor for the LargeInt class will have one parameter, a
View Full Document