Preview

04 - Accessor Methods

 1. Accessor methods are used for initializing and accessing the value of ______________.
private int width = 10;
private int height = 3;
private Color rectCol = Color.blue;

public int getWidth()
{
     return width;
}

public int getHeight()
{
     return height;
}

public Color getColor()
{
    return rectCol;
}

  objects

  private strings

  instance variables.

  class methods

 2. Getter and setter methods allow us to get and set instance variables from the ___________.

  declared variables

  client class

  constructor

  derived object

 3. An accessor method is useful for efficiency and speed but it does not make the program more readable and understandable.

  FALSE

  TRUE

 4. One of the ways we can enforce data ______________ is through the use of accessors and mutators.

  inheritance

  encapsulation

  polymorphism

  variability

 5. The role of accessors and mutators are to return and set the values of an object's __________.

  methods

  fields

  state

  accessibility

 6. An alternative to using an accessor method is to just change the private fields of the class definition to be public and achieve the same results.
Note: Doing so is unideal as we wish to hide the data of the object as much as possible.

  TRUE

  FALSE

 7. The name of an accessor method starts with the word _____.

 8. What is missing from the following code? (accessor method)
public String Name()
		{
			Return name;
		}

  Line 1 should be: public String getName()

  Line 3 should be: Return get name;

  Line 1 should be: private String getName()

  Line 1 should be: public getName (remove the String)

 9. Which statement is true of the following list?
Single point of contact
Lazy initialization
Complete control
Easy modification to business rules
Encapsulation of validation logic
Overcome name hiding issue
Hide undo and redo logic
Use of getter accessor for constants

  They list the advantages of using accessor methods

  They list the disadvantages of using accessor methods (public fields are a better option)

  They list the advantages of using public methods over accessor methods

  They list the desired objectives of using a mutator method over an accessor method

 10. We use getter and setter methods for validation, to hide internal representation, expansion, and ______.

  binary representation

  security

  file compression

  data duplication

 11. Getter methods usually return a number of different variables, including ones from other functions that we may not be trying to get.

  FALSE

  TRUE

 12. When creating getter methods there is a common convention you should follow. Use this format: get[Variable Name] for the name.

  TRUE

  FALSE

 13. Getter (accessor) methods are also known as modifier methods

  FALSE

  TRUE

 14. Here is an example of an accessor method called __________ for the Student class which also demonstrates how to call ________using a Student object:
class Student {

  //Instance variable name
  private String name;

  /** getName() example
   *  @return name */
  public String getName()
  {
     return name;
  }

  public static void main(String[] args)
  {
     // To call a get method, use objectName.getVarName()
     Student s = new Student();
     System.out.println("Name: " + s.getName() );
  }

  return name

  objectName.get

  getName()

  geVarName()

 15. Accessor methods that return primitive types use "return by value" where a copy of the value is returned.

  False

  True

 16. When the return expression is a reference to an object, a copy of that reference is returned, not a copy of the object.

  False

  True

 17. Analyse the code below. The method getNumOfPeople does NOT work because the method should have at least one parameter.
public class Party
{
    private int numOfPeople;

    public Party(int num)
    {
        numOfPeople = num;
    }

    private int getNumOfPeople()
    {
        return numOfPeople;
    }
}

  FALSE

  TRUE

 18. Referring to the code above, the instance variable num should be returned instead of numOfPeople, which is local to the constructor. (then it will work!)

  FALSE

  TRUE

 19. To get the code in question 17 to work, the getNumOfPeople method should be declared as public.

  TRUE

  FALSE

 20. Consider the code below. Which of the following reasons explains why this code does not compile?
public class Student
{
    private int id;
    public getId()
    {
        return id;
    }
    // Constructor not shown
}

  The id instance variable should be public.

  The return type of the getId method needs to be defined as int.

  The getId method requires a parameter.

  The getId method should be declared as private.