Chapter Review Questions

  1. Where in a class are instance variables declared?

  2. For most instance variables, what is the modifier used that does not appear at the beginning of a local variable declaration?

  3. What is the lifetime of an instance variable: When does it come into existence, and how long does it last?

  4. Why do we generally make an instance variable private?

  5. In what code can an instance variable be seen and used?

  6. Must instance variables and methods always be preceded by an explicit object reference and .?

  7. Can we refer to an instance variable in a part of the code where there is no current object?

  8. In what kind of method in a class definition are instance variables never accessible?

  9. What is the purpose of a constructor?

  10. How is the heading of a constructor different from a regular method?

  11. How are parameters to a constructor generally used?

  12. If you do not explicitly assign a value to an instance variable in a constructor, does the instance variable have a value?

  13. If we want users to be able to see the value of a private instance variable from outside of the class, how do we do it?

  14. What is the general name of the category of methods that return instance state values?

  15. Instance variables are usually visible from inside instance methods for the class. What is the exception? In the exceptional case, what is the workaround to allow access to the instance variable?

  16. Sometimes you need to refer explicitly to the current object. How do you do it?

  17. Sometimes you want to let users outside the class modify the value of a private instance variable. How do you do it?

  18. What is the general name of the category of public methods whose sole purpose is to set a part of instance state to a new specified value?

  19. If a class has one or more setter methods, is the object type immutable?

  20. What is the return type for a setter method?

  21. If you want to set an instance variable in a method, should you declare that instance variable in the method?

  22. A method with what signature allows you to control how the string concatenation operate (+) generates a string from the object?

  23. If you write an override the ToString method in a class, should the method print the string? If not, what should it do with the resulting string?

  24. What is this?

  25. Can aliased objects cause problems when created for an immutable object? Mutable object?

  26. In a class with instance methods you can always design the class so variables are instance variables and not local variables. When should you use local variables instead?

  27. If an instance method has a formal parameter of the same type as the class being defined, can you refer to a private instance variable in the parameter object? May you change it? How do you distinguish an instance variable for the current object from the corresponding instance variable for the parameter object?