1. What are mutators and accessors? Give examples.
-Mutators are method that changes an object’s state. One example is the setName method. Accessors are method to access the object’s state to check if the mutator has changed the states of object. One example is the getName method.
2. List two visibility modifiers and describe when they are used.
-(1)private: specify that data or method to only be used in certain cirtaintance, within {}.
-(2)public: the data or imformation after such a modifier can be seen or used by anybody and anywhere.
3.What is a constructor?
– It’s a method that used to initialize the values for the object which with the same name as the class it falls in.
4.Why do we include a toString method with a new user-defined class?
-By using a toString method with a new user-defined class, we converted all the values of the class into strings, therefore, these informations can be easily viewed.
5. How can two variables refer to the same object? Give an example.
-We can refer one to the object , then says the other one equals to the previous one.
– For example: s = new Student ()
s1 = s
6.Explain the difference between a primitive type and a reference type, and give an example of each.
-a primitive type is data type that contains a value, such as int. a reference type contains a pointer to an object, such as class.
7. What is the null value?
-It make the variable no longer refers to anything. It means empty value
8. What is a null pointer exception? Give an example.
-The null pointer exception throws the value that contains a null value, which refers to nothing.
– String krit = null;
System.out.println (krit)
9.How does a default constructor differ from other constructor?
-It’s a primitive constructor java contains behind the scene. It initialize numeric variables to zeros and objects to null. For the other constructor, you can assign all variables values yourself.
10.How does Java handle the initialization of instance variables if no constructors are provided?
-It will give all numeric variables zeros, and all objects null value.
11.What is the purpose of a constructor that expects another object of the same class?
-To be able to change the poperty of one object by using the poperty of another object.