fa06M1 - Points off 1 2 3 4 Total off Net Score CS 307...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
Points off 1 2 3 4 Total off Net Score CS 307 – Midterm 1 – Fall 2006 Your Name____________________________________ Your UTEID __________________________________ Circle yours TA’s name: Alison Tekin Vineet Instructions: 1. Please turn off your cell phones 2. There are 4 questions on this test. 3. You will 2 hours to complete the test. 4. You may not use a calculator or any other electronic devices while taking this test. 5. Please make your answers legible. 6. When code is required, write Java code. 7. When writing a method, assume the preconditions of the method are met. 8. When writing a method you may add helper methods if you wish. 9. 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 an exception error answer "exception". If it would result in an infinite loop answer "infinite loop". A. What is the output of the following code? int z = 3; int y = z * z + 5 / 2; ++z; System.out.println( z + " " + y); B. What is the output of the following code? int x = -2; int y = 12; while( x <= y ){ x = x * x; y++; } System.out.println( x + " " + y ); CS 307 – Midterm 1 – Fall 2006 1
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
C. What is the output of the following code? int[] data = {2, 1, 7, 3, 4, 2}; for(int i = 1; i < data.length; i++){ data[i] = data[i] + data[i-1]; } System.out.println( data[5] ); D. What is the output of the following code? int[][] mat = {{2, 3, 4}, {-1, 5, 10}, {4, 6, 7}}; for(int r = 0; r < mat.length; r++){ for(int c = 0; c < mat[0].length; c++){ if( r != c ){ mat[r][c] = mat[c][r]; } } } for(int i = 0; i < mat.length; i++) System.out.print( mat[i][2] + " "); E. What is the output of the following code when method kk9 is called? public void kk9(){ int total = 3; mm6( total, 2 ); System.out.print( total ); } public void mm6(int total, int n){ total = total * (n + 1); System.out.print(total + " " + n + " "); } F. Could a call to method rs41 ever result in a NullPointerException? Briefly explain why or why not. // pre: obj != null public void rs41(Object obj){ String s = obj.toString(); System.out.println( s ); } CS 307 – Midterm 1 – Fall 2006 2
Background image of page 2
For questions G – O consider the following classes and interfaces. public interface MinuteBasedPlan{ public int cost(int used); public int baseCost(); } // a calling plan with a base cost and a cost per minute public class CallingPlan implements MinuteBasedPlan{ private int incCost; private int basePlanCost; public CallingPlan(int cost, int base){ incCost = cost; basePlanCost = base; } public int cost(int used){ return (used * incCost) + basePlanCost; } public int baseCost(){ return basePlanCost; } public void priceIncrease(){ incCost++; } } public class CallingPlanWithBaseMin extends CallingPlan{ private int baseMin; public CallingPlanWithBaseMin(int cost, int base, int min){
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 11/30/2010 for the course CS 307 taught by Professor Scott during the Spring '08 term at University of Texas.

Page1 / 20

fa06M1 - Points off 1 2 3 4 Total off Net Score CS 307...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online