In: Computer Science
I'm doing a practice test and I can't seem to find the answers anywhere for these specific questions. If someone could please help clarify so I can learn that would be helpful!
1. Identify the true statements. Select ALL that apply.
a. Setters and getters are not required for public instance
variables.
b. A setter method is return-type void and takes a parameter of the same type as its field.
c. A getter method has the same return-type as the field it retrieves and takes no parameters.
d. Every private instance variable in a class definition must have a mutator method.
2.
private char gender;public class Person {
static int number;
//...
public static void main(String[] args) {
Person person;
//...
}
public static void callPerson(int hours) {
double payRate = 24.99;
}
}
The code above compiles okay. Refer to it to complete this matching exercise.
|
|
3. Identify the true statements. Select ALL that apply.
a. Static methods are also class methods
b. Static methods of a class can be called without instantiating the class.
c. A static method cannot access the data members of its own class.
d. A non-static method can be called from a static method.
4. What does the public visibility modifier mean?
a. means accessible from any class in the same package.
b. means accessible to the current program.
c. means accessible to any method in the class.
d. means accessible from any other classes.
5.
3. public class Person {
4. private double salary;
5. public Person() {
6. salary = 1000.0;
7. }
What type of constructor is illustrated by lines 5 through 7?
6. Identify the true statements. Select ALL that apply.
a. Constants in a class should be declared as final static.
b. Static variables and static methods of a class can be accessed by an object of that class and by the class name.
c. Static variables have a default value of null.
d. Anonymous objects are possible in Java.
7. What Java keyword sometimes used in a class definition refers to
the class itself?
1.
a.Public instance variable can be accessed(used) from outside the class. Hence setter and getter methods are not required. Thus the statement is true.
b. Setter method as the name suggest, it sets a value to a variable. Hence the return value is void and the parameter should be same as variable type to which this parameter value is assigned. Hence this statement is true.
c. Getter method is to get the value of the field, hence its return type should be of same type of field. As it is just getting the value, no parameters are required. Thus this statement is true.
d. Private instance variable can't be accessed from outside the class. Hence there should be an interface to access this variable either to assign a value or retrieve its value. This interface is nothing but mutator method. Thus this statement is true.
2. Match:
person: instance variable
hours: parameter
number: class variable
payRate: local primitive variable( allocated in stack and scope is till it's function(function in which it is declared) execution end
gender: local reference variable(allocated in heap and this variable point to that object).
2. a. Static methods are class methods as they belong to class and not to object of class. Hence it is true.
b. Static methods can be called without instantiating the class. Hence this statement is true.
c. Static methods can access data members that are too static(static variable) which belongs to the same class. Hence this statement is not true.
d. Static methods can't call non-static methods. It can call only other static methods of its own class. Hence this statement is not true.
4. Public access modifier makes irrespective of code location, it can access class, methods, variable which are assigned to public modifier. Hence the entire program can access them. Correct option is: b