midterm1Spring003 - Points off 1 2 3 4 Admin Total off Net...

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 Admin Total off Net Score CS 307 – Midterm 1 – Spring 2003 Your Name____________________________________ First Name of USL ______________ Last four digits of Social Security Num / Student ID _______________________ 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. When code is required, write Java code. 5. Please make your answers legible. 6. The style guide is not in effect, except as noted. 7. Unless noted on the question, you may not use any of the classes or methods from the Java standard library. You may use native arrays 1. (2 points each, 30 points total) Java Mechanics. For all parts, what is the output of the code fragment? Write your answer on the line provided. Consider each piece of code in isolation. If the code would cause an error, answer "syntax error" or "runtime error" depending on what type of error it would be. A. public void bug(int y) { int temp = 0; for(int i = 0; i < y; i++) temp += i * 2; System.out.println( temp ); } What is the output of the above code assuming method bug is called with an argument of 4? ( bug(4); ) ____________________________________________ CS 307 – Midterm 1 – Spring 2003 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
B. // pre: list != null public void jump(int[] list) { int total = 0; int limit = 100; for(int i = 0; i < limit; i++) total += list[i]; System.out.println(total); } Can the above code cause a runtime error? Explain why or why not. _________________________________________________ For parts C – J consider the following class: public class Rectangle { private int iMyWidth; private int iMyHeight; public Rectangle() { iMyWidth = 1; iMyHeight = 1; } public Rectangle(int width, int height) { iMyWidth = width; iMyHeight = height; } public void setWidth(int newWidth) { iMyWidth = newWidth; } public void setHeight(int newHeight) { iMyHeight = newHeight; } public int getWidth() { return iMyWidth; } public int getHeight() { return iMyHeight; } public String toString() { return iMyWidth + " " + iMyHeight; } } CS 307 – Midterm 1 – Spring 2003 2
Background image of page 2
Rectangle[] rList = new Rectangle[10]; System.out.println( rList[3].getWidth() ); What is the output of the above code? D. public class RectangleUser2 { public void rough(Rectangle r1) { r1.setWidth( 50 ) r1 = new Rectangle(10, 30); r1.setHeight( 15 ); System.out.println( r1.toString() ); } public void neck() { Rectangle r1 = new Rectangle(5, 20); System.out.println( r1.toString() ); ep1.setWidth(40); System.out.println( r1.toString() ); } } What is the output when method neck in class RectangleUser2 is called? ___________________________________________________________ CS 307 – Midterm 1 – Spring 2003
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.

Page1 / 14

midterm1Spring003 - Points off 1 2 3 4 Admin Total off Net...

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