Chapter 4: Writing Classes

Test

Multiple Choice:

1)The behavior of an object is defined by the object’s

a)instance data

b)constructor

c)visibility modifiers

d)methods

e)all of the above

2)To define a class that will represent a car, which of the following definitions is most appropriate?

a)private class car

b)public class car

c)public class Car

d)public class CAR

e)private class Car

3)In order to preserve encapsulation of an object, we would do all of the following except for which one?

a)Make the instance data private

b)Define the methods in the class to access and manipulate the instance data

c)Make the methods of the class public

d)Make the class final

e)All of the above preserve encapsulation

4)Consider a sequence of method invocations as follows: main calls m1, m1 calls m2, m2 calls m3 and then m2 calls m4, m3 calls m5. If m4 has just terminated, what method will resume execution?

a)m1

b)m2

c)m3

d)m5

e)main

5)A class’ constructor usually defines

a)how an object is initialized

b)how an object is interfaced

c)the number of instance data in the class

d)the number of methods in the class

e)if the instance data are accessible outside of the object directly

6)Instance data for a Java class

a)are limited to primitive types (e.g., int, double, char)

b)are limited to Strings

c)are limited to objects(e.g., Strings, classes defined by other programmers)

d)may be primitive types or objects, but objects must be defined to be private

e)may be primitive types or objects

7)Consider a method defined with the header: public void foo(int a, int b). Which of the following method calls is legal?

a)foo(0, 0.1);

b)foo(0 / 1, 2 * 3);

c)foo(0);

d)foo( );

e)foo(1 + 2, 3 * 0.1);

8)Consider a method defined with the header: public void doublefoo(double x). Which of the following method calls is legal?

a)doublefoo(0);

b)doublefoo(0.555);

c)doublefoo(0.1 + 0.2);

d)doublefoo(0.1, 0.2);

e)all of the above are legal except for d

.

Question 9 refers to the following method:

public intdogYears (int age)

{

if (age >= 0)

return age * 7;

else

return 0;

}

9)What is a mutator method?

a)A method that modifies a value.

b)A method that provides read-only access to a value.

c)A method that has the same name, but different parameters, as another method.

d)A method that is called when an object is first created.

e)A method that does not return a value.

True/False –

11)Formal parameters are those that appear in the method call and actual parameters are those that appear in the method header.

12)Java methods can return more than one item if they are modified with the reserved word continue, as in public continue intfoo( ) { … }

13)A method defined in a class can access the class’ instance data without needing to pass them as parameters or declare them as local variables.

14)Defining formal parameters requires including each parameters type.

15)While multiple objects of the same class can exist, there is only one version of the class.

16)A constructor is a method that gets called automatically whenever an object is created, for example with the new operator.

.

17)A constructor must always return an int.

18)The methods in a class should always be made public so that those outside the class can use them

19)The return statement must be followed a single variable that contains the value to be returned.

20)The different versions of an overloaded method are differentiated by their signatures.

21)A method defined without a return statement will cause a compile error.

22)Method decomposition is the process of creating overloaded versions of a method that do the same thing, but operate on different data types.

23)In a method with both a precondition and a postcondition, the postcondition should be true when the method finishes executing regardless of whether the precondition was true.

24)An accessor method provides access to a value and allows the caller to change that value.

Free Response:

Use the back of the answer sheet.

1)Write a method that receives two int parameters and returns the smaller of the two values. Call your method getSmaller.