Java Interview Questions
Java Interview Questions
1. Current organisation
2. Roles and responsibility
3. Where and how SNMP is used in ur project
4. When and where to use which collection
5. How garbage collection works
6. Difference in Jdk and jre
7. What’s the role of jvm
8. How jre and jvm is related
9. What of design patterns used and worked
10. String with 1 & 0 values in it, how to take out 0 or count 0 without loop
11. While loop with condition always true , inside loop you are creating the object of a class and Syso it. What will happen.
12. What is out of memory exception and how will you manage it
13. What are the key areas you would keep in mind when creating the HLD
14. What is thread deadlock
15. What is thread pool
Answers :
Q4. When and where to use which collection?
The key interfaces used by the collection framework are List, Set and Map. The List and Set extends the
Collection interface. Should not confuse the Collection interface with the Collections class which is a utility class.
A Set is a collection with unique elements and prevents duplication within the collection. HashSet and TreeSet
are implementations of a Set interface. A List is a collection with an ordered sequence of elements and may
contain duplicates. ArrayList, LinkedList and Vector are implementations of a List interface.
The Collection API also supports maps, but within a hierarchy distinct from the Collection interface. A Map is an
object that maps keys to values, where the list of keys is itself a collection object. A map can contain duplicate
values, but the keys in a map must be distinct. HashMap, TreeMap and Hashtable are implementations of a Map
interface.
How to implement collection ordering? SortedSet and SortedMap interfaces maintain sorted order. The
classes, which implement the Comparable interface, impose natural order. For classes that don’t implement
comparable interface, or when one needs even more control over ordering based on multiple attributes, a
Comparator interface should be used.
Design pattern: What is an Iterator? An Iterator is a use once object to access the objects stored in a collection.
Iterator design pattern (aka Cursor) is used, which is a behavioural design pattern that provides a way to access
elements of a collection sequentially without exposing its internal representation.
We can use the following options with the Java command to enable tracing for garbage collection events.
-verbose:gc reports on each garbage collection event.
Explain types of references in Java? java.lang.ref package can be used to declare soft, weak and phantom
references.
ô Garbage Collector won’t remove a strong reference.
ô A soft reference will only get removed if memory is low. So it is useful for implementing caches while
avoiding memory leaks.
ô A weak reference will get removed on the next garbage collection cycle. Can be used for implementing
canonical maps. The java.util.WeakHashMap implements a HashMap with keys held by weak references.
ô A phantom reference will be finalized but the memory will not be reclaimed. Can be useful when you want to
be notified that an object is about to be collected.