11. Setters are void return type methods. True O False 12. To make fields directly accessible to other classes, the class fields must be marked public. True O False 13. Static variables of a class can be accessed, even if the class has not been instantiated. True O False 14. Given the following code, why does your IDE complain that “non-static variable name cannot be referenced from a static context”? public class Employee public String name public static void di public static int employeeID System.out.printin ID); System.out.printin(name) It would be possible to call the display() method and attempt to reference an object’s name before any object exists. 0 The variable name has a null value. O static variables are only accessible from instance methods. O Static variables cannot be referenced from methods. 15. Objects are accessed using reference variables. True O False
Expert Answer
11) True
Setters are those methods in the class which are used to set or initialize the class fields. Since they don’t have to return anything, their return type is set to ‘void’.
12) True
To use a class field in some other class (in case it is not being inherited), they need to be declared public.
13) True
The static members of a class are not bound with an instance of the class but with the class itself. Therefore, they can be accessed using the class name only without instantiating it.
14) It would be possible to call the display() method and attempt to reference an object’s name before any object exists.
As explained earlier, a static field in a class is bound to the class and is shared by all the instance of that class, i.e., they can be referenced without creatin an object of the class. Since “display()” is a static member of the class, it can be called directly using the class name and would try to reference the fields “name” and “employeeID”. “employeeID”, being a static field itself, would give no problem. But the variable “name” would require an object of the class. Therefore it cannot be accessed inside a static member function.
15) True
A reference variable stores the instance of an object and so is used to access it. For example, for a class named “Student”,
Student obj;
“obj” will serve as a reference variable. But this variable doesn’t point to an object of the class “Student” yet. It would need to be instantiated, which is done as follows:
obj = new Student();
Now this “obj” can be used to access an object of the “Student” class.
16)
All Objects of the same class have the same methods.
Since objects are the instance of a class, different objects of the same class share the same set of methods in the class.
Each new instance of an object will have a different location in the memory
Whenever you instantiate an object, it is provided with a memory and a different memory address. If they were not provided with different memory locations, they could be used for illegal access to previous objects or even override their data.
17) Student
‘s’ is a reference variable and is being initialized with an instance of the class “Student”, which makes it a reference variable of type “Student”.
18) C
public Employee(String name, double salary){
this.name = name;
this.salary = salary;
}
“this” operator is used to refernece the current object of the class, which means when you use “this” with a variable name, it would point to that variable of the “class”, so by the statement,
this.name = name;
we are assigning the “name” variable of the class with the “name” variable passed to the constructor.
20) True
To be honest it depends, if you don’t have a constructor of your own, the default constructor will assign the default values to the different fields. Like, an integer variable will be assigned with value ‘0’, a String variable with “null”. etc.