Java interview questions and answers Top 20 Frequently asked Interview questions in Core Java Here we have list of important questions with answers for Core java interview. Free online study material for IT companies Interview.
1. When do you override hashcode and equals() ?Answer : Whenever necessary especially if you want to do equality check or want to use your object as key in HashMap.
|
2. Is it better to synchronize critical section of getInstance() method or whole getInstance() method ?Answer: Answer is critical section because if we lock whole method than every time someone call this method will have to wait even though we are not creating any object.
|
3. What is the difference when String is gets created using literal or new() operator ?Answer: When we create string with new() its created in heap and not added into string pool while String created using literal are created in String pool itself which exists in Perm area of heap.
|
4. Can you override private or static method in Java ?Answer: you can not override private or static method in Java, if you create similar method with same return type and same method arguments that’s called method hiding.
|
5. What is the issue with following implementation of compareTo() method in Java ?Answer: public int compareTo(Object o){ Employee emp = (Employee) emp; return this.id – o.id; }
|
6. Can you access non static variable in static context?Answer: No you cannot access static variable in non static context in Java.
|
7. What is the difference between Collection and Collections ?Answer: Collection is an interface while Collections is a java class , both are present in java.util package and part of java collections framework.
|
8. What is the difference between List and Set ?Answer: Set contain only unique elements while List can contain duplicate elements.
|
9. What is an iterator ?Answer: Iterator is an interface . It is found in java.util package. It provides methods to iterate over any Collection.
|
10. What is the difference between HashMap and Hashtable ?Answer: a. HashMap allows one null key and any number of null values while Hashtable does not allow null keys and null values.
|
11. What is the difference between HashSet and TreeSet ?Answer: HashSet maintains the inserted elements in random order while TreeSet maintains elements in the sorted order
|
12. Write the code for iterating the list in different ways in java ?Answer: There are two ways to iterate over the list in java :
|
13. How many types of memory areas are allocated by JVM?Answer: 1. Class(Method) Area 2. Heap 3. Stack 4. Program Counter Register 5. Native Method Stack |
14. What is classloader?Answer: The classloader is a subsystem of JVM that is used to load classes and interfaces.There are many types of classloaders e.g. Bootstrap classloader, Extension classloader, System classloader, Plugin classloader etc.
|
15. What is difference between object oriented programming language and object based programming language?Answer: Object based programming languages follow all the features of OOPs except Inheritance. Examples of object based programming languages are JavaScript, VBScript etc.
|
16. What is constructor?Answer: Constructor is just like a method that is used to initialize the state of an object. It is invoked at the time of object creation. |
17. Can we execute a program without main() method?Answer: Yes, one of the way is static block.
|
18. What is Inheritance?Answer: Inheritance is a mechanism in which one object acquires all the properties and behaviour of another object of another class. It represents IS-A relationship. It is used for Code Resusability and Method Overriding. |
19. Which class is the superclass for every class.Answer: Object Class |
20. What is super in java?Answer: It is a keyword that refers to the immediate parent class object. |