DOC PREVIEW
UT CS 307 - Midterm 2

This preview shows page 1-2-3-4-5 out of 15 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 15 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 15 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 15 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 15 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 15 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 15 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Points off 1 2 3 4 5 Admin Total off Net ScoreCS 307 – Midterm 2 – Fall 2001Name____________________________________Last 4 digits of SSN / Student ID ______________Class Unique ID ___________________________Instructions: 1. There are 5 questions on this test. Each is worth 20 points.2. You will have 2 hours to complete the test.3. You may not use a calculator. 4. When code is required, write Java code. 5. You may not use any methods from the Java Standard Library other than the ones specifically mentioned on each question.6. The style guide is not in effect.7. Please make your answers legible. 1. Java mechanics and short answer questions. Write the answer to each question in the space provided. If code results in an error indicate if it is a compile error or runtime error.For questions A through C consider the following classes:public class Vehicle{ private int iMyWheels;public Vehicle(int w){ iMyWheels = w; }public void setWheels(int w){ iMyWheels = w; }public int getNumWheels(){ return iMyWheels; }public String toString(){ return "wheels: " + iMyWheels; }}CS 307 – Midterm 1 – Fall 2001 1public class Truck extends Vehicle{ private boolean bMyAllWheelDrive;public Truck(int w, boolean d){ super(w);bMyAllWheelDrive = d;}public void setWheels(int w){ if( w < 6 )super.setWheels(w);elsebMyAllWheelDrive = false;}public String toString(){ String result = super.toString();if( bMyAllWheelDrive )result += ", " + getNumWheels() + " wheel drive";elseresult += ", " + 2 + " wheel drive";return result;}}A. Consider the following code which appears in a class other than Vehicle and Truck:Truck t = new Truck(4, true);System.out.println( t.getNumWheels() );What is the output of the this of code?____________________________________________________B. Consider the following code which appears in a class other than Vehicle and Truck:Truck t1 = new Truck(6, true);t1.setWheels( 8 );System.out.println( t1.toString() );What is the output of the this of code?____________________________________________________CS 307 – Midterm 1 – Fall 2001 2C. Consider the following code which appears in a class other than Vehicle and Truck:Vehicle[] v = new Vehicle[2];v[0] = new Vehicle(4);v[1] = new Truck(6, true);System.out.println( v[0].toString() + " " + v[1].toString() );What is the output of the this of code?____________________________________________________D. Consider the following method:public int wand(int n){ if( n == 0)return 2;elsereturn 1 + wand(n/2) + wand(n/3);}What is the output of the following statement which appears in the same class as wand?System.out.println( wand(6) );____________________________________________________CS 307 – Midterm 1 – Fall 2001 3E. Consider the following method:public int stone(int n){ if( n == 0)return 0;elsereturn n * n + stone( n – 1 );}What is the output of the following statement which appears in the same class as stone?System.out.println( stone(5) );____________________________________________________F. Consider the following method:public int cloak(int n){ if( n == 0)return 0;elsereturn 2 + cloak( n – 3 );}What is the output of the following statement which appears in the same class as cloak?System.out.println( cloak(10) );____________________________________________________G. What is the sum of the integers from 1 to 5000?____________________________________________________H. What is the Big O number of executions necessary to insert an item into an arbitrary positionin an array of N items while maintaining the relative order of the original items?____________________________________________________CS 307 – Midterm 1 – Fall 2001 4I. Consider the following code:for(int i = 0; i < N * 2; i++)for(int j = N - 10; j > 0; j = j / 2){ … }What is the Big O number of executions of this code in terms of N?____________________________________________________J. Consider a LinkedList class with the following methods:public LinkedList()public void addFront(int x)public void addBack(int x)public void removefront()public void removeBack()public int getFront()public int getBack()This linked list is designed to store integers. What is the output of the following code?LinkedList ls = new LinkedList();ls.addFront(10);ls.addBack(12);ls.addFront(7);ls.addBack(2);ls.addFront(12);ls.removeBack();ls.addBack( ls.getFront() * 2 );ls.removeFront();ls.removeFront();ls.removeFront();ls.removeFront();ls.addBack( ls.getFront() );System.out.println( ls.getFront() );____________________________________________________CS 307 – Midterm 1 – Fall 2001 52. An area on the earth's surface can be modeled with a matrix of integers. Each element of the matrix represents a small area. Many types of information could be stored about the earth's surface, but in this problem only the elevation of the area is modeled and stored. An integer value represents the elevation of the area. Here is an example: 0 1 2 3 4 5 6 7 8 90 1 1 2 2 3 4 2 1 1 11 2 2 3 2 2 1 1 1 1 12 4 4 3 2 2 2 1 1 1 13 4 4 3 1 1 2 2 2 1 14 5 4 3 1 1 2 2 2 2 35 5 4 3 3 3 3 2 2 2 36 6 4 4 4 4 4 1 1 1 3 The flow of water in the area may also be modeled by looking at the various elevations. Assume a drop of water is placed in a cell. In our model water can flow to a neighboring cell if that neighbor's elevation is less than or equal to the elevation of the current cell. Also assume water can only flow to cells north, south, east, and west, not diagonally. Here is an interesting question: can water from agiven cell flow out of the area? In other words can it reach one of the cells on the border or edge of the matrix. Obviously any cells on the border drain. Look at another cell, 3, 5. The elevation there is2. Water can flow off the matrix from this cell via many possible paths, one of which is (2,5), (1,5), (1,6), (1,7), (0,7) then off the matrix. Note water in cell 3,4 cannot flow off the board, it is part of a depression or low area surrounding by cells of higher elevation.Write a method to determine if water from a given cell can drain from the area represented by a matrix of integers. Water can flow to cells to the north, south, east, or west with the same or lower elevation. Once the edge of the matrix is reached (any of the cells in the first or last row, or first or last column) water is assumed to have successfully drained./* pre: world != null, the matrix is rectangular meaning each row has the same number of


View Full Document

UT CS 307 - Midterm 2

Documents in this Course
Midterm 1

Midterm 1

15 pages

Syllabus

Syllabus

24 pages

s

s

8 pages

Midterm 1

Midterm 1

14 pages

Midterm 2

Midterm 2

14 pages

Recursion

Recursion

14 pages

Midterm 1

Midterm 1

16 pages

Load more
Download Midterm 2
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Midterm 2 and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Midterm 2 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?