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". |
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 |
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 |
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.)
________________________________________________________________________
________________________________________________________________________
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 4should print out:
ellHandle 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 --->