PIC 20A Lecture 1, Spring 2002

Instructor: Fedor A. Andrianov

MIDTERM EXAM



STUDENT DATA:




1. (20 points):
What happens when you try to compile and run the following code? (Circle the correct answer.)
    public class EqualsTest {
	public static void main(String[] args) {
	    Long a = new Long(7);
	    Long b = new Long(7);
	    if (a == b) {
		System.out.println("Equal");
	    } else {
		System.out.println("Not Equal");
	    }
	}
    }
    
A. The program compiles but throws a runtime exception in line 5.
B. The program compiles and prints "Equal".
C. The program compiles and prints "Not Equal".









2. (20 points):
The GenericFruit class declares the following method:
    public void setCalorieContent(float f)
    
You are writing a class Apple to extend GenericFruit and want to add methods that overload the method in GenericFruit. Which of the following would constitute legal declarations of overloading methods? (Circle all correct answers.)
A. protected float setCalorieContent(String s)
B. protected void setCalorieContent(float x)
C. public void setCalorieContent(double d)
D. public void setCalorieContent(String s) throws NumberFormatException



3. (20 points):
The GenericFruit class declares the following method to return float number of calories in the average serving size:
    public float aveCalories()
    
Your Apple class, which extends GenericFruit, overrides this method. In a DietSelection class that extends Object, you want to use the GenericFruit method on an Apple object. What is the correct way to finish the statement in the following code fragment so the GenericFruit version of aveCalories is called using the gf reference?
    GenericFruit gf = new Apple();
    float cal = // finish this statement using gf
    
(Circle the correct answer.)
A. gf.aveCalories();
B. ((GenericFruit) gf).aveCalories();
C. gf.super.aveCalories();
D. There is no way to call the GenericFruit method









4. (20 points):
You want the following method to print a message when the input value is in the range equal to or greater than xMin and less than or equal to xMax:
    public void chkRange(int x) {
	if ( /* ??? */ ) System.out.println("In Range");
    }
    
What alternate expressions could be substituted for /* ??? */ in line 2 of the code? (Enter at least two correct answers.)




5. (20 points):
Write a program Substring that takes two numbers and a string as command-line arguments and prints out the substring of the string specified by the two numbers (first number is the beginning index, inclusive, and the second number - the ending index, exclusive). For example:
    > java Subsrtring hello 1 4
    
should print out:
    ell
    
Handle all possible exceptions that might arise because of bad input. Please remember that java.lang.ArrayIndexOutOfBoundsException thrown to indicate that an array has been accessed with an illegal index. You also might find useful the following methods from the Standard Library:
    public String java.lang.String.substring(int beginIndex, int endIndex)
	    throws IndexOutOfBoundsException;
    public int java.lang.Integer.parseInt(String s)
	    throws NumberFormatException;
    
Please continue your answer on the other side of page 3 --->



Good Luck !

Last modified on by fedandr@math.ucla.edu.