Java Interview

JAVA 

========================= 

1. What is hashcode?

Ans - 

1. hashCode() is a method of Object class.

2. hashCode() method returns a unique / integer values to identify object uniquely. 

3. If two object are equal by equals() method then there hashcode return by hashCode() method should be same.

4. We can override hashCode() method.

 2. What is Object class?

Ans - 

1. Object class is parent class in java.

2. Object class can have below methods

MethodDescription
1. public final Class getClass()returns the Class class object of this object. The Class class can further be used to get the metadata of this class.
2. public int hashCode()returns the hashcode number for this object.
3. public boolean equals(Object obj)compares the given object to this object.
4. protected Object clone() throws CloneNotSupportedExceptioncreates and returns the exact copy (clone) of this object.
5. public String toString()returns the string representation of this object.
6. public final void notify()wakes up single thread, waiting on this object's monitor.
7. public final void notifyAll()wakes up all the threads, waiting on this object's monitor.
8. public final void wait(long timeout)throws InterruptedExceptioncauses the current thread to wait for the specified milliseconds, until another thread notifies (invokes notify() or notifyAll() method).
9. public final void wait(long timeout,int nanos)throws InterruptedExceptioncauses the current thread to wait for the specified milliseconds and nanoseconds, until another thread notifies (invokes notify() or notifyAll() method).
10. public final void wait()throws InterruptedExceptioncauses the current thread to wait, until another thread notifies (invokes notify() or notifyAll() method).
11. protected void finalize()throws Throwableis invoked by the garbage collector before object is being garbage collected.

 3. Which method override of Object class?

Ans - 

1. clone(), equals(), toString(), finalize() and hashcode()


 4. Which equals() method?

Ans -

1. equals() method of Object class method.

2. equals() method compare the original content of the string.

3. It compare the values of string for equality.


5. What is == operator?

Ans -

1. == operator compare references not values.


6. What is compareTo() method?

Ans -

1. compareTo() method compare values and returns an integer.

2. If the values compare less than, equal or Gretter than


7. Which collection prevent insertion order?

Ans -

1. ArrayList(), LinkedList(), LinkedHashSet() and LinkedHashMap().


8. How to sort list?

Ans -

1. Using Collections.sort() method


9. How to sort list?

Ans -

1. Using Collections.sort() method


10. HashMap is not order preserve then how to print in ascending order?

Ans -

1. hashMap<Integer, String> hm = new hashMap<>();

                hm.put(1,"A");

                hm.put(3,"C");

                hm.put(2,"B");

                hm.put(4,"D");

                Map<Integer, String> m = new TreeMap<>(hm);

                System.out.println(m);


11. How to convert hashMap to ArrayList?

Ans -

1. hashMap<Integer, String> hm = new hashMap<>();

                hm.put(1,"A");

                hm.put(3,"C");

                hm.put(2,"B");

                hm.put(4,"D");

        Set s = hm.entrySet();

        ArrayList<> a = new ArrayList<>(s);

        System.out.println(a);

12. Char ch[] = {'a','b','c','a','b'}

output should be =  a,b,c  using Collection

Ans -

1.  Char ch[] = {'a','b','c','a','b'}

    HashSet<> hs = new HashSet<>();

    for(int i=0; i< ch.length; i++){

        hs.add(ch[i]);

    }

 System.out.println(hs);

-----------------------------------------------------

reverse output ------------------

        ArrayList<> a = new ArrayList<>(hs);

        Collections.reverse(a)

        System.out.println(a);

13. hashMap<Integer, Integer> hm = new hashMap<>();  Is Integer Allowed

Ans -

1.  Yes Allowed


14. hashMap<intInteger> hm = new hashMap<>();  Is int Allowed

Ans -

1.  No, Not Allowed, Only Wrapper classes and Custom Classes Allowed

===========================================================

SPRING, SPRING MVC, SPRING BOOT

===========================================================


1. What are the components/modules in spring?

Ans -

1. Core Container (IoC), Test, AOP, Spring MVC, DAO etc.


2. What is controller in spring MVC?

Ans -

1. DispatcherServlet class works as front Controller.

2. It is responsible to manage the flow of the spring MVC application.


No comments:

Post a Comment