CPSC150 EH19 1
CPSC150 EH19
Q1: The ______of a class are also called the public services or the public interface that the class provides to its clients.
- public constructors.
- public instance variables.
- public methods.
- All of the above.
Q2: Which statement is false?
- The actual data representation used within the class is of no concern to the class’s clients.
- Clients generally care about what the class does but not how the class does it.
- Clients are usually involved in a class’s implementation.
- 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?
- Methods and instance variables can both be either public or private.
- Information hiding is achieved by restricting access to class members via keyword public.
- The private members of a class are directly accessible to the client of a class.
- None of the above is true.
Q4: When should a program explicitly use the this reference?
- Accessing a private variable.
- Accessing a public variable.
- Accessing a local variable.
- Accessing a field that is shadowed by a local variable.
Q5: Having a this reference allows:
- A method to refer explicitly to the instance variables and other methods of the object on which the method was called.
- A method to refer implicitly to the instance variables and other methods of the object on which the method was called.
- An object to reference itself.
- All of the above.
Q6: A constructor cannot:
- Be overloaded.
- Initialize variables to their defaults.
- Specify return types or return values.
- Have the same name as the class.
Q7: Constructors:
- Initialize instance variables.
- When overloaded, can have identical argument lists.
- When overloaded, are selected by number and types of parameters.
- a and c.
Q8: A constructor that has no arguments is called a ______.
- zero-argument constructor.
- no-argument constructor.
- default constructor.
- 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 compile-time error occurs.
- A runtime error occurs.
- It is a syntax error.
- 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.
- public.
- private.
- protected.
- All of the above.
Q11: Which statement is false?
- The compiler always creates a default constructor for a class.
- 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.
- 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.
- 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.
- query, mutator.
- accessor, mutator.
- mutator, accessor.
- query, accessor.
Q13: Using publicset methods provides data integrity if:
- The instance variables are public.
- The instance variables are private.
- The methods perform validity checking.
- Both b and c.
Q14: Composition:
- Is a form of software reuse.
- Is using an object reference as a class member.
- Is a good design practice.
- All of the above.
Q15: Composition is sometimes referred to as a(n) ______.
- is-a relationship.
- has-a relationship.
- have-a relationship.
- 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.