midterm2Fall2001ver2 - Points off 1 2 3 4 5 Admin Total off...

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

View Full Document Right Arrow Icon
Points off 1 2 3 4 5 Admin Total off Net Score CS 307 – Midterm 2 – Fall 2001 Name____________________________________ Last 4 digits of SSN / Student ID ______________ Class Unique ID ___________________________ Instructions: 1. There are 5 questions on this test. Each is worth 20 points. 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. You may not use any methods from the Java Standard Library other than the ones specifically mentioned on each question. 6. The style guide is not in effect. 7. Please make your answers legible. 1. Java mechanics and short answer questions. Write the answer to each question in the space provided. If code results in an error indicate if it is a compile error or runtime error. For questions A through C consider the following classes: public class Vehicle { private int iMyWheels; public Vehicle(int w) { iMyWheels = w; } public void setWheels(int w) { iMyWheels = w; } public int getNumWheels() { return iMyWheels; } public String toString() { return "wheels: " + iMyWheels; } } CS 307 – Midterm 1 – Fall 2001 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
public class Truck extends Vehicle { private boolean bMyAllWheelDrive; public Truck(int w, boolean d) { super(w); bMyAllWheelDrive = d; } public void setWheels(int w) { if( w < 6 ) super.setWheels(w); else bMyAllWheelDrive = false; } public String toString() { String result = super.toString(); if( bMyAllWheelDrive ) result += ", " + getNumWheels() + " wheel drive"; else result += ", " + 2 + " wheel drive"; return result; } } A. Consider the following code which appears in a class other than Vehicle and Truck: Truck t = new Truck(4, true); System.out.println( t.getNumWheels() ); What is the output of the this of code? ____________________________________________________ B. Consider the following code which appears in a class other than Vehicle and Truck: Truck t1 = new Truck(6, true); t1.setWheels( 8 ); System.out.println( t1.toString() ); What is the output of the this of code? ____________________________________________________ CS 307 – Midterm 1 – Fall 2001 2
Background image of page 2
C. Consider the following code which appears in a class other than Vehicle and Truck: Vehicle[] v = new Vehicle[2]; v[0] = new Vehicle(4); v[1] = new Truck(6, true); System.out.println( v[0].toString() + " " + v[1].toString() ); What is the output of the this of code? ____________________________________________________ D. Consider the following method: public int wand(int n) { if( n == 0) return 2; else return 1 + wand(n/2) + wand(n/3); } What is the output of the following statement which appears in the same class as wand? System.out.println( wand(6) ); ____________________________________________________ CS 307 – Midterm 1 – Fall 2001 3
Background image of page 3

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

View Full DocumentRight Arrow Icon
E. Consider the following method: public int stone(int n) { if( n == 0) return 0; else return n * n + stone( n – 1 ); } What is the output of the following statement which appears in the same class as stone? System.out.println( stone(5) );
Background image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 15

midterm2Fall2001ver2 - Points off 1 2 3 4 5 Admin Total off...

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

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