Page 285 - CA_Blue( J )_Class10
P. 285

3.  What is the difference between instance variable and static variable?
                   Ans.
                                       Instance Variable                                 Static Variable
                        Different memory  is allocated whenever  a variable is  Only one memory is allocated for all the variables created.
                        created.
                        Exist as long as the object they belong to exists.  Exist as long as the class exists.
                        Values of these  variables  depend on  the state  of the  Values of these variables will be the same for all instances of the
                        object.                                      class.
                        Declaration:                                 Declaration:
                        class sum                                    class sum
                        {int a, b, s;}                               {static int a, b, s;}

                    4.  Which keyword is used if the instance variable name and the local variable name are the same?
                   Ans.  If the instance variable and local variable have the same name, then the keyword this is used.
                    5.  What is meant by the keyword this?
                   Ans.  This keyword refers to the current object. It is used to eradicate the confusion between instance variables and parameters passed
                       in the method when both the variables have the same name.
                    6.  What is instantiation?
                   Ans.  Creating an object of a class is called instantiation.
                    7.  Name the three types of access specifiers.
                   Ans.  The three types of access specifiers are public, private and protected.
                    8.  Consider the following class:
                      class sum
                      {
                          int n1, n2;
                          int s, d;
                          void assign(int n1, int n2)
                          {
                              n1=n1; n2=n2;
                          }
                          void display()
                          {
                            s=n1+n2;
                            System.out.println("Sum : "+s);
                            d=n1-n2;
                             System.out.println("Difference : "+d);
                         }
                         public static void main()
                         {   sum ob = new sum();
                             ob.assign();
                             display();
                         }
                      }
                       Identify the errors in the preceding code and correct them.
                   Ans.  class sum
                       {
                           int n1, n2;
                           int s, d;
                           void assign(int n1, int n2) {
                               this.n1=n1;
                               this.n2=n2;
                           }





                                                                                                                       283
                                                                                      Class as the Basis of all Computation  283
   280   281   282   283   284   285   286   287   288   289   290