Wednesday, February 1, 2012

Puzzle code of class and object in Java

Q1)Find the output
class c
{
    public void hello()
     {
        private int a=1;
        protected int b=2;
        public int c=3;
            System.out.println(a+b+c);
     }
}
class d
{
 public static void main(String args[])
 {
   
    c t=new c();
    t.hello();   
 }
}
Output:-Compile error
Discussion:-we cannot initilize the private,public and protected data member directly.

Q2)output of the program
class d
 {
      public static void main (String[] a1)
      {
       System.out.print(a1[1] + a1[2] + a1[3]);
      }
}
Output:-ArrayIndexOutOfBounds exception in run time
Discussion:-array index outof bounds exception only till a1[2] is allowed.

Q4)Output of the program
class d

    public String name="sourav";
    public String getname()
     {
        return name;
     }
     public static void main(String args[])
      {
        d t=new d();
        String s=t.getname();
        System.out.println(s);
      };
     
};
Ans:-sourav
Discussion:-Here getname function return a string object.

Q5)Output of the program
class d

    public d()
            {
              System.out.println("I am constructor function");
            }
             
};
class a extends d{
}
Ans:-program will compile,But not run.
Discussion:-No main() methode is declared ,so it will show run time error.

Q6)Output of the program
abstract class abc

    abc()
    {
      System.out.println("I am constructor function");
    }
             
};
class d
{
  abc t=new abc();
}
Ans:-Compile time error.
Discussion:-Here abc is a abstract class.And abstract class cannot be instantiated.

Q7)Output of the program
final class abc

    public int a=100;
             
};
class d extends abc
{
  public static void main(String args[])
  {
    abc t=new abc();
  }
}
Ans:-compile time error
Discussion:-Here abc is a final class.We cannot inherit any class from final class.

Q8)Output of the program
class abc

    static void hello()
     {
        System.out.println("Hello world");
     };
};
class d
{
  public static void main(String args[])
  {
    abc.hello();
  }
}
Ans:-Hello world
Discussion:-Here hello function is declared as static function.So without any object with classname we can access
            the static function.
Q9)Output of the program
class d
{
    String cal(String cal)
    {
      return cal;
    };
    void cal()
    {
      System.out.println("cal");
    };
   
    public static void main(String args[])
    {
     d t=new d();
     String cal=t.cal("cal");
     System.out.println(cal.toUpperCase());
     t.cal();
    };
};
Ans:-CAL
     cal
Discussion:-Here toUppercase is a function of string class.And convert the string into uppercase.

Q10)Output of the program
class head
{
   public int a=100;
   static void hello()
   {
     System.out.println(a);
   };

};
class d
{
  public static void main(String args[])
   {
    head h=new head();
    h.hello();
   }
Ans:-Compile time error
Discussion:-Static function cannot handle not static data.Here a is a non static data.


Q11)Output of the program
class head
{
   public static final int a=100;
   static void hello()
   {
     System.out.println(++a);
   };

};
class d
{
  public static void main(String args[])
   {
    head h=new head();
    h.hello();
   }
}
Ans:-Compile time error.
Discussion:-Here a is a final variable.And in hello function it is tryed to modify.


Q12)Output of the program
class d{
 public static void main(String a[])    
 {
  int i1=9;
  int i2;
  if(i1>3)
   {        
      i2=8;
   }
   System.out.println(i2);
 }
}
Ans:-Compilation error,i2 might not initialized.
Discussion:-Here within if condition the expression i1>3 return false.So i2 will not initialized.


Q13)Output of the program
class d{
public static void main(String args[])
  {
   int a = 1;
   a += ++a + a++;
   System.out.print(a);
  }
}
Ans:-5
Discussion:-The expression a+=++a + a++;
            will evaluate like
            ++a=2
            a++=2
            ++a + a++=4;
            a=1+4;
            a=5;
           
           
Q14)Output of the program
class string
{
    String cal(String s1,String s2)
     {
        String s=s1+s2;
        return s;
       
     }   
};
class d{
public static void main(String args[])
  {
    string p=new string();
    System.out.println(p.cal("c","c++"));
  }
}
Ans:-cc++
Discussion:-Here cal function is taking two string type object and after concatinating it is returning one string type object.


Q15)Output of the program
static class abc
{
    void hello()
     {
     
     }
}
class d{
public static void main(String args[])
  {
     abc t=new abc();
     t.hello();
  }
}
Ans:-Compile time error
Discussion:-We cannot define a class as static.









No comments:

Post a Comment