Wednesday, February 1, 2012

Puzzle code of Packages in Java

Q1)Output of the program

//Package body.
package mypack;
public class pack
{
 public void hello()
 {
   System.out.println("Hello Package");
 };
};

//Program
import mypack.*;
class main
{
 public static void main(String args[])
 {
    pack p=new pack();
    p.hello();
 }
}
Output:-Hello Package
Discussion:-Here mypack package is imported in main class.So using pack class object we can access the property of pack class.

Q2)Output of the program

//Package body..
package mypack;
private class pack
{
 public void hello()
 {
   System.out.println("Hello Package");
 };
};


//Program
import mypack.*;
class main
{
 public static void main(String args[])
 {
    pack p=new pack();
    p.hello();
 }
}
Output:-compile time error
Discussion:-In package the pack class is declared as private class,so outside from package it is not possible to access the class property.


Q3)Output of the program

//Package body..
package mypack;
public class pack
{
 private void fun()
 {
   System.out.println("I am fun");
 };
 public void hello()
 {
   fun();
 };
};

//Function Body
import mypack.*;
class main
{
 public static void main(String args[])
 {
    pack p=new pack();
    p.hello();
 }
}
Output:-I am fun
Discussion:-In package body the fun class is declared as private class.So from outside of package it is not possible to access the methode.
           
           
Q4)Output of the program

//Package body
package mypack;
public class pack
{
 protected void hello()
 {
    System.out.println("I am protected function");
 };
};           

//Program body..
import mypack.*;
class main
{
 public static void main(String args[])
 {
    pack p=new pack();
    p.hello();
 };
};

Output:-Compile time error.
Discussion:-hello() methode is declared protectedly in package .So from outside of package,it is not possible to invoke hello function.

Q5)Output of the program

//Package body.
package mypack;
public class pack
{
 public int a=100;
 public int b=200;
};

//Program body
import mypack.*;
class main
{
 public static void main(String args[])
 {
    pack p=new pack();
    System.out.println(p.a);
    System.out.println(p.b);
 };
};
Output:-100 200
Discussion:-Here a and b declared in public section in package.So it is possible to access from outside of the package.


Q6)Output of the program
//Package body
package mypack;
public class pack
{

};
public class pack1
{

};

//Program body
import mypack.*;
class main
{
 public static void main(String args[])
 {
   
 };
 Output:-compile error
 Discussion:-More than one public class is declared in the package.But it is not possible,In one package only one public class is allowed to declare.


 Q7)Output of the program

 //Package 1 body...

package mypack;
public class pack
{
 public void print()
  {
   System.out.println("I am pack class");
  };
};
};

//Package 2 body..
package mypack;
public class add
{
  public void print()
  {
    System.out.println("I am add class");
  };
};

//Program body..
import mypack.*;
import mypack.add.*;

class main
{
 public static void main(String args[])
 {
    pack p=new pack();
    p.print();   
   
    add s=new add();
    s.print();
 };
};

Output:-I am pack class
        I am add class
Discussion:-Here both pack and add class is imported.So both the print function are possible to invoke from main function.


Q8)Output of the program.

//Package body..
package mypack;
class hide
{
 public void printhide()
  {
    System.out.println("I am hide class");
  };
};

public class pack extends hide
{

 public void print()
  {
   System.out.println("I am pack class");
  };
 
};

//Program body..
import mypack.*;
class main
{
 public static void main(String args[])
 {
    pack p=new pack();
    p.print();   
    p.printhide();   
 };
};
Output:-I am pack class   
        I am hide class
Discussion:-Though the hide class is declared privately,but the methode of hide class is possible to invoke from pack class.
             Because pack class is inherited from hide class.
           
Q9)Output of the program

//Package body..

package mypack;
public class pack
{
 private void printhide()
  {
    System.out.println("I am hide class");
  };
};
           

//Program body..
import mypack.*;
class main
{
 public static void main(String args[])
 {
    pack p=new pack();
    p.printhide();   
 };
};       
   
Output:-Run time error.
Discussion:-Though the pack class is declared publicly but the printhide function is declared privately.So outside from package it is not possible to
            invoke printhide() methode.
   
Q10)Output of the program

//Package body..
package mypack;
public class pack
{
 public static void printhide()
  {
    System.out.println("I am hide class");
  };
};

//Program body..
import mypack.*;
class main
{
 public static void main(String args[])
 {
   pack.printhide();   
 };
};
Output:-I am hide class
Discussion:-Here printhide() methode is declared as static function.So without any object of pack class,it is possible to invoke printhide() methode.

Q11)Output of the program

import java.lang.*;
class main
{
 public static void main(String args[])
 {
   System.out.println(Math.sqrt(5));
 };
};

Output:-2.2360
Discussion:-Here lang system package is imported.So we can use sqrt() function of Math class.

Q12)Output of the program
import java.util.*;
class main
{
 public static void main(String args[])
 {
    Date d=new java.util.Date();
    System.out.println(d.getHours());
 };
};
Output:-System hour.
Discussion:-In this program util package is imported,so we can print system date using getHours methode of Date class.

Q13)Output of the program

//Package body.
package mypack;
public class pack
{
 public String stringadd(String str1,String str2)
  {
    String str=str1 + str2;
    return str;
  };
};

//Program body.
import mypack.*;
class main
{
 public static void main(String args[])
 {
   pack p=new pack();
   System.out.println(p.stringadd("sourav","kayal"));
 };
};

Output:-souravkayal
Discussion:-stringadd function is taking to string type object and after adding them it is returning one string object.



   


No comments:

Post a Comment