Chapter 8



CPSC150 EH19

Q1: The _________ of a class are also called the public services or the public interface that the class provides to its clients.

a. public constructors.

b. public instance variables.

c. public methods.

d. All of the above.

Q2: Which statement is false?

a. The actual data representation used within the class is of no concern to the class’s clients.

b. Clients generally care about what the class does but not how the class does it.

c. Clients are usually involved in a class’s implementation.

d. Hiding the implementation reduces the possibility that other program parts will become dependent on class-implementation details.

Q3: Which of the following statements is true?

a. Methods and instance variables can both be either public or private.

b. Information hiding is achieved by restricting access to class members via keyword public.

c. The private members of a class are directly accessible to the client of a class.

d. None of the above is true.

Q4: When should a program explicitly use the this reference?

a. Accessing a private variable.

b. Accessing a public variable.

c. Accessing a local variable.

d. Accessing a field that is shadowed by a local variable.

Q5: Having a this reference allows:

a. A method to refer explicitly to the instance variables and other methods of the object on which the method was called.

b. A method to refer implicitly to the instance variables and other methods of the object on which the method was called.

c. An object to reference itself.

d. All of the above.

Q6: A constructor cannot:

a. Be overloaded.

b. Initialize variables to their defaults.

c. Specify return types or return values.

d. Have the same name as the class.

Q7: Constructors:

a. Initialize instance variables.

b. When overloaded, can have identical argument lists.

c. When overloaded, are selected by number and types of parameters.

d. a and c.

Q8: A constructor that has no arguments is called a ________.

a. zero-argument constructor.

b. no-argument constructor.

c. default constructor.

d. main constructor.

Q9: What happens when this is used in a constructor’s body to call another constructor of the same class if that call is not the first statement in the constructor?

a. A compile-time error occurs.

b. A runtime error occurs.

c. It is a syntax error.

d. Nothing happens. The program compiles and runs.

Q10: When implementing a method, use the class’s set and get methods to access the class’s ________ data.

a. public.

b. private.

c. protected.

d. All of the above.

Q11: Which statement is false?

a. The compiler always creates a default constructor for a class.

b. If a class has constructors, but none of the public constructors are no-argument constructors, and a program attempts to call a no-argument constructor to initialize an object of he class, a compilation error occurs.

c. A constructor can be called with no arguments only if the class does not have any constructors or if the class has a public no-argument constructor.

d. Java allows other methods of the class besides its constructors to have the same name as the class and to specify return types.

Q12: Set methods are also commonly called ________ methods and get methods are also commonly called ________ methods.

a. query, mutator.

b. accessor, mutator.

c. mutator, accessor.

d. query, accessor.

Q13: Using public set methods provides data integrity if:

a. The instance variables are public.

b. The instance variables are private.

c. The methods perform validity checking.

d. Both b and c.

Q14: Composition:

a. Is a form of software reuse.

b. Is using an object reference as a class member.

c. Is a good design practice.

d. All of the above.

Q15: Composition is sometimes referred to as a(n) ________.

a. is-a relationship.

b. has-a relationship.

c. have-a relationship.

d. one-to-many relationship.

Q16: Create a class Rectangle. The class has attributes length and width, eachof which defaults to 1. It has methods that calculate the perimeter and the area of the rectangle, It has set and get methods for both length and width. The set methods should verify that length and width are each floating-point numbers larger that 0.0 and less than 20.0. Write a program to testclass Rectangle.

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download