Unformatted text preview:

Points off 1 2 3 4 Admin Total off Net ScoreCS 307 – Midterm 1 – Fall 2003Your 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. 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 methods, and native arrays.1. (3 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[] list = {16, 9, 4, 1};int[] otherList = new int[list[1]];for(int i = 0; i < otherList.length; i++)otherList[i] = list[3] + i * i;System.out.println( otherList[0] + " " + otherList[5] + " " + otherList[otherList.length-1]);____________________________________________CS 307 – Midterm 1 – Fall 2003 1B.What is the output of the following code?int[][] mat = new int[5][4];for(int c = 2; c < mat[0].length; c++)for(int r = 1; r < 4; r++)mat[r][c] = r * c;int total = 0;for(int c = 0; c < mat.length; c++)total += mat[c][c];System.out.println( total );_________________________________________________C.Consider the following method:public static void hershey(int a, int b){ int x = a * 2;int y = b * 3;System.out.println(a + " " + b + " " + x + " " + y);b--;a++;System.out.println(a + " " + b + " " + x + " " + y);}What is the output of the following code?int x = 5;int y = 3;System.out.println( x + " " + y );hershey(x, y);System.out.println( x + " " + y );_________________________________________________CS 307 – Midterm 1 – Fall 2003 2For questions D – J Consider the following classes:public abstract class Candy{ private String myName;private int iMyCalories;public Candy(){ myName = "unknown";iMyCalories = 0;}public Candy(String name){ myName = name;iMyCalories = 0;}public Candy(String name, int calories){ myName = name;iMyCalories = calories;}public String toString(){ return myName + " " + iMyCalories; }public void setCalories(int calories){ iMyCalories = calories; }public int getCalories(){ return iMyCalories ; }public abstract String getDescriptor();}public class ChocolatyCandy extends Candy{ private String myAdj;public ChocolatyCandy(){ myAdj = "Chocolaty"; }public ChocolatyCandy(String name, String adj, int calories){ super(name, calories);myAdj = adj;}public String toString(){ return super.toString() + " " + myAdj; }public String getDescriptor(){ return myAdj; }}CS 307 – Midterm 1 – Fall 2003 3D. Consider the following methodpublic static void setTrueCal(ChocolatyCandy c){ System.out.println( c.toString() );c.setCalories( c.getCalories() * 3 );System.out.println( c.toString() );}What is the output of the following code? (setTrueCal and the following code appear in a class other than Candy and ChocolatyCandy. )ChocolatyCandy c2 = new ChocolatyCandy("Kit-Kat", "Crunchy", 200);System.out.println( c2.toString() );setTrueCal(c2);System.out.println( c2.toString() );_________________________________________________E. Consider the following method:public static void incrementCal(Candy c, int delta){ System.out.println( c.toString() );c = new ChocolatyCandy("Snickers", "with Peanuts", c.getCalories() + delta );System.out.println( c.toString() );}What is the output of the following code? (incrementCal and the following code appear in a class other than Candy and ChocolatyCandy. )ChocolatyCandy c3 = new ChocolatyCandy("Snickers", "with Peanuts", 300);System.out.println( c3.toString() );incrementCal(c3, 200);System.out.println( c3.toString() );_________________________________________________CS 307 – Midterm 1 – Fall 2003 4F. What is the output of the following code?ChocolatyCandy c4 = new ChocolatyCandy("Snickers", "with Peanuts", 300);ChocolatyCandy c5 = new ChocolatyCandy("Snickers", "with Peanuts", 300);System.out.println( c4 == c5 );c4 = c5;c5.setCalories(500);c4.setCalories(200);System.out.println( c4 == c5 );System.out.println( c4.toString() );System.out.println( c5.toString() );_________________________________________________G. What is the output of the following code?ChocolatyCandy[] cList = new ChocolatyCandy[3];for(int i = 0; i < cList.length; i++)System.out.println( cList[i].toString() );_________________________________________________ CS 307 – Midterm 1 – Fall 2003 5H. Consider the following classpublic class SugaryCandy extends Candy{ private int iMySugarPercentage;public SugaryCandy(String name, int sugar, int calories){ super(name, calories);iMySugarPercentage = sugar;}public String toString(){ return super.toString() + " Sugar % " + iMySugarPercentage;}}Explain why this class will not compile and what the implication of allowing the class to compile would be._________________________________________________ I. Explain how Polymorphism expands what a valid data type is for an object variable. _________________________________________________ CS 307 – Midterm 1 – Fall 2003 6J. What is the output of the following code . (The code appears in a class other than Candy or ChocolatyCandy. )ChocolatyCandy c6 = new ChocolatyCandy("100 Grand Bar", "Chewy", 1000);ChocolatyCandy c7 = new ChocolatyCandy("Milky Way", "Yummy", 1000);System.out.println( c6.toString() + " " + c6.iMyCalories + " " + c7.equals(c6) );_________________________________________________ CS 307 – Midterm 1 – Fall 2003 72. Arrays(30 Points) This question uses a Card class.public class Card { public static final int TWO = 0; public static final int THREE = 1; public static final int FOUR = 2; public static final int FIVE = 3; public static final int SIX = 4; public static final int SEVEN = 5; public static final int EIGHT = 6; public static final int NINE = 7; public static final int TEN = 8; public static final int JACK = 9; public static final int QUEEN = 10; public static final int KING = 11; public static final int ACE = 12; public static final int CLUBS = 0; public static final int DIAMONDS = 1; public static


View Full Document

UT CS 307 - CS 307 – Midterm

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
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 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 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?