DOC PREVIEW
UT CS 307 - CS 307 Midterm 1

This preview shows page 1-2-19-20 out of 20 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 20 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 20 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 20 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 20 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 20 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Points off 1 2 3 4 Total off Net ScoreCS 307 – Midterm 1 – Spring 2007Your Name____________________________________Your UTEID __________________________________ Circle yours TA’s name: Alison Aparajit Vineet Instructions: 1. Please turn off your cell phones2. 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. Please make your answers legible. 6. When code is required, write Java code.7. When writing a method, assume the preconditions of the method are met.8. When writing a method you may add helper methods if you wish.9. 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 error answer "exception". If it would result in an infinite loop answer "infinite loop". A. What is the output of the following code?int x = 5;int y = 3;int z = x / y + x * y;System.out.println( z );B. What is the output of the following code?int x = 128;int y = 0;while( x > 3 ){ x /= 2; y++;}System.out.println( y + "_" + x ); CS 307 – Midterm 1 – Spring 2007 1C. What is the output of the following code?int[] data = {5, 2, 0, 4, 3, 1};int tgt = 0;for(int i = 0; i < 4; i++) tgt = data[tgt];System.out.println(tgt);D. What is the output of the following code?final int SIZE = 4;char[][] letters = new char[SIZE][SIZE];String alpha = "abcdefghijklmnopqrstuvwxyz";int in = alpha.length() - 1;for(int c = SIZE - 1; c >= 0; c--){ for(int r = SIZE - 1; r >= 0; r--){ letters[r][c] = alpha.charAt(in); in--; }}for(int i = 0; i < SIZE; i++) System.out.print( letters[i][i] );E. When method thorndale is called the output is 2323. Briefly explain why.public void roundTop(int x, int y){ x = x * 2; y = y + 2;}public void thorndale(){ int x = 2; int y = 3; System.out.print( "" + x + y ); roundTop(x, y); System.out.print( "" + x + y );} CS 307 – Midterm 1 – Spring 2007 2F. What is the output when method industry is called?public int carmine(int[] list){ int total = 0; try{ for(int i = 0; i < 10; i++) total = list[i]; } catch(NullPointerException e){ total = -1; } catch(ArrayIndexOutOfBoundsException e){ total = -2; } return total;}public void industry(){ int[] data = {2, -3, 4, -5, 6, -5}; System.out.println( carmine(data) );}G. Is the code in method carmine in question F a proper use of a try - catch block? Briefly explain why or why not. CS 307 – Midterm 1 – Spring 2007 3For questions H – N consider the following classes and interfaces. public interface Vehicle{public boolean humanPowered(); public void show();}public abstract class Bike implements Vehicle{ private String brand; public Bike(String b){ brand = b; } public void show(){ System.out.print("Bike"); } public String toString(){ return "engine: " + !humanPowered(); }}public class MountainBike extends Bike{ private int front; private int back; public MountainBike(int f, int b, String br){ super(br); front = f; back = b; } public boolean humanPowered(){ return true; } public void show(){ System.out.println("Mountain Bike"); } public int gears(){ return front * back; } public void inc(){ back++; }} CS 307 – Midterm 1 – Spring 2007 4H. The following declaration is not allowed. Briefly explain why it is not allowed. Bike b = new Bike("Trek");I. What is the output of the following code? MountainBike m1 = new MountainBike(2, 8, "Giant");MountainBike m2 = new MountainBike(2, 8, "Giant");System.out.println( m1 != m2 );J. What is the output of the following code? Bike b = new MountainBike(2, 7, "Fisher");System.out.println( b.toString() );b.show();K. What is the output of the following code when method shelby is called?public void thrall(MountainBike mb1, MountainBike mb2){mb1.inc();mb2.inc(); mb1 = new MountainBike(3, 10, "Trek");}public void shelby(){MountainBike m = new MountainBike(2, 8, "Giant"); thrall(m, m); System.out.println( m.gears() );}L. List all the data types of object variables that may refer to objects of type MountainBike.M. Do objects of type MountainBike have an equals method that returns a boolean? Brieflyexplain why or why not. CS 307 – Midterm 1 – Spring 2007 5N. Briefly explain why the following code will not compile.Bike b = new MountainBike(3, 10, "Raleigh");b.show();System.out.println( b.gears() );O. What is the output of the following code when method coupland is called?public int taylor(int x, int y){ x++; y--; return x * y;}public int taylor(int z){ int a = z - 2; z++; return taylor(a, z);}public void coupland(){ int x = taylor(2, 3); int y = taylor(2); System.out.println( x + " " + y );} CS 307 – Midterm 1 – Spring 2007 62. Arrays (25 Points) This question involves a class that represents playing cards named Card and a class that represents a group of playing cards named Hand. Consider the public part of the Card class.public class Card{ public static final int CLUBS = 0; public static final int DIAMONDS = 1; public static final int HEARTS = 2; public static final int SPADES = 3; /** * get this Card's suit. * <br>pre: none<br> * post: returns this Card's suit * @return this Card's suit */ public int getSuit() //other methods not shown}You will be completing a method in the Hand class. This method returns true if the calling Hand has the same number cards of each suit as another Hand object that is sent as a parameter. The Hand classuses a native array of Card objects to store the cards in the hand. For example assume c = clubs, d = diamonds, h = hearts, and spades = s. Assume numbers refer to card ranks. 2 for two, 3 for three and so forth. If one hands has the following cards: (2c, 3c, 2d, 5s) and the other hand has (2s, 5c, 6c, 4d) the method would return true. Each hand has 2 clubs, 1 diamond, and 1 spade.


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?