DOC PREVIEW
UT 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 – Fall 2010 1 Points off 1 2 3 4A 4B Total off Net Score CS 307 – Midterm 1 – Fall 2010 Your Name____________________________________ Your UTEID __________________________________ Circle yours TA’s name: Harsh Yi-Chao 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 samples state the output. If the code would cause a syntax error then answer "syntax error". If it would cause a runtime exception then answer "exception". If it would result in an infinite loop answer "infinite loop". A. What is output by the following code when the client code is executed? public int a(int y){ y--; int x = y * 2 - 1; return x; } // client code int x = 4; int y = a(x); System.out.print(x + " " + y); B. What is output by the following code? ArrayList<Integer> vals = new ArrayList<Integer>(); ArrayList<Integer> other = vals; other.add(4); vals.add(2); vals.add(5); System.out.println(other);CS 307 – Midterm 1 – Fall 2010 2 C. What is output by the following code when the client code is executed? public void c(int[] list){ list[list.length - 1] += 2; list[0]--; } // client code int[] data = {3, 5, 2}; c(data); System.out.println(Arrays.toString(data)); D. What is output by the following code? ArrayList<String> names = new ArrayList<String>(); System.out.println(names.size()); E. Briefly explain why the following code will not compile. Object obj = new ArrayList<String>(); obj.add("Kelly"); System.out.println( obj.toString() ); System.out.println( obj.equals(obj) ); F. Does the following code result in syntax error, a runtime error (exception), or neither? int k = 10; String[] titles = new String[k + 2]; System.out.println( titles[ titles.length / 2 ].length() );CS 307 – Midterm 1 – Fall 2010 3 For questions G - N consider the following classes. public abstract class IPod { private int memory; public IPod() { memory = 40; } // pre: m > 0 public IPod(int m) { memory = m; } public abstract String memoryType(); public String toString() { return memoryType() + " " + memory; } } public class Touch extends IPod { public Touch() {} public Touch(int s) { super(s); } public String memoryType() { return "flash"; } public boolean hasTouchScreen() { return true; } } public class Classic extends IPod { public Classic(int s) { super(s); } public String memoryType() { return "hard drive"; } public int numColors() { return 2; } } public class HP extends Classic { public HP(int s) { super(s); } public int numColors() { return 1; } } G. Briefly explain why the following code will not compile: IPod p = new IPod(-20);CS 307 – Midterm 1 – Fall 2010 4 H. What is output by the following code? Touch t = new Touch(30); System.out.println( t.toString() ); I. What is output by the following code? IPod[] ps = new IPod[3]; ps[0] = new Touch(); ps[1] = new Classic(20); ps[2] = new HP(30); for(IPod p : ps) System.out.println(p); J. State if the following declarations are valid or invalid (meaning they cause a syntax error). 1 point each Classic c2 = new Touch(20); Classic c3 = new HP(20); K. State if the following declarations are valid or invalid (meaning they cause a syntax error). 1 point each Object obj = new HP(20); HP h1 = new Classic(30); L. What is output by the following code? Object obj2 = new Touch(30); System.out.println(obj2); M. What is output by the following code? Classic c2 = new HP(30); System.out.println( c2.memoryType() + " " + c2.numColors() ); N. What is output by the following code? IPod i2 = new Classic(30); System.out.println( i2.memoryType() + " " + ((Classic) i2).numColors() ); O. How many interfaces can a class in Java implement?CS 307 – Midterm 1 – Fall 2010 5 2. The GenericList class. (25 points) In lecture, to demonstrate encapsulation and the syntax for building a class in Java, we developed a GenericList class to represent a list of objects. As discussed in class the internal storage container may have extra capacity. Complete a method for the GenericList class named interleave. This is an instance method in the GenericList class that creates and returns a new GenericList object that has the elements of the calling GenericList and another GenericList sent as an explicit parameter interleaved. The method header is: /* pre: other != null post: return a new GenericList that has the elements of this GenericList and other interleaved. The size of the returned list equals 2 times the minimum of the size of this list and other. The elements of the returned list alternate between the values of this list, then other, then this list, then other, and so forth. Neither this nor other is altered as a result of this method call. */ public GenericList interleave(GenericList other) { Examples of calls to interleave: (Assume values shown are Strings.) [A, B, C].interleave([Q, M, P]) -> [A, Q, B, M, C, P] [].interleave([Q, M, P]) -> [] [A, B, C].interleave([]) -> [] [A, A, B, A, B].interleave([P, M, P]) -> [A, P, A, M, B, P] [A, B, C, D, E, F].interleave([Q, M, P]) -> [A, Q, B, M, C, P] [A, B, C].interleave([Q, M, P, Z, X, Y, K]) -> [A, Q, B, M, C, P] [A, B, C, D, E, F].interleave([B, A, M]) -> [A, B, B, A, C, M] [].interleave([]) -> [] [A].interleave([]) -> [] [A].interleave([Q]) -> [A, Q] You may not use any other methods in the GenericList class except the default constructor unless you define and implement them yourself. Recall that the interleave method is in the GenericList class so you have access to all GenericList objects' private instance variables.CS 307 – Midterm 1 – Fall 2010 6 You may not use objects or methods from other Java classes other than native arrays and the Math class. If you want to use any other GenericList methods, other than the default constructor,


View Full Document

UT CS 307 - Midterm 1

Documents in this Course
Midterm 2

Midterm 2

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