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:

CS 307 – Midterm 1 – Spring 2010 1 Points off 1 2 3A 3B 4 Total off Net Score CS 307 – Midterm 1 – Spring 2010 Your Name____________________________________ Your UTEID __________________________________ Circle yours TA’s name: Guillermo Reza Shyamala Instructions: 1. There are 4 questions on this test. 2. You have 2 hours to complete the test. 3. You may not use a calculator or any other electronic devices while taking the test. 4. When writing a method assume the preconditions of the method are met. 5. When writing a method you may add helper methods if you wish. 6. 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 a runtime exception answer "exception". If it would result in an infinite loop answer "infinite loop". Questions A and B use the following method. public void helper(int[] vals){ vals[vals.length - 1] += 3; vals = new int[2]; vals[0]++; } A. What is output by the following code when method a is called? public void a(){ int[] data = {2, 3}; helper(data); System.out.println(Arrays.toString(data)); } B. What is output by the following code when method b is called? public void b(){ int[] data = new int[0]; helper(data); System.out.println(Arrays.toString(data)); }CS 307 – Midterm 1 – Spring 2010 2 For questions C – M consider the following classes and interfaces. Recall, the Comparable interface has one method, compareTo which returns an int. public abstract class Property implements Comparable{ private String name; public abstract int getValue(); public Property(String str){ name = str; } public String toString(){ return name; } public int compareTo(Object other){ return getValue() - ((Property)other).getValue(); } } public class Business extends Property{ private int area; public Business(String str, int a){ super(str); area = a; } public int getValue(){ return area * 100; } } public class Residential extends Property { private int rooms; public Residential(int r){ super("Res"); rooms = r; } public int getValue(){ return rooms * 1000; } public void addOn(){ rooms++; } } public class Rented extends Residential { public Rented(int r){ super(r); } public int getValue(){ return super.getValue() / 2; } } public class Owned extends Residential { private int yardArea; public Owned(int y){ this(y, 4); } public Owned(int y, int r){ super(r); yardArea = y; } public String toString(){ return "" + yardArea; } }CS 307 – Midterm 1 – Spring 2010 3 C. State if the following declarations are valid or invalid (meaning they cause a syntax error). 1 point each Object obj1 = new Rented(3); Object obj2 = new Property("Gym"); D. State if the following declarations are valid or invalid (meaning they cause a syntax error). 1 point each Comparable c1 = new Comparable(); Comparable c2 = new Owned(100); E. State if the following declarations are valid or invalid (meaning they cause a syntax error). 1 point each Owned w = new Residential(5); Business b = new Owned(100); F. State if the following declarations are valid or invalid (meaning they cause a syntax error). 1 point each Property p1 = new Rented(3); Property p2 = new Business("7-11", 100); G. What is output by the following code? Owned o1 = new Owned(100); Owned o2 = new Owned(100); System.out.println(o1 == o2); H. What is output by the following code when method h2 is called? public void h1(Residential r){ r.addOn(); r.addOn(); } public void h2(){ Residential r1 = new Residential(4); h1(r1); System.out.println(r1.getValue()); }CS 307 – Midterm 1 – Spring 2010 4 I. What is output by the following code when method i2 is called? public void i1(Business b){ b = new Business("Mac's", 100); } public void i2(){ Business b1 = new Business("J's", 200); i1(b1); System.out.println(b1); } J. What is the output by the following code? Owned o3 = new Owned(100); System.out.print(o3 + " "); System.out.println(o3.getValue()); K. What is the output by the following code when method k2 is called? public void k1(Residential r){ System.out.print(r.getValue() + " "); } public void k2(){ Rented d = new Rented(4); k1(d); Residential s = new Residential(4); k1(s); } L. What is the output by the following code? Property[] ps = new Property[2]; ps[0] = new Business("JP", 100); ps[1] = new Residential(5); System.out.println( ps[0].compareTo(ps[1]) ); M. Explain in one sentence why the following client code contains a syntax error. Owned o5 = new Owned(100, 5); System.out.println(o5); o5.rooms = 7; System.out.println(o5.getValue());CS 307 – Midterm 1 – Spring 2010 5 N. Recall the following methods from the IntList class we developed in lecture. public class IntList{ public IntList() // create an empty IntList public int add(int val) // add to end public int insert(int pos, int val) // add at specified location public int size() // return size of list public String toString(); // returns String of the form [val1, val2, ..., lastVal] } What is output by the following code? IntList list = new IntList(); System.out.print( list.size() + " " ); list.add(12); list.insert(0, 5); // parameters are position, value System.out.print( list ); O. What is returned by the following method if the file /bin/scores.tx does not exist? public int countTokens() { int result = 0; try { Scanner sc = new Scanner(new File("/bin/scores.tx")); while(sc.hasNext()){ sc.next(); } result = 5; } catch(FileNotFoundException e){ result = -1; } return result; }CS 307 – Midterm 1 – Spring 2010 6 2. The GenericList class. (25 points) In lecture we developed a GenericList class to represent a list of objects to demonstrate encapsulation and the syntax for building a class in Java. As discussed in class the internal storage container may have extra capacity. Complete a method for the GenericList class named addNewValues. This is an instance method in the GenericList class that adds all the elements from another


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?