Java Interview Questions
Java Interview Questions
Java Interview Questions
Java Platform
1. Why is Java so popular?
2. What is platform independence?
3. What is bytecode?
4. Compare JDK vs JVM vs JRE
5. What are the important differences between C++ and Java?
6. What is the role for a classloader in Java?
Wrapper Classes
1. What are Wrapper classes?
2. Why do we need Wrapper classes in Java?
3. What are the different ways of creating Wrapper class instances?
4. What are differences in the two ways of creating Wrapper classes?
5. What is auto boxing?
6. What are the advantages of auto boxing?
7. What is casting?
8. What is implicit casting?
9. What is explicit casting?
Strings
1. Are all String’s immutable?
2. Where are String values stored in memory?
3. Why should you be careful about String concatenation(+) operator in loops?
4. How do you solve above problem?
5. What are differences between String and StringBuffer?
6. What are differences between StringBuilder and StringBuffer?
7. Can you give examples of different utility methods in String class?
Modifiers
1. What is default class modifier?
2. What is private access modifier?
3. What is default or package access modifier?
4. What is protected access modifier?
5. What is public access modifier?
6. What access types of variables can be accessed from a class in same package?
7. What access types of variables can be accessed from a class in different package?
8. What access types of variables can be accessed from a sub class in same
package?
9. What access types of variables can be accessed from a sub class in different
package?
10. What is the use of a final modifier on a class?
11. What is the use of a final modifier on a method?
12. What is a final variable?
13. What is a final argument?
14. What happens when a variable is marked as volatile?
15. What is a static variable?
Exception handling
1. Why is exception handling important?
2. What design pattern is used to implement exception handling features in most
languages?
3. What is the need for finally block?
4. In what scenarios is code in finally not executed?
5. Will finally be executed in the program below?
6. Is try without a catch is allowed?
7. Is try without catch and finally allowed?
8. Can you explain the hierarchy of exception handling classes?
9. What is the difference between error and exception?
10. What is the difference between checked exceptions and unchecked exceptions?
11. How do you throw an exception from a method?
12. What happens when you throw a checked exception from a method?
13. What are the options you have to eliminate compilation errors when handling
checked exceptions?
14. How do you create a custom exception?
15. How do you handle multiple exception types with same exception handling
block?
16. Can you explain about try with resources?
17. How does try with resources work?
18. Can you explain a few exception handling best practices?
Miscellaneous topics
1. What are the default values in an array?
2. How do you loop around an array using enhanced for loop?
3. How do you print the content of an array?
4. How do you compare two arrays?
5. What is an enum?
6. Can you use a switch statement around an enum?
7. What are variable arguments or varargs?
8. What are asserts used for?
9. When should asserts be used?
10. What is garbage collection?
11. Can you explain garbage collection with an example?
12. When is garbage collection run?
13. What are best practices on garbage collection?
14. What are initialization blocks?
15. What is a static initializer?
16. What is an instance initializer block?
17. What is tokenizing?
18. Can you give an example of tokenizing?
19. What is serialization?
20. How do you serialize an object using serializable interface?
21. How do you de-serialize in Java?
22. What do you do if only parts of the object have to be serialized?
23. How do you serialize a hierarchy of objects?
24. Are the constructors in an object invoked when it is de-serialized?
25. Are the values of static variables stored when an object is serialized?
Collections
1. Why do we need collections in Java?
2. What are the important interfaces in the collection hierarchy?
3. What are the important methods that are declared in the collection interface?
4. Can you explain briefly about the List interface?
5. Explain about ArrayList with an example?
6. Can an ArrayList have duplicate elements?
7. How do you iterate around an ArrayList using iterator?
8. How do you sort an ArrayList?
9. How do you sort elements in an ArrayList using comparable interface?
10. How do you sort elements in an ArrayList using comparator interface?
11. What is vector class? How is it different from an ArrayList?
12. What is linkedList? What interfaces does it implement? How is it different
from an ArrayList?
13. Can you briefly explain about the Set interface?
14. What are the important interfaces related to the Set interface?
15. What is the difference between Set and sortedSet interfaces?
16. Can you give examples of classes that implement the Set interface?
17. What is a HashSet?
18. What is a linkedHashSet? How is different from a HashSet?
19. What is a TreeSet? How is different from a HashSet?
20. Can you give examples of implementations of navigableSet?
21. Explain briefly about Queue interface?
22. What are the important interfaces related to the Queue interface?
23. Explain about the Deque interface?
24. Explain the BlockingQueue interface?
25. What is a priorityQueue?
26. Can you give example implementations of the BlockingQueue interface?
27. Can you briefly explain about the Map interface?
28. What is difference between Map and sortedMap?
29. What is a HashMap?
30. What are the different methods in a Hash Map?
31. What is a TreeMap? How is different from a HashMap?
32. Can you give an example of implementation of navigableMap interface?
33. What are the static methods present in the collections class?
Advanced collections
1. What is a lock? How is it different from using synchronized approach?
2. What is initial capacity of a Java collection?
3. What is load factor?
4. When does a Java collection throw UnsupportedOperationException?
5. What is difference between fail-safe and fail-fast iterators?
6. What are atomic operations in Java?
7. What is BlockingQueue in Java?
Generics
1. What are Generics?
2. Why do we need Generics? Can you give an example of how Generics make a
program more flexible?
3. How do you declare a generic class?
4. What are the restrictions in using generic type that is declared in a class declaration?
5. How can we restrict Generics to a subclass of particular class?
6. How can we restrict Generics to a super class of particular class?
7. Can you give an example of a generic method?
Multi threading
1. What is the need for threads in Java?
2. How do you create a thread?
3. How do you create a thread by extending thread class?
4. How do you create a thread by implementing runnable interface?
5. How do you run a thread in Java?
6. What are the different states of a thread?
7. What is priority of a thread? How do you change the priority of a thread?
8. What is executorservice?
9. Can you give an example for executorservice?
10. Explain different ways of creating executor services .
11. How do you check whether an executionservice task executed
successfully?
12. What is callable? How do you execute a callable from executionservice?
13. What is synchronization of threads?
14. Can you give an example of a synchronized block?
15. Can a static method be synchronized?
16. What is the use of join method in threads?
17. Describe a few other important methods in threads?
18. What is a deadlock?
19. What are the important methods in Java for inter-thread
communication?
20. What is the use of wait method?
21. What is the use of notify method?
22. What is the use of notifyall method?
23. Can you write a synchronized program with wait and notify methods?