Programming and Problem Solving with Java

(やまだぃちぅ) #1
143

16.What distinguishes the heading of a value-returning method from a method
that doesn’t return a value? (pp. 122–123)
17.What features distinguish class methods from instance methods, both syntacti-
cally and semantically? (pp. 122–123)
18.When saving a class called Animalsin a separate file, what should you name the
file? (pp. 131–134)

Answers


1.There is only one copy, with two references to it.2.intand floatare both 32 bits.longand doubleare both 64
bits.3.A decimal point, a fractional part, and an exponent.4.Parentheses ( )



  1. a.someDouble = (double)(5 + someInt);
    b.someInt = (int)(2.5* (double)(someInt) / someDouble);

  2. 15 ow7.False. It can be called only within an expression.8.final doublePI = 3.14159;

  3. int count;
    doublesum;

  4. a.9.0/ 5.0or(double) 9 / (double) 5 b. 9 / 5
    11.The value is 1.12.The result is 3.13.9.0/ 5.0* c + 32.014.Math.sqrtand Math.abs15.The human reader.
    The compiler ignores code formatting.16.It has a type or class name in place of the keyword void.17.Class
    methods are static. They exist in only one place, yet are accessible to all objects of the class. In referring to a
    class method, we precede its name with the class name and a period. Instance methods are not static, and
    they are associated with each object. In referring to an instance method, we precede its name with the object
    name and a period.18.Animals.java.


Exam Preparation Exercises


1.Mark the following constructs either valid or invalid. Assume all variables are
of typeint.
Valid Invalid
a.x * y = c; ______ _______
b.y = con; ______ _______
c. private static final intx : 10 ; ______ _______
d.intx; ______ _______
e.a = b % c; ______ _______
2.If alphaand betaare intvariables with alpha= 4 and beta= 9, what value is stored
into alphain each of the following? Answer each part independently of the
others.
a.alpha = 3 * beta;
b.alpha = alpha + beta;
c. alpha++;
d.alpha = alpha / beta;
e.alpha--;
f. alpha = alpha + alpha;
g.alpha = beta % 6 ;
Free download pdf