Consider the following code snippet:
Employee programmer = new Employee(10254, "exempt"); String str = programmer.toString();
Assume that the Employee class has not implemented its own toString() method. What value will the variable str contain when this code is executed?
Group of answer choices
the variable will become a null reference
the code will not compile because Employee has not implemented toString()
the default from Object, which is the class name of the object and its hash code
the string representations of all of the field values
2
With very few exceptions, such as certain static constants, what access should fields of classes always have?
Group of answer choices
package
protected
public
private
3
How can a subclass modify a private field within its superclass?
Group of answer choices
use "super." followed by the name of the field
use a public method of the superclass
override the field with a field of the same name
use the name of the superclass field
4
Which is the difference between having a public field, and having a private field with public getter/setter methods?
Group of answer choices
there is effectively no difference between these approaches
getter/setter methods can be inherited, while fields cannot
setters can reject invalid values, and getters can do processing of the output
fields can be inherited, whereas getter/setter methods cannot
ANSWERS -
1) (c) the default from Object, which is the class name of the object and its hash code
The variable str will contain the object name along with its hash code when this code is executed.
2) (c) Public
Most of the time classes should also have Public declared.
3) (b) use a public method of the superclass
A subclass can modify a private field by using public method within its superclass.
4) (c) setters can reject invalid values, and getters can do processing of the output
Setters and getters to make write only or read only, it can also make sure that correct value is being set by placing some validations in setters and getters.
=================================END================================
Please comment if u need any other info and DO LEAVE A LIKE, it would mean a lot. Thanks :)
Get Answers For Free
Most questions answered within 1 hours.