DOC PREVIEW
UT CS 307 - CS 307 – Midterm 1

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 Admin Total off Net ScoreCS 307 – Midterm 1 – Spring 2005Your Name____________________________________Your UTEID __________________________________ Your TAs name ________________________________ (Peter or David) Instructions: 1. There are 4 questions on this test. 2. You will have 2 hours to complete the test.3. You may not use a calculator. 4. Please make your answers legible. 5. When code is required, write Java code.6. The class style guide and coding standards are not in effect7. You are not graded on the efficiency of your solutions unless stated.8. When writing code for questions 2 and 3 assume the preconditions of the method are met.9. You may not use any classes' or methods from the Java Standard Library except as noted. You mayuse System.out.println, System.out.print, any classes' equals method, and native arrays.1. (2 points each, 30 points total) Place your answers on the attached sheet. Short answer questions. For code sample state the output. If the code would cause a syntax error answer "syntax error" and if it would cause a runtime error answer "runtime error".A. What is the output of the following code?int x = 4;int y = 3;int z = 2;x = x + z * y;System.out.println( x ); CS 307 – Midterm 1 – Spring 2005 1B. Show the contents of list after this code segment completes.int[] list = {2, 6, 3, 7, 5, 9, 9};int temp1;int temp2;for(int i = 0; i < list.length; i++){ temp1 = (i * i) % list.length;temp2 = list[i];list[i] = list[temp1];list[temp1] = temp2;}C. What is the output of the following code?int[][] mat = new int[2][3];for(int c = 0; c < mat[0].length; c++){ for(int r = 0; r < mat.length; r++){ mat[r][c] = r – c;}}for(int r = 0; r < mat.length; r++){ for(int c = 0; c < mat[0].length; c++){ System.out.print( mat[r][c] + " " );}} CS 307 – Midterm 1 – Spring 2005 2D. Consider the following methods which are all in the same class.public void comm(){ System.out.print("A");}public void graph(int x){ System.out.print("B");for(int i = 0; i < x; i++){ comm();}System.out.print("B");}public void plan(int x){ comm();graph(2);for(int i = 0; i < x; i++){ System.out.print("C");}graph(1);}What is output by the following method call?plan(3);E. What is the output of the following code?public void art(int p, int q){ System.out.print(p + " " + q);p++;q--;System.out.print(" " + p + " " + q);}int p = 3;int q = 4;art(p, q);System.out.print(" " + p + " " + q); CS 307 – Midterm 1 – Spring 2005 3For questions F through L consider the following two classes.public class Superhero{ private String myName;private String myPower;public Superhero(String name, String power){ myName = name;myPower = power;}public void change (String newName){ myName = newName; }public String toString(){ return myName + "'s power is " + myPower; } }public class FlyingSuperhero extends Superhero{ private int iMyMaxSpeed;public FlyingSuperhero(String name, String power, int maxSpeed){ super(name, power);iMyMaxSpeed = maxSpeed;}public void change(int newSpeed){ iMyMaxSpeed = newSpeed; }public String toString(){ return super.toString() + " and can fly"; }}F. What is the output of the following code when method act is called?public void web(Superhero s){ s.change("Nightwing"); }public void act(){ Superhero dg = new Superhero("Robin", "acrobatics");System.out.println( dg.toString() );web(dg);System.out.println( dg.toString() );} CS 307 – Midterm 1 – Spring 2005 4G. What is the output of the following code when method ada is called?public void access(Superhero s){ s = new Superhero( "Matches", "Disguises");System.out.println( s.toString() );}public void ada(){ Superhero bw = new Superhero( "Batman", "Intelligence");access(bw);System.out.println( bw.toString() );}H. What is the output of the following code? // in a class other than Superhero or FlyingSuperheroFlyingSuperhero ke = new FlyingSuperhero( "Superman", "Strength", 2000);ke.myPower = ke.myPower + " heat vision ";System.out.println( ke.toString() );I. What is the output of the following code when method arch is called?public void apl(Superhero s){ System.out.println( s.toString() ); }public void arch(){ FlyingSuperhero d = new FlyingSuperhero("Wonder Woman", "Golden Lasso", 500);apl(d);}J. What is the output of the following code when method cas is called? .public void bed(Superhero s){ s.change("gone");System.out.println( s );}public void cas(){ FlyingSuperhero js = new FlyingSuperhero("Green Lantern", "power ring", 1000);bed(js);} CS 307 – Midterm 1 – Spring 2005 5K. What is the output of the following code?Superhero pp = new Superhero("Spiderman", "Spider powers!");Superhero eb = new Superhero("Spiderman", "Spider powers!");System.out.println( pp == eb );L. What is the output of the following code?FlyingSuperhero dmg = new FlyingSuperhero("Dove", "Agile", 250);dmg.change("Super Strength");System.out.println( dmg.toString() );From the String class. (character indices start at 0.) char charAt(int index) Returns the character at the specified index.String substring(int beginIndex, int endIndex) Returns a new string that is a substring of this string. The substring begins at the specified beginIndex and extends to the character at index endIndex - 1. Thus the length of the substring is endIndex-beginIndex.M. What is the output of the following code?String s = "Starfire";String result = "";for(int i = s.length() - 1; i >=0; i -= 2){ result += s.charAt(i);}System.out.println( result );N. What is the output of the following code?String s2 = "Flash";String result2 = "";int limit = s2.length() / 2;for(int i = 0; i < limit; i++){ result2 += s2.substring( i, s2.length() – i);}System.out.println( result2 ); CS 307 – Midterm 1 – Spring 2005 6O. Consider the following method:public void chi(int[] list, int x){ int y = list[x];}Is it possible that method chi will generate an exception? Explain why or why not. CS 307 – Midterm 1 – Spring 2005 72. Arrays (30 Points) Consider an array of ints that represents measurements of elevation, that is height above sea level, gathered while riding on a road. Each int represents the elevation at a given point. It is assumed that transitions between points are smooth. Thus a road that goes up and down a hill would look like this:0 1 2 3 4 5 6


View Full Document

UT CS 307 - CS 307 – Midterm 1

Documents in this Course
Midterm 2

Midterm 2

15 pages

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 CS 307 – Midterm 1
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 CS 307 – Midterm 1 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 CS 307 – Midterm 1 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?