DOC PREVIEW
UT CS 307 - CS 307 – Midterm 1

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

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

Unformatted text preview:

CS 307 – Midterm 1 – Fall 2009 1 Points off 1 2 3A 3B 4A 4B Total off Net Score CS 307 – Midterm 1 – Fall 2009 Your Name____________________________________ Your UTEID __________________________________ Circle yours TA’s name: Oswaldo Rashid Swati Instructions: 1. Please turn off or silence your cell phones. 2. There are 4 questions on this test. 3. You have 2 hours to complete the test. 4. You may not use a calculator or any other electronic devices while taking the test. 5. When code is required, write Java code. 6. When writing a method, assume the preconditions of the method are met. 7. When writing a method you may add helper methods if you wish. 8. 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". A. What is output by the following code? int[] list1 = {4, 2, -1}; int[] list2 = new int[3]; list1[1]++; list2 = list1; list1[1]++; System.out.println(Arrays.toString(list2)); // prints out elements B. What is output by the following code? int[] list3 = {2, 1, 5, 1}; System.out.println( list3[ list3[0] ] );CS 307 – Midterm 1 – Fall 2009 2 For questions C – N consider the following classes and interfaces. public interface TotalPoints{ public int getPoints(); } public class WinLossRecord implements TotalPoints{ private int wins; private int losses; public WinLossRecord(){ this(0, 0); } public WinLossRecord(int ws, int ls){ wins = ws; losses = ls; } public void win(){ wins++; } public void lose(){ losses++; } public int getWins(){ return wins; } public int getLosses(){ return losses; } public int getPoints(){ return wins * 2; } } public class WinLossRecordWithTies extends WinLossRecord{ private int ties; public WinLossRecordWithTies(){ ties = 0; } public WinLossRecordWithTies(int ws, int ls, int ts){ super(ws, ls); ties = ts; } public void tie(){ ties++; } public int getTies(){ return ties; } public int getPoints(){ return getWins() * 3 + getTies(); } }CS 307 – Midterm 1 – Fall 2009 3 C. State if the following declarations are valid or invalid (meaning they cause a syntax error). 1 point each TotalPoints t1 = new TotalPoints(); Object obj1 = new WinLossRecord(); D. State if the following declarations are valid or invalid (meaning they cause a syntax error). 1 point each WinLossRecordWithTies w2 = new Object(); TotalPoints t2 = new WinLossRecordWithTies(3, 2, 3); E. State if the following declarations are valid or invalid (meaning they cause a syntax error). 1 point each TotalPoints t3 = new WinLossRecord(); WinLossRecord w3 = new WinLossRecordWithTies(0, 4, 0); F. What is output by the following code when method f2 is called? public void f1(WinLossRecord w){ w.win(); w.lose(); w.win(); } public void f2(){ WinLossRecord ws = new WinLossRecord(); f1(ws); System.out.println( ws.getPoints() ); } G. What is output by the following code when method g2 is called? public void g1(int w){ w += 3; w *= 2; } public void g2(){ WinLossRecord ws = new WinLossRecord(); g1( ws.getWins() ); System.out.println( ws.getPoints() ); }CS 307 – Midterm 1 – Fall 2009 4 H. What is output by the following code when method h2 is called? public void h1(WinLossRecord w){ System.out.print( w.getPoints() + " "); // single space added } public void h2(){ WinLossRecord ws1 = new WinLossRecord(3, 5); h1(ws1); WinLossRecordWithTies ws2 = new WinLossRecordWithTies(3, 5, 3); h1(ws2); } I. What is output by the following code? WinLossRecordWithTies w5 = new WinLossRecordWithTies(); w5.win(); w5.win(); w5.tie(); System.out.println( w5.getPoints() ); J. What is the output by the following code? TotalPoints[] tList = new TotalPoints[2]; tList[0] = new WinLossRecordWithTies(); tList[1] = new WinLossRecord(); System.out.println( tList[0].getPoints() + " " + tList[1].getPoints() ); K. Explain in one or two sentences why the following code contains a syntax error. TotalPoints t4 = new WinLossRecord(); t4.win(); System.out.println( t4.getPoints() ); L. What is the output by the following code? Object obj2 = new WinLossRecord(); WinLossRecordWithTies w6 = (WinLossRecordWithTies)obj2; System.out.println( w6.getTies() );CS 307 – Midterm 1 – Fall 2009 5 M. Explain in one or two sentences why the following client code contains a syntax error. WinLossRecord w7 = new WinLossRecord(); w7.win(); w7.lose(); w7.wins *= 2; System.out.println( w7 ); N. What is output by the following client code? WinLossRecord w8 = new WinLossRecord(); WinLossRecord w9 = new WinLossRecord(); System.out.println( w8 == w9 ); O. 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 toStiring(); // returns String of the form [val1, val2, ..., lastVal] } What is output by the following client code? IntList list = new IntList(); System.out.print( list.size() + " "); list.add(5); list.add(3); list.add(0); System.out.println( list.toString() );CS 307 – Midterm 1 – Fall 2009 6 2. The IntList class. (10 points) In lecture we developed an IntList class to represent a list of ints 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 IntList class named increaseRange. This is an instance method in the IntList class that increases the values in a specified range of the calling object by the corresponding values in another IntList. Here is the method header: /* pre: other != null 0 <= start < size, start <= stop < size(), post: all elements in this IntList in the range from start to stop inclusive have been increased by the corresponding values in


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?