In: Computer Science
Question 1
price is an instance variable of a class. Inside that class there is a method public void change(int price).
From inside the method " change" how do we refer to the instance variable price and not the parameter price?
Answer:
Question 2
A method of a class can access which of the following:
1 global variables in the same scope
2. local variables defined within the method
3. the instance variables of its class
4. its parameters
Question 3
Which of the following are true of accessor methods:
1. accessor methods are of the same type as the variable to which they provide access.
2. accessor methods take no parameters
3. accessor methods are always of type void
4. accessor methods should be private
Solution :
Answer 1 :
this.price will refer to the instance variable of the class. only
"price" will refer to the parameter of the method.
Answer 2 :
1 : global variables can be accessed if they are in same
scope.
2 : local variables defined inside method can be accessed inside
method.
3 : Instance variable of the class can be accessed inside method
too if the method is not static.
4 : parameters passed to the method can be accessed inside
method.
Answer 3 :
1. Yes the return type of the accessor method is same as the data
type of the variable that is being accessed.
2. Yes, Accessor method doesnt need any parameter, as it returns
the value of the variable being accessed.
3. No accessor methods are not void always.
4. no accessor method can not be private.
Answer: 1 and 2 are true