DOC PREVIEW
UT CS 307 - Midterm 1

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

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 16 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 16 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 16 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 16 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 16 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 16 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 2004Your Name____________________________________Your UTEID __________________________________ Your USL's Name________________________________ 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 solutions8. 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) Short answer questions. For code sample state the output. If the output would cause a syntax error answer "syntax error" and if it would cause a runtime error answer "runtime error". In the case of an error explain what caused the syntax error or runtime error.A.What is the output of the following code?int x = 4;int y = 7;int z = 3;int zz = x + y * z;System.out.println(zz);____________________________________________ CS 307 – Midterm 1 – Spring 2004 1B.What is the output of the following code?int x = 5;double a = 2.0;double b = x / a;System.out.println(b);_________________________________________________C.What is the output of the following code?int[] list = {0, 1, 2, 3, 5, 7, 9, 11, 13};System.out.println( list.length );_________________________________________________D.What is the output of the following code?int total = 0;for(int i = 0; i < 10; i++)for(int j = 1; j < 10; j = j * 2)total++;System.out.println( total );_________________________________________________ CS 307 – Midterm 1 – Spring 2004 2E.What is the output of the following code?From the String class. (character indices start at 0.) charcharAt(int index) Returns the character at the specified index.String s = "duct tape";String result = "";for(int i = 2; i < s.length(); i = i + 3)result += s.charAt(i);System.out.println(result);_________________________________________________F. What is the output of the following code when method guy is called?public void noir(int x, int y){ int z = x;x = y;y = z;}public void guy(){ int a = 12;int b = 39;System.out.println( a + " " + b );noir(a, b);System.out.println(a + " " + b );}_________________________________________________ CS 307 – Midterm 1 – Spring 2004 3For questions G through K consider the following class:public class Face{ private String myEyeColor;private String myHairColor;public Face(){ myEyeColor = "Brown";myHairColor = "Brown";}public void changeHair(String hair){ myHairColor = hair; }public void changeEyes(String eyes){ myEyeColor = eyes; }public String toString(){ return myEyeColor + " " + myHairColor; }}G. What is the output of the following code when method lake is called?public void wobegone(Face f){ f.changeHair("blue");f.changeEyes("blue");}public void lake(){ Face f1 = new Face();System.out.println( f1.toString() );wobegone(f1);System.out.println( f1.toString() );}_________________________________________________ CS 307 – Midterm 1 – Spring 2004 4H. What is the output of the following code when method powderMilk is called?public void biscuits(Face f){ f = new Face();f.changeHair("blue");f.changeEyes("blue");}public void powderMilk(){ Face f1 = new Face();System.out.println( f1.toString() );biscuits(f1);System.out.println( f1.toString() );}_________________________________________________ I. What is the output of the following code?Face f1 = new Face();Face f2 = new Face();f1.changeHair("black");f1.changeEyes("green");f2.changeHair("black");f2.changeEyes("green");System.out.println( f1 == f2 ); _________________________________________________ CS 307 – Midterm 1 – Spring 2004 5J. What is the output of the following code . (The code appears in a class other than Face. )Face f1 = new Face();Face f2 = new Face();f1.changeEyes("grey");f2.myHairColor = "blonde";System.out.println( f1.toString() );_________________________________________________ K. What is the output of the following code?Face f1 = new Face();Face f2 = new Face();f1.changeEyes("grey");f2.changeHair("red");f2 = f1;f1.changeHair("grey");System.out.println( f1.toString() );System.out.println( f2.toString() );_________________________________________________ For questions L, M, and N consider the following classes:public abstract class Shape3D{ public abstract int surfaceArea();public abstract int volume();public String toString(){ return "3D Shape"; }}public class Cube extends Shape3D{ private int iMySide;public Cube(int side){ iMySide = side; }public int surfaceArea(){ return iMySide * iMySide* 6; }public int volume(){ return iMySide * iMySide * iMySide; }} CS 307 – Midterm 1 – Spring 2004 6L. What is the output of the following code:Cube c1 = new Cube(2);System.out.println( c1.surfaceArea() + " " + c1.toString() );_________________________________________________ M. What is the output of the following code:Shape3D s1 = new Cube(1);System.out.println( s1.surfaceArea());System.out.println( s1.volume() );System.out.println( s1.toString() );_________________________________________________ N. Assume the header for the Cube class is changed to this:public class Cube extends Shape3D implements ComparableNo other changes are made to the class Cube. What is the output of the following code:Cube c2 = new Cube(3);System.out.println( c2.volume() );_________________________________________________ O. What is the output of the following code:int[][] mat = new int[5][3];for(int r = 0; r < mat.length; r++)for(int c = 0; c < mat[r].length; c++)mat[r][c] = (r * 2) + c;System.out.println( mat[3][2] + " " + mat[2][0]);_________________________________________________ CS 307 – Midterm 1 – Spring 2004 72. Arrays (25 Points) In this question you will implement another method in the Statistics class. Recall that a Statistics object tracks the outcomes of events when the outcomes are always integers greater than or equal to 0. The portions of the Statistics class that you can use is as follows:public class Statistics{ private int[] myData;public int numEvents()// returns the number of events


View Full Document

UT 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

Load more
Download 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 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 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?