Wednesday, February 1, 2012

Puzzle code of Constructor and Destructor in Java

Q1)Output of the program
class a
{
 a()
 {
  System.out.println("I am constructor");
 }
 a(int n)
 {
  System.out.println("I am Paramiterised constructor");
 }
 a(a p)
 {

 }
}
class main
{
  public static void main(String args[])
   {
     a t=new a();
   
   }
}
Output:-I am constructor
Discussion:-Here by default the default constructor will exhicute.

Q2)output of the program
class a
{
 private a()
 {
  System.out.println("I am constructor");
 }
}

class main
{
  public static void main(String args[])
   {
     a t=new a();
   }
}
Output:-Compile time error.
Discussion:-Here default constructor is declared publicly.So the statement a t=new a();
            cannot able to exhicute the default constructor.

Q3)Output of the program
class a
{
 a()
 {
  System.out.println("Default \n");
 }
 a(int n)
 {
    System.out.println(n + "\n");   
 }
 a(a t)
 {
    System.out.println("Copy");
 }

}

class main
{
  public static void main(String args[])
   {
     a t=new a();
     a p=new a(10);
     a r=new a(p);
   }
}
Output:-Default
        10
        Copy
Discussion:-Here the statement
            a t=new a();     Call the default constructor.
            a p=new a(10);   Call the paramiterised constructor.
            a r=new a(p);    Call the copy constructor.
           

           
           
Q4)Output of the program
class base
{
 base()
 {
  System.out.println("Base constructor");
 };
};
class derive extends base
{
 derive()
 {
  System.out.println("Derive constructor");
 };
};

class main
{
  public static void main(String args[])
   {
     derive d=new derive();
    
   }
}        
Output:-Base constructor
        Derive constructor
Discussion:-Here derive class is extended from base class.
            So base class constructor is invoke first.Then derive
            class constructor.

Q5)Output of the program
class base
{
 base()
 {
  System.out.println("Base constructor");
 };
};

class derive1 extends base
{
 derive1()
 {
  System.out.println("Derive constructor1");
 };
};

class derive2 extends derive1
{
 derive2()
 {
  System.out.println("Derive constructor2");
 };
};

class main
{
  public static void main(String args[])
   {
     derive2 d=new derive2();
        
   }
}
Output:-Base constructor           
        Derive constructor1
        Derive constructor2
Discussion:-It is a example of multilevel inheritence.
            According to inheritence of base class and derive class the constructor will call.
           
Q5)Output of the program
class base
{
 private int a,b;
 public base()
       {
         this.a=10;
         this.b=10;
         System.out.println(this.a);
         System.out.println(this.b);
       };
};


class main
{
  public static void main(String args[])
   {
    base b=new base();
   }
}
Output:-10 10
Discussion:-Here this pointer points the curent object property.

Q6)Output of the program
class base
{
 private int a,b;
 public base()
       {
         this.a=10;
         this.b=10;
       };
       boolean check(base b,base c)
       {
        if(b.a==c.a)
        return true;
        else
        return false;
       };
};

class main
{
  public static void main(String args[])
   {
    base b=new base();
    base c=new base();
    boolean t=b.check(b,c);
    System.out.println(t);
   };
};
Output:-true
Discussion:-Here check function is taking tow object and after checking returning a boolean type
            value.
           

Q7)Output of the program
class base
{
  base()
   {
    System.out.println("I am base constructor");
   };
};
class derive extends base
{
 derive()
 {
    super();
    System.out.println("I am derive constructor");
 };
};
class main
{
  public static void main(String args[])
   {
     derive d=new derive();
   
   };
};
Output:-I am base constructor           
        I am derive constructor
Discussion:-Here super() calls, it's immediate super class.
            So at first base class constructor is exhicuted then extended class.

Q8) Output of the program
class base
{
   protected base()
   {
    System.out.println("Hello Java");
   };
};

class main
{
  public static void main(String args[])
   {
     base b=new base();
   };
};
Output:-Hello Java
Discussion:-Here base constructor is declared protectedly.But the statement base b=new base();
            is able to call the protected constructed.

           
Q9)Output of the program.
class base
{
   protected base(String str)
   {
    System.out.println("Hello "+ str);
   };
};

class main
{
  public static void main(String args[])
   {
     base b=new base("Sourav");
   };
};
Output:-Hello Sourav
Discussion:-Here base() is a paramiterized constructor and taking one string object.
            And the object is concatinated with string Hello.

Q10)Output of the program
class base
{
   base()
   {
    System.out.println("Base class");
   };
};
class derive extends base
{
 derive()
 {
    base();
    System.out.println("Derive class");
 }
}
class main
{
  public static void main(String args[])
   {
     derive d=new derive();
   };
};
Output:-Compile time error.
Discussion:-We know ,Constructor need not call.It call automatically when the object is created.

            

No comments:

Post a Comment