Chapter 2 questions

advertisement
Name _____________________
Biomedical Technology
Chapter 5 Practice Test
Multiple Choice:
For questions 1 and 2, consider a class that stores 2 int values. These values can be assigned int values with the
messages set1(x) and set2(x) where x is an int, and these values can be accessed through get1( ) and get2( ). Assume
that y and z are two objects of this class. The following instructions are executed:
y.set1(5);
y.set2(6);
z.set1(3);
z.set2(y.get1( ));
y = z;
1)
The statement y.get2( ); will
a) return 5
b) return 6
c) return 3
d) return 0
e) cause a run-time error
Answer: a. Explanation: The statement y = z; causes y and z to be aliases where y.get2( ); returns the same value as
z.get2( );. Since z.set2(y.get1( )); was performed previously, z and y's second value is 5.
2)
If the instruction z.set2(y.get1( )); is executed, which of the following is true?
a) (y = = z) is still true
b) (y.get1( ) = = z.get1( )) but (y.get2( ) != z.get2( ))
c) (y.get1( ) = = z.get1( )) and (y.get2( ) = = z.get2( )) but (y != z)
d) (y.get1( ) = = z.get2( )) and (y.get2( ) = = z.get1( )) but (y != z)
e) the statement causes a run-time error
Answer: a. Explanation: Since y=z; was performed previously, y and z are aliases, so that a change to one results in a
change to the other. The statement z.set2(y.get1( )); is essentially the same as z.set2(z.get1( )); or y.set2(y.get1( )); and
in any case, it sets the second value to equal the first for the object which is referenced by both y and z.
3)
Which of the following methods is a static method? The class in which the method is defined is given in
parentheses following the method name.
a) equals (String)
b) toUpperCase (String)
c) sqrt (Math)
d) format (DecimalFormat)
e) paint (Applet)
Answer: c. Explanation: The Math class defines all of its methods to be static. Invoking Math methods is done by
using Math rather than a variable of type Math. The other methods above are not static.
For question 4, use the following class definition:
public class StaticExample
{
private static int x;
public StaticExample (int y)
{
x = y;
}
Ch 5 Practice Test
Ch 5 Practice Test
public int incr( )
{
x++;
return x;
}
}
4)
If there are 4 objects of type StaticExample, how many different instances of x are there?
a) 0
b) 1
c) 3
d) 4
e) There is no way to know since any of the objects might share x, but they do not necessarily share x
Answer: b. Explanation: Because x is a static instance data, it is shared among all objects of the StaticExample class,
and therefore, since at least one object exists, there is exactly one instance of x.
5)
An object that refers to part of itself within its own methods can use which of the following reserved words to
denote this relationship?
a) inner
b) i
c) private
d) this
e) static
Answer: d. Explanation: The reserved word this is used so that an object can refer to itself. For instance, if an object
has an instance data x, then this.x refers to the object’s value x. While this is not necessary, it can be useful if a local
variable or parameter is named the same as an instance data. The reserved word this is also used to refer to the class as
a whole, for instance, if the class is going to implement an interface class rather than import an implementation of an
interface class.
If s is a String, and s = “no”; is performed, then s
a) stores the String “no”
b) references the memory location where “no” is stored
c) stores the characters ‘n’, ‘o’
d) stores an int value that represents the two characters
e) stores the character ‘n’ and a reference to the memory location where the next character, ‘o’ is stored
Answer: b. Explanation: Strings are objects and all objects in Java are referenced by the variable declared to be an
object. That is, the variable represents the memory location where the object is stored. So, s does not directly store
“no” or ‘n’, ‘o’, but instead stores a memory location where “no” is stored.
6)
7)
In order to have some code throw an exception, you would use which of the following reserved words?
a) throw
b) throws
c) try
d) Throwable
e) goto
Answer: a. Explanation: The reserved word throw is used to throw an exception when the exception is detected, as in:
if (score < 0) throw new IllegalTestScoreException(“Input score ” + score + “ is negative”);
True/False Questions:
1) The = = operator performs differently depending on whether the variables being compared are primitives types or
objects.
Ch 5 Practice Test
Answer: True. Explanation: If two objects are being compared using = =, then the comparison returns true if the two
objects are aliases (the same object). But if two primitive data types are being compared, then the comparison returns
true if the two variables store the same value.
2) If String x = null; then x.length( ); returns the int 0.
Answer: False: Explanation: Since x is null, the instruction x.length( ); tries to pass the length( ) message to no object.
This cannot occur and so a NullPointerException is thrown.
3) Any variable can take on the value null to indicate that it has no value.
Answer: False. Explanation: Only variable that are object references can be null, which indicate that they do not refer
to any object. Variables of primitive types such as int and double cannot be assigned null.
Free-form Questions:
1) Identify the possible classes in the following partial program description:
The program will be used at a vehicle inspection station. Each vehicle that enters the station will have an inspector
assigned to it. The vehicle must pass a series of tests. A record of each customer, their vehicle, and the test results
should be kept.
Answer:
(vehicle inspection) station
vehicle
inspector
customer
test
Download