Java Interview Questions - 2

 


Java Interview Questions

In our earlier tutorial we have discussed about few Java interview programming questions. In this tutorial we will discuss on more interview questions.


1. How to run program without main() method?

    By using static block as given below.


    public class MyTestClass {
   
        static{
            System.out.println("Hi, I'm in static block");
        }   
    }



2. Can we call constructor from another constructor?
   
    Yes, we can call by using "this" keyword as given below.

    public class MyTestClass {
   
    public MyTestClass() {       
        this(10);       
    }   
   
    MyTestClass(int i){
        System.out.println("Value : "+i);
    }
   
    public static void main(String[] args) {
        new MyTestClass();
    }   
}



3. What will be the size of HashMap if we place twice same Object in key?

    public static void main(String[] args) {
        MyTestClass obj = new MyTestClass();
        Map<MyTestClass, String> map = new HashMap<MyTestClass, String>();
        map.put(obj, "first");
        map.put(obj, "second");
        System.out.println("Size : "+map.size());       
    }   


   SIZE: 1


4. What will be the size of HashMap if we place 2 Objects of same class in key?
  
    public static void main(String[] args) {
        

        MyTestClass obj = new MyTestClass();
        MyTestClass obj1 = new MyTestClass();
        Map<MyTestClass, String> map = new HashMap<MyTestClass, String>();
        map.put(obj, "first");
        map.put(obj1, "second");
       

        System.out.println("Size : "+map.size());      
    }

  
    SIZE: 2


 5. How to call parametrized method using java reflection?

    Please refer to Java Reflection post for set of reflection related questions and answers.

6. Can we have try block without catch block?
  
    Yes, we can but finally block should be there. Below are the combination of valid and invalid blocks in exception handling.
  
    VALID:
  
    try{
        ....
    }catch(Exception ex){
        ....
    }
    
    try{
        ....
    }finally{
        ....
    }
    
    try{
        ....
    }catch(Exception ex){
        ....
    }finally{
        ....
    }
    
    try{
        ....

    }catch(Exception ex){
        ....
    }catch(Exception ex){
        ....
    }finally{
        ....
    }


    INVALID:
  
    try{
        ....
    }




7. What will be the output of this code?
   
    public class MyTestClass {

        public static void main(String[] args) {
            System.out.println(new MyTestClass().myMethod());
        }
        
        public int myMethod(){
            try{
                int tmp = 5/0;
                return 5;
            }catch (Exception e) {
                return 6;            
            }finally{
                return 10;
            }
        }
    }


    OUTPUT: 10

    method myMethod() will return 10 irrelevant to try or catch blocks. Finally block will be executed always and will return 10.



8. Can we able to override static or private method?

    No, we can not override static or private methods.

9. Will this piece of code work or throws exception?
   
    It will give "null" output, it won't throw any exception.

    public static void main(String[] args) {
      

        HashMap hm = new HashMap();
        hm.put(null,null);
       

        System.out.println(hm.get(null));
    }


    OUTPUT: null
 

10. What is a transient variable?

    If we say in one line, Transient variables will not be serialized.
    For example when we serialize any Object if we need to serialize only few variables and rest should not be serialized then we can use transient keyword to avoid serialization on those variables.

11. Default value assigned to Boolean variable?

    false.

12. Abstract class can be final ?

    No, Class cannot be both final and abstract.

13. Abstract class should have abstract methods?

    No, its not compulsory to have abstract methods. But if there are any abstract method then class must be abstract.

14. Abstract class can have variables?

    Yes can have.

15. Interface can have method definition?

    No, interface can have only method declaration.

16. Interface can have variables?

    Yes, by default it will be static.

17. Interface can be abstract?

    Yes, but we can't have any method definitions.
      

18. How this and super keywords are used?

    this is used to refer to the current object instance and variables.
    super is used to refer to the variables and methods of the superclass of the current object instance.


19. How to Synchronize List, Map and Set?

    By using Collections util class we can Synchronize as given below,

    List<String> list = new ArrayList<String>();
     Set<String> set = new HashSet<String>();
     Map<String, String> map = new HashMap<String, String>();
   
     list = Collections.synchronizedList(list);
     set = Collections.synchronizedSet(set);
     map = Collections.synchronizedMap(map);


20. Why java is not a 100% OOP's?

    Since we have primitive data-types (int, float, double) than complete Objects. Hence we can say it as not 100% OOP's.




 




No comments:
Write comments