Select chapter



basics (7.papers)
constructors (1.papers)
packages (1.papers)
inheritance (7.papers)
misc (3.papers)
exceptions (5.papers)
threads (4.papers)
overloading (1.papers)
jsp (3.papers)
servlets (9.papers)
jdbc (2.papers)
util (1.papers)
xml (2.papers)
IO stream (3.papers)
Hide

Sponsored Links


  

java interview questions and answers

Show Answer
Share Facebook gmail Twitter
2
What is the use of static-nested class?
  1. If I want to access private members of class B in class A directly, then it is possible only if you declare class A as static nested class of class B.
  2. static nested class is just like any other outer class, so, just for packaging convenience we can club static nested classes into one outer class for readability purpose.
  3. both option 1 and 2 are true.
  4. none
Show Answer
Share Facebook gmail Twitter
3
which of the below statements are true about creating an object for nested classes?
  1. To create object for an inner class (non-static), we don't need to create object for outer class.
  2. To create object for a static nested class, we don't need to create object for outer class.
  3. It is possible to have local static nested class in a function.
  4. we can't have inner class in a function.
Show Answer
Share Facebook gmail Twitter
4
How to create object for a static nested class in java?
If OuterClass is the name of the outer class, and SClass is the static nested class in the Outer class, then how to create object for SClass?
  1. OuterClass oc = new OuterClass();
    OuterClass.SClass sc = new OuterClass.SClass();
  2. OuterClass.SClass sc = new OuterClass.SClass();
  3. OuterClass.SClass sc = OuterClass.new SClass();
  4. OuterClass oc = new OuterClass();
    OuterClass.SClass sc = new oc.SClass();
Show Answer
Share Facebook gmail Twitter
Show Answer
Share Facebook gmail Twitter
Show Answer
Share Facebook gmail Twitter
7
Example for static nested class accessing static variables:
What is the output of below Java program?
class Outer{
        static int z = 10;
        public static class NestedClass{
                public void fun(){
                        System.out.println("Nested Class fun.."+z);
                }
        }
        public void disp(){
                System.out.println("Outer class display.."+z);
        }
}
public class NestedDemo {
        public static void main(String[] args) {
                Outer o = new Outer();
                o.disp();
                Outer.NestedClass nc = new Outer.NestedClass();
                nc.fun();
        }
}
  1. compile time error in fun() of NestedClass. Static nested class can't access outer class members directly.
  2. Outer class display..10
    Nested Class fun..10
  3. compile time error in disp() of Outer class, normal functions can't access static variables directly. Only static functions can access static variables.
  4. no error no output
Show Answer
Share Facebook gmail Twitter
8
Is it possible to access instance members and static members of outer class from static nested class directly? Which of the below statements are wrong about a static nested classes?
  1. Static nested class can't access instance variables of outer class directly.
  2. Static nested class can access instance methods of outer class directly.
  3. Static nested class can access static methods of outer class directly.
  4. Static nested class can access static variables of outer class directly.
Show Answer
Share Facebook gmail Twitter
Show Answer
Share Facebook gmail Twitter
Show Answer
Share Facebook gmail Twitter
11
Which of the below statements holds false for nested classes in general?
  1. Inner class (non-static nested class) objects will exist with in the object of outer class.
  2. you can have a non-static nested class with in a static nested class.
  3. you can have a static nested class with in a non-static nested class.
  4. you can inherit one inner class into other inner class.
Show Answer
Share Facebook gmail Twitter
12
Example for variable shadowing in inner classes:
What is the output of below Java program?
class Outer{
        private int j = 10;

        public class InnerClass{
                int j = 20;
                public void fun(){
                        System.out.println("Inner Class fun.."+j);
                }
        }
        public void disp(){
                System.out.println("Outer class display.."+j);
        }
}
public class NestedDemo {
        public static void main(String[] args) {
                Outer o = new Outer();
                o.disp();
                Outer.InnerClass ic = o.new InnerClass();
                ic.fun();
        }
}
  1. Outer class display..10
    Inner Class fun..20
  2. Outer class display..10
    Inner Class fun..10
  3. Compile time error in fun() of InnerClass, you can't have same variable names in Outer class and Inner class.
  4. ambiguity because variable j is available in both inner class and outer class, so we can't predict the output.
Show Answer
Share Facebook gmail Twitter
13
Example for variable shadowing in inner classes, and accessing outer class variable from inner class:
What is the output of below Java program?
class Outer{
        private int j = 10;

        public class InnerClass{
                int j = 20;
                public void fun(){
                        System.out.println("Inner Class fun.."+j);
                        System.out.println("Inner Class fun.."+this.j);
                        System.out.println("Inner Class fun.."+Outer.this.j);
                }
        }
        public void disp(){
                System.out.println("Outer class display.."+j);
        }
}
public class NestedDemo {
        public static void main(String[] args) {
                Outer o = new Outer();
                Outer.InnerClass ic = o.new InnerClass();
                ic.fun();
        }
}
  1. Inner Class fun..20
    Inner Class fun..10
    Inner Class fun..10
  2. Compile time error in fun() of Inner class, in third print statement Outer.this.j. It is not possible to access outer class variables like this.
  3. compile time error in fun() of Inner class in second print statement, this.j . Here it is confusing statement as we don't know whether it picks outer class j or inner class j as "this" keyword here means both the classes. this is ambiguity and gives compile time error.
  4. Inner Class fun..20
    Inner Class fun..20
    Inner Class fun..10
Show Answer
Share Facebook gmail Twitter
14
Which of the below are not true about nested classes, in general?
  1. You can declare a non static nested class with in a function of outer class.
  2. You can declare a non static nested class with out any name, with in a function of outer class.
  3. You can give access modifiers like private, public, protected for a non static nested class which is declared with in a function of outer class.
  4. You can give access modifiers like private, public, protected for a non static nested class which is declared in the outer class.
Show Answer
Share Facebook gmail Twitter
15
Is it possible to access local variables of a function and members of outer class in a local inner class?
  1. A local inner class defined in a function can access members of outer class.
  2. A local inner class defined in a function can not access members of outer class.
  3. A local inner class defined a function can access all the local variables of that function.
  4. all are true.
Show Answer
Share Facebook gmail Twitter
16
Which of the below statements are true about local inner classes in java?
  1. Local inner classes can't define a static variable or static function with in it.
  2. Local inner classes defined in a static method can access only static members of outer class.
  3. Local inner classes are non-static as they have accessibility to instance members of outer classes.
  4. all are true.
Show Answer
Share Facebook gmail Twitter
17
What is true about inner classes?
  1. A local class can have static members provided if they are constant variables.
  2. you can declare a static initializer in a local class.
  3. You can declare an interface inside a block.
  4. all are true.
Show Answer
Share Facebook gmail Twitter
Show Answer
Share Facebook gmail Twitter
19
What is an anonymous inner class? And what is the use of anonymous inner classes in java?
  1. A class declared with in a class is called as anonymous inner class.
  2. A class declared with in a function is called as anonymous inner class.
  3. A class declared in a block or function with out any name is called as anonymous inner class.
  4. A static class declared in a block with out any name is called as anonymous inner class.
Show Answer
Share Facebook gmail Twitter
20
How to create object for InnerClasses (non static nested classes)?
Assume that Outer is the name of the outer class, and InnerClass is the non-static nested class (inner class) in the Outer class.
  1. Outer o = new Outer();
    Outer.InnerClass ic = new Outer.InnerClass();
  2. Outer o = new Outer();
    Outer.InnerClass ic = new o.InnerClass();
  3. Outer o = new Outer();
    Outer.InnerClass ic = o.new InnerClass();
  4. none
Show Answer
Share Facebook gmail Twitter
Read more papers :