Saturday, April 29, 2017

Java interview questions

1. How to create immutable objects and benefits of it?

  1. Don't provide "setter" methods — methods that modify fields or objects referred to by fields. Use constructor to take the values required.
  2. Make all fields final and private.
  3. Don't allow subclasses to override methods. The simplest way to do this is to declare the class as final. A more sophisticated approach is to make the constructor private and construct instances in factory methods.
  4. If the instance fields include references to mutable objects, don't allow those objects to be changed:
    • Don't provide methods that modify the mutable objects.
    • Don't share references to the mutable objects. Never store references to external, mutable objects passed to the constructor; if necessary, create copies, and store references to the copies. Similarly, create copies of your internal mutable objects when necessary to avoid returning the originals in your methods.

      Benifits:
  1. Immutable objects are thread-safe so you will not have any synchronization issues.
  2. Immutable objects are good Map keys and Set elements, since these typically do not change once created.
  3. Immutability makes it easier to write, use and reason about the code (class invariant is established once and then unchanged)
  4. Immutability makes it easier to parallelize your program as there are no conflicts among objects.
  5. The internal state of your program will be consistent even if you have exceptions.
  6. References to immutable objects can be cached as they are not going to change.

Refernces
http://okyasoft.blogspot.com/2014/05/6-benefits-of-programming-with_5146.html
https://docs.oracle.com/javase/tutorial/essential/concurrency/imstrat.html



2. Collections overview and Big o notation?

The following table summarizes the principal classes in Java collections framework for quick reference:
Principal collection class
Base class
Base interfaces
Allow duplicate elements?
Ordered?
Sorted?
Thread-safe?
ArrayList
AbstractList
List
Yes
Yes
No
No
LinkedList
AbstractSequentialList
List;
Deque
Yes
Yes
No
No
Vector
AbstractList
List
Yes
Yes
No
Yes
HashSet
AbstractSet
Set
No
No
No
No
LinkedHashSet
HashSet
Set
No
Yes
No
No
TreeSet
AbstractSet
Set;
NavigableSet;
SortedSet
No
Yes
Yes
No
HashMap
AbstractMap
Map
No
No
No
No
LinkedHashMap
HashMap
Map
No
Yes
No
No
Hashtable
Dictionary
Map
No
No
No
Yes
TreeMap
AbstractMap
Map;
NavigableMap;
SortedMap
No
Yes
Yes
No

Refernces: http://www.codejava.net/java-core/collections/java-collections-framework-summary-table


List implementations:
                      get  add  contains next remove(0) iterator.remove
ArrayList             O(1) O(1) O(n)     O(1) O(n)      O(n)
LinkedList            O(n) O(1) O(n)     O(1) O(1)      O(1)
CopyOnWrite-ArrayList O(1) O(n) O(n)     O(1) O(n)      O(n)
Set implementations:
                      add      contains next     notes
HashSet               O(1)     O(1)     O(h/n)   h is the table capacity
LinkedHashSet         O(1)     O(1)     O(1) 
CopyOnWriteArraySet   O(n)     O(n)     O(1) 
EnumSet               O(1)     O(1)     O(1) 
TreeSet               O(log n) O(log n) O(log n)
ConcurrentSkipListSet O(log n) O(log n) O(1)
Map implementations:
                      get      containsKey next     Notes
HashMap               O(1)     O(1)        O(h/n)   h is the table capacity
LinkedHashMap         O(1)     O(1)        O(1) 
IdentityHashMap       O(1)     O(1)        O(h/n)   h is the table capacity 
EnumMap               O(1)     O(1)        O(1) 
TreeMap               O(log n) O(log n)    O(log n) 
ConcurrentHashMap     O(1)     O(1)        O(h/n)   h is the table capacity 
ConcurrentSkipListMap O(log n) O(log n)    O(1)
Queue implementations:
                      offer    peek poll     size
PriorityQueue         O(log n) O(1) O(log n) O(1)
ConcurrentLinkedQueue O(1)     O(1) O(1)     O(n)
ArrayBlockingQueue    O(1)     O(1) O(1)     O(1)
LinkedBlockingQueue   O(1)     O(1) O(1)     O(1)
PriorityBlockingQueue O(log n) O(1) O(log n) O(1)
DelayQueue            O(log n) O(1) O(log n) O(1)
LinkedList            O(1)     O(1) O(1)     O(1)
ArrayDeque            O(1)     O(1) O(1)     O(1)
LinkedBlockingDeque   O(1)     O(1) O(1)     O(1)

Spring:

2. Spring scopes?

The scopes supported out of the box are listed below:

Table 4.4. Bean scopes
ScopeDescription
Scopes a single bean definition to a single object instance per Spring IoC container.
Scopes a single bean definition to any number of object instances.
Scopes a single bean definition to the lifecycle of a single HTTP request; that is each and every HTTP request will have its own instance of a bean created off the back of a single bean definition. Only valid in the context of a web-aware SpringApplicationContext.
Scopes a single bean definition to the lifecycle of a HTTP Session. Only valid in the context of a web-aware SpringApplicationContext.
Scopes a single bean definition to the lifecycle of a global HTTP Session. Typically only valid when used in a portlet context. Only valid in the context of a web-aware Spring ApplicationContext.
Reference:
http://docs.spring.io/spring/docs/3.0.0.M3/reference/html/ch04s04.html


No comments:

Post a Comment