Select chapter


Sponsored Links



      Submit a Question Submit Paper

0 Says Important
0 People Like it
RateToughnessemptystaremptystaremptystaremptystaremptystar
1
What is the use of Local inner classes?
Toughnessemptystaremptystaremptystaremptystaremptystar
  1. If you want to access a class only with in a given function, then you can go for local inner class.
  2. If you want to access a class only with in other class, then you can go for local inner class.
  3. both are true
  4. both are false.
Show Answer and Explanation
Share it onFacebook gmail Twitter
0 Says Important
0 People Like it
RateToughnessemptystaremptystaremptystaremptystaremptystar
Show Answer and Explanation
Share it onFacebook gmail Twitter
0 Says Important
0 People Like it
RateToughnessemptystaremptystaremptystaremptystaremptystar
Show Answer and Explanation
Share it onFacebook gmail Twitter
0 Says Important
0 People Like it
RateToughnessemptystaremptystaremptystaremptystaremptystar
Show Answer and Explanation
Share it onFacebook gmail Twitter
0 Says Important
0 People Like it
RateToughnessemptystaremptystaremptystaremptystaremptystar
Show Answer and Explanation
Share it onFacebook gmail Twitter
0 Says Important
0 People Like it
RateToughnessemptystaremptystaremptystaremptystaremptystar
6
Example program for using anonymous inner classes:
What is the output of below Java program?
interface MyInterface{
        public void test();
}
class Bahar{
        public void fun(){
                new MyInterface() {
                        @Override
                        public void test() {
                                System.out.println("got it");
                        }
                }.test();
        }
}
public class NestedDemo2 {
        public static void main(String[] args) {
                Bahar b = new Bahar();
                b.fun();
        }
}
Toughnessemptystaremptystaremptystaremptystaremptystar
  1. No error no output
  2. compile time error in function fun() of Bahar class. You can't implement an interface like this with in a function.
  3. got it
  4. compile time error in function fun(), you can't call test() function after implementing  MyInterface immediately.
Show Answer and Explanation
Share it onFacebook gmail Twitter
0 Says Important
0 People Like it
RateToughnessemptystaremptystaremptystaremptystaremptystar
Show Answer and Explanation
Share it onFacebook gmail Twitter
0 Says Important
0 People Like it
RateToughnessemptystaremptystaremptystaremptystaremptystar
8
Example for immutable class and immutable variable:
What is the output of below java program?
final class Immutable{
        int i=10;
        public void disp(){
                System.out.println(i);
        }
}
public class ImmutableDemo {
        public static void main(String[] args) {
                final Immutable im = new Immutable();
                im.i = 20;
                im.disp();
        }
}
Toughnessemptystaremptystaremptystaremptystaremptystar
  1. Compile time error in main function as we can't create object for immutable(final) classes.
  2. Compile time error in main function, in the line im.i=20. We can't change the value of variable "i" of class Immutable because im is a final variable.
  3. Compile time error in main function, in the line im.i=20. We can't change the value of variable "i" of class Immutable because Immutable is a final class, we can't change its values.
  4. it prints 20.
Show Answer and Explanation
Share it onFacebook gmail Twitter
0 Says Important
0 People Like it
RateToughnessemptystaremptystaremptystaremptystaremptystar
9
Example for final variables in java:
What is the output of below java program?
final class Immutable{
        int i=10;
        public void disp(){
                System.out.println(i);
        }
}
public class ImmutableDemo {
        public static void main(String[] args) {
                final Immutable im = new Immutable();
                Immutable j = new Immutable();
                j.i = 100;
                im = j;
                im.disp();
        }
}
Toughnessemptystaremptystaremptystaremptystaremptystar
  1. Compile time error in main function as we can't create object for immutable(final) classes.
  2. Compile time error in main function, in the line im.i=20. We can't change the value of variable "i" of class Immutable because im is a final variable.
  3. Compile time error in main function, in the line im=j.  Because im is a final variable, once we assign some value to it, we can't change it.
  4. it prints 20.
Show Answer and Explanation
Share it onFacebook gmail Twitter
0 Says Important
0 People Like it
RateToughnessemptystaremptystaremptystaremptystaremptystar
Show Answer and Explanation
Share it onFacebook gmail Twitter
0 Says Important
0 People Like it
RateToughnessemptystaremptystaremptystaremptystaremptystar
11
Is this a correct way of creating immutable object?
final class Immutable{
        private int i=10;
        public void disp(){
                System.out.println(i);
        }
}
public class ImmutableDemo {
        public static void main(String[] args) {
                Immutable im = new Immutable();
                im.disp();
        }
}
Toughnessemptystaremptystaremptystaremptystaremptystar
  1. Yes, because even here also no body can change value of "i". So im will become immutable object.
  2. No, though it looks like im is an immutable object, still there is a chance that people can change the value of i, if we expose a public function from this class, which can set the value of i. Other wise assuming that if there are no public setter methods to change the value of i, then this is fine.
  3. No, its not an immutable object example
  4. none
Show Answer and Explanation
Share it onFacebook gmail Twitter
0 Says Important
0 People Like it
RateToughnessemptystaremptystaremptystaremptystaremptystar
12
Where all anonymous inner classes can be used with?
Toughnessemptystaremptystaremptystaremptystaremptystar
  1. You can use anonymous inner classes when you are implementing an interface.
  2. You can use anonymous inner class with an abstract class.
  3. both are true
  4. none
Show Answer and Explanation
Share it onFacebook gmail Twitter
0 Says Important
0 People Like it
RateToughnessemptystaremptystaremptystaremptystaremptystar
Show Answer and Explanation
Share it onFacebook gmail Twitter
0 Says Important
0 People Like it
RateToughnessemptystaremptystaremptystaremptystaremptystar
14
How to create a singleton class in Java?
Toughnessemptystaremptystaremptystaremptystaremptystar
  1. Don't expose public constructors for your class. Then use public static method to create an object for your class and return it. Note: Make sure that you are creating object only once in this method.
  2. public class MySingleTon {
         
        private static MySingleTon myObj;
        /**
         * Create private constructor
         */
        private MySingleTon(){
             
        }
        /**
         * Create a static method to get instance.
         */
        public static MySingleTon getInstance(){
            if(myObj == null){
                myObj = new MySingleTon();
            }
            return myObj;
        }
         
        public void getSomeThing(){
            // do something here
            System.out.println("I am here....");
        }
         
        public static void main(String a[]){
            MySingleTon st = MySingleTon.getInstance();
            st.getSomeThing();
        }
    }
    
  3. Option 1 is wrong. We can create singleton class by making constructors public also.
  4. option 1 and option 2 are correct.
Show Answer and Explanation
Share it onFacebook gmail Twitter
0 Says Important
0 People Like it
RateToughnessemptystaremptystaremptystaremptystaremptystar
Show Answer and Explanation
Share it onFacebook gmail Twitter
0 Says Important
0 People Like it
RateToughnessemptystaremptystaremptystaremptystaremptystar
Experienced interview question

16
If data(or states) of an object can't be changed once set, then that object is called as?
Toughnessemptystaremptystaremptystaremptystaremptystar
  1. Final class (or Immutable class)
  2. final variable
  3. Immutable object
  4. none
Show Answer and Explanation
Share it onFacebook gmail Twitter
0 Says Important
0 People Like it
RateToughnessemptystaremptystaremptystaremptystaremptystar
Fresher interview question

17
What is an immutable object?
Toughnessemptystaremptystaremptystaremptystaremptystar
  1. An object created for an immutable class is called as immutable object.
  2. Final variable(object) created for a class, is called as immutable object.
  3. If both condition 1 and 2 is applied then it will become an immutable object.
  4. all are wrong
Show Answer and Explanation
Share it onFacebook gmail Twitter
0 Says Important
0 People Like it
RateToughnessemptystaremptystaremptystaremptystaremptystar
Fresher interview question

18
Which of the below classes will create immutable object?
Toughnessemptystaremptystaremptystaremptystaremptystar
  1. String
  2. StringBuilder
  3. StringBuffer
  4. Character array
Show Answer and Explanation
Share it onFacebook gmail Twitter
0 Says Important
0 People Like it
RateToughnessemptystaremptystaremptystaremptystaremptystar
Experienced interview question

19
What is the use of anonymous inner classes in java?
Toughnessemptystaremptystaremptystaremptystaremptystar
  1. It is useful when you want to declare and instantiate a class at the same time.
  2. If you want to declare and use a class at only one place, then use anonymous inner class. i.e if we want only one object for that class definition.
  3. both are true
  4. both are false.
Show Answer and Explanation
Share it onFacebook gmail Twitter