www.bullraider.com

Frequenty Asked Interview Question In Java

AddThis Social Bookmark Button

Frequenty Asked Interview Question In Java

Java Class Loader ,Variables and Blocks Related Questions

  1. What is package-private ?
     Package-private is a different name of default access specifier.
  2. What is Class Loader in java ?

    Java Runtime Environment that dynamically loads Java classes into the Java Virtual Machine on demand .

    There are three types of class loaders. There are

     Bootstrap class loader

                 The bootstrap class loader loads the core Java libraries[5] located in the {JAVA_HOME}/lib directory
      Extensions class loader

                 The extensions class loader loads the code in the extensions directories {JAVA_HOME}/lib/ext directory
       System Class  Path Loader

                  The system class loader loads code found on java.class.path, which maps to the system CLASSPATH variable

  3. What is the order of execution of Class Loader in java?
    Java applications  involve three classloaders – Bootstrap, Extensions and System-Classpath classloaders
    Bootstrap classloader is the parent of all classloaders ,extensions Classloader is the immediate child of Bootstrap classloader and System-Classpath classloader is the immediate child of Extensions classloader. Generally  Order of execution happens from sub-sequent children to parent. 
    So order of execution is starts from System-classpath-loader then extension then Bootstrap Loader.
  4. What is differenence between ClassNotFoundException and NoClassDefFoundError ?
    ClassNotFoundException is generated by a call to Class.forName() with a String that contains a class not available on the bundle's class path. For example when we are dynamically want to load the class using  Class.forName("oracle.jdbc.driver.OracleDriver"); if that class is not aviailable in runtion, it'll throw ClassNotFoundException
    NoClassDefFoundError is thrown if the Java Virtual Machine or a ClassLoader instance tries to load in the definition of a class and that class or its dependancies are not available  in run time. For example if Class x extends Class y and Class y is extends  class Z .When class loader tries to load class x if any of the depenacies is missing it'll give
  5. If we are using importing a class e.g java.util.ArrayList , and we are not using that class in side our program, does class loader will load the class?
    No, it won't
  6. Local variable, instance variable, method and object, amonng of these which one will store in heap and which one will store in stack?
    local variable and method will store in stack
    instance variable and object will store in heap
  7. Emp e=new Employee(), where the "e" and "new Employee" will store?
    "e" will store in stack and Emploee() ( or the object )  will store in heap.
  8. Where we can can declare local variable ,where instance variable and where static variable?
    Local : inside mehod, constructor, and block
    instance: outside method, constructor and block
    static: outside method, constructor and block
  9. Can I write like this?
    void methodA()
    {
    int a;
    System.out.println("I am in methodA");
    }
    yes, ( a stupid question for smart people )
  10. Can I write like this?
    void methodA()
    {
    int a;
    System.out.println("I am in methodA " + (a+5));
    }
    no, because as long as we are not using local variable it won't give any error, but we should initialise it before using it(using of a).
  11. Can we declare a local variable with in static block, like below?
    public class TestVariable {
            static{
             int i=9;
             System.out.println("print  "+i);
        }
    yes
  12. Can we use an instance variable with in static block ?
    public class TestVariable {
        int i=9;
            static{
                 System.out.println("print  "+i);
        }

    No, because instance variable alwasys accessable wth a reference type.
  13. Can we write like this?
    public class TestVariable {
                    static{
             static int i=9;
             System.out.println("print  "+i);
        }
    no, static variable always declares out side of block?
  14. Can I write like this?

    public class TestVariable {
        int y=9;
        static{
             System.out.println("print  "+new TestVariable().y);
        }
    }
    yes
  15. If I am writing static and non static block in my program like below, what is output?
    public class TestBlock {

        static{

            System.out.println("I am in static block");
        }

        {
            System.out.println("I am in non static block");
        }
        public static void main(String a[]){
            TestBlock block=new TestBlock();

        }
    }
    Ans: I am in static block
    I am in non static block
  16. What will be output for this program?
    public class TestBlock {

        static{

            System.out.println("I am in static1 block");
        }

        {
            System.out.println("I am in non static block");
        }

        static{

            System.out.println("I am in static2 block");
        }
        public static void main(String a[]){
            TestBlock block=new TestBlock();
      }
    }
    Ans
    I am in static1 block
    I am in static2 block
    I am in non static block
  17. What will be the out put for folowing code?

    public class TestBlock {

        static{

            System.out.println("I am in static1 block");
        }

        {
            System.out.println("I am in non static block");
        }


        public TestBlock() {
            System.out.println("I am in non argumented constructor");
        }
        
        public TestBlock(String s) {
            System.out.println("I am in argumented constructor  "+s);
        }

        public static void main(String a[]){
            TestBlock block=new TestBlock("hello");

        }
    }
    I am in static1 block
    I am in non static block
    I am in argumented constructor  hello
  18. Can I use instance variable in static variable like below?
    public class TestBlock {
        int z=98;
        static{
            System.out.println("I am in static block"+new TestBlock().z);
        }
        public static void main(String a[]){
            TestBlock block=new TestBlock();
        }
    }
    yes, you can execute this program, out put will
    I am in static block98
Powered by Bullraider.com