{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

sp09M1 - Points off 1 2 3 4 Total off Net Score CS 307...

Info icon This preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon
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);
Image of page 1

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

View Full Document Right Arrow Icon
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 );
Image of page 2
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; } }
Image of page 3

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

View Full Document Right Arrow Icon
CS 307 – Midterm 1 – Spring 2009 4 G. State if the following declarations are valid or invalid (meaning they cause a syntax error).
Image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern