To answer this question, let's go through the program step by step:
- The program defines a class named "type".
- Inside the class, there is an integer variable "num" initialized with the value 12.
- The class also contains two methods - "setnum" and "getnum".
- The "setnum" method takes an input parameter "num" and assigns it to the same-named instance variable "num" of the class.
- The "getnum" method simply returns the value of the instance variable "num".
- In the main method, an object of the class "type" is created using the "new" keyword and assigned to the variable "obj".
- The "setnum" method of the "obj" object is called with the argument 10, which should set the value of the instance variable "num" to 10.
- Finally, the "getnum" method of the "obj" object is called, and the result is printed.
Based on the above explanation, the correct answer is B) 12.
The reason is that the "setnum" method parameter has the same name as the instance variable "num". So when the "num" parameter is assigned to itself using num=num;
, it creates a conflict within the method, and the instance variable "num" is not updated. As a result, the value of "num" remains 12, which is the initial value assigned to it. Therefore, when the "getnum" method is called, it returns the value 12.