Wednesday, January 29, 2014

Core Java Interview Questions

                   

Any java interview be it for fresher or for experienced professional will have an interview round completely focusing on Core Java. For experienced candidates this is usually the first round.
I am enlisting few of the most popular core java questions.

1.Interview questions for freshers.
2.Interview questions for experienced .


1.Interview questions for freshers.


  1. What is Class?
Ans. Class is a template. It specifies the data members and possible operations on its data  members.
  1. What is Object?
Ans. Object is an instance of class.
  1. What is an abstract class?
Ans. Abstract class is a special type of class which cannot be instantiated. The purpose of                                      
        Using abstract classes is to provide the Common functionality to all the subclasses.
  1. What is interface?
Ans. Interface is a way to achieve full abstraction in java. All the methods in  an interface do not have body.
      Any class implementing interface should fulfill the contract to implement all the methods in the interface. This behavior allows us to use Run time polymorphism to fullest.
  1. What is difference between abstract class and interface? when will you use the abstract class?
a.       Abstract classes represent partial abstraction. Interfaces represent full abstraction.
b.      The purpose of using abstract classes is to provided abstraction for a few methods and provide some common default implementation for some methods. Whereas interfaces are used when you want all the subclasses to provide their own implementation to all methods.
c.       It is always preferable to implement an interface then extending an abstract class. As once your class extend an abstract class it cannot extend any other class.

  1. A class may extend maximum of how other many classes?
Ans A class in java can extend at most one class.
  1. Does Java supports multiple inheritance?
Ans. Java does not support multiple CLASS inheritance, as it creates some major problems like Diamond problem in c++.  However Java supports Multiple interface implementation. So your class can implement multiple interfaces.
  1. What are the four main concepts of object oriented languages? Explain all four?
Ans- Four major concepts are
1.      Abstraction
2.      Encapsulation
3.      Inheritance
4.      Polymorphism
  1. What are types of  relationship between objects?
Ans-
a.       Association- Class A uses class B. This relationship is used when one object wants to use other object to perform some service for it. Association also specifies the multiplicity between objects.
Note-  There is no ownership between objects, Both have independent lifecycle.
Example- Teacher and student. A teacher can

b.      Aggregation- Class “A” has an instance of class “B” . So Aggregation is special type of association , it is directional association between object . Direction specifies which object contains other object.

 where instance of Contained class(B) can independently exist even without instance of parent class (A).

c.       Composition- Composition is a stricter form of aggregation. Where a Contained object cannot exist without container object.
Example- A line item cannot exist without Order.

  1. What is inheritance? explain with example.
Ans Inheritance is a feature in java which allows a new class(derived class) to be derived from other class(base class). There by allowing the  derived class to inherit the properties and methods of the base class. This helps improve the code reuse.
       
      11.  What is polymorphism? explain with example.
Ans. Polymorphism means, ability to behave differently in different context. In term of java
Polymorphism  is the ability of calling the  methods based on type of the object  not based on the type of reference.
In java reference of the base class can refer to the object of derived class.
Class a{
Public void fun(){
System.out.println(“ Function of class a called”);
}
}
Class b extends a{
Public void fun(){
System.out.println(“ Function of class b called”);

}
}

   // calling code
        a aobj=new b();
        b.fun();// this will call the fun method of class b
      So the method being called is based on the type of object (class b) not based on the refrence(class a);
  1. What are different types of polymorphism?
a.       Static polymorphism. Example- method  overloading
b.      Dynamic polymorphism example- method overriding.
  1. What is the base class of all the classes?
Object class is base class of all objects.
  1. What is difference between JVM, JRE and JDK?
JDK- Java development kit includes the tools, binaries and java classes  used for  
Developing, compiling and running java programs. JDK includes tools like compiler, visualvm ect.
JVM- Converts the byte codes to machine specific code, and critical functionalities like
 Memory management, garbage collection and security.
JRE- Java runtime environment is implementation of JVM, it is the platform to execute java programs. It contains jvm, java binaries and other custom binaries to execute java program. JRE does not includes development specific tools like compiler.

  1.  How Java  achieves platform independence?
  2. What is stack?
  3. What is heap?
  4. Java is call by reference or call by value?
  5. What is garbage collector?
  6.  How can you call garbage collector?
  7. What is equals function of Object class used for ?
  8. what is difference between == and equals() function?
  9. What is thread?
  10. What is difference between thread and process?
  11. What are different ways to create a thread? which is preferred way?
  12. What are the functions of object class-  wait,notify and notifyall  used for? or how will threads inter communicates?
  13. What is synchronized key word in java?
  14. What is deadlock? 
  15. what is thread starvation?
  16. What are Executor interface?
  17. Write a program to simulate producer consumer problem?
  18. What do you mean by string is immutable? 
  19. What is  String pool?
  20. Write a program to Reverse a string.
  21. Write a program to generate a Fibonacci sequence.
  22. Write a program to check if a string is java palindrome?
  23. Write a program to check if two given strings are same? do not use .equals function?
  24.  What is Collection framework in java?
  25.  What is List, Map, Set interfaces? what are there concrete implementation classes?
  26. What is difference between list and set implementations?
  27. What is difference between a list and a map?
  28.  What is difference between Hashtable and Hashmap?
  29.  What is Exception in java?

Core Java Interview questions for experienced Java developers

 

  Core Java Interview questions for experienced Java developers

 Well the interview questions for experienced java candidates vary based on the experience and the
  JD. Following is the list of  very frequently asked Core Java questions .
  1. What is class loader? 
  2. What is are different types of class loader?
  3. Can you have two instance of a singleton class?
  4. Which class loader is used when you execute main method of the your class, where does it picks searches the file?
  5.  What is static class loading & dynamic class loading, give example and difference?
  6.  Diff b/w ClassDefNotFound and NoClassDefFound exceptions.
  7.  what is checked and unchecked exception.
  8.  Draw the exception class hierarchy.
  9.  How will you create  your own exception class?
  10.  what is  difference between instanceOF and isInstance?
  11.  what is difference between final,finally,finalize?
  12.  How many types of reference are there in java?
  13.  what is soft refrence,weak refrence,phantom refrence?
  14.  what is weak hash map?
  15. what is stringpool , intern method?
  16. what is  diff b/w stringbuilder and string buffer?
  17. How do you make a class immutable. example with a class having a date object.?
  18. What are Atomic variables?
  19. Difference between equals and == ?
  20. Draw collection hierarchy.
  21. What are concrete implementations of list,set, map interfaces?
  22. What is difference between hashmap and hashtable?
  23. What method calls would be made internally, when i call get and put  function on a hash map.
  24. what is the return value of put function in hashmap?
  25. Why do objects have Hashcode and equals method?
  26.  How does hash map internally stores the data?
  27. What special methods do i need  to implement i want my class object to be key of a hashmap? 
  28. Why do objects have equals method? What would the code of equals method look like?
  29. Write your own hashmap implementation?
  30.  Write your own list implementation?
  31.  How can you make a singleton class?
  32.  what will happen when you de-serialize a single tone  object having  reference to itself?
  33.  LRU CACHE implementation, which Class will you use and why?
  34. What is ConcurrentHashMap, explain internal element storage and retrieval by multiple threads?