Most Common Java interview questions
getClass().getName() + '@' + Integer.toHexString(hashCode())
it is a native method in object Class , returns integer code .
Two unequal objects can have same hashcode
called before Garbage Collection .
@FunctionalInterface annotation will add a compile time check for an interface to have only one abstract method. It will be used for lambda expressions.
- String vs StringBuffer vs StringBuilder
- String : immutable
- StringBuffer : mutable , synchronized
- StringBuilder : mutable , not tread safe .
- Explain toString method.
getClass().getName() + '@' + Integer.toHexString(hashCode())
- Explain hashcode
it is a native method in object Class , returns integer code .
Two unequal objects can have same hashcode
- Explain finalize
called before Garbage Collection .
- How to create a immutable class ?
- make the class as final
- attributes as private and remove setters .
- getters should return a copy of object .
- what is transient ?
- What is volatile ?
- What is static inner class
- can be accessed without creating object of outer class , does not have access to instance variables .
- What is Stack
- LIFO
- push : add element on the top of stack
- pop : remove element from the top of stack
- peek : returns element at top of stack without removing it .
- What is Queue
- add : adds to last , in case of capacity restriction throws IllegalStateException.
- remove : returns and remove the element on the top of the queue , throws NoSuchElementException if the queue is empty .
- element : returns the element on the top of the queue , throws NoSuchElementException if the queue is empty .
- offer : adds to last , in case of capacity restriction returns false .
- poll: returns and remove the element on the top of the queue , returns null if the queue is empty
- peek: returns the element on the top of the queue , returns null if the queue is empty .
- What is Dqueue ?
- push (addFirst) : add element on the top of stack
- pop : remove element from the top of stack
- peek : returns element at top of stack without removing it .
- add : adds to last , in case of capacity restriction throws IllegalStateException.
- remove : returns and remove the element on the top of the queue , throws NoSuchElementException if the queue is empty .
- element : returns the element on the top of the queue , throws NoSuchElementException if the queue is empty .
- offer : adds to last , in case of capacity restriction returns false .
- poll: returns and remove the element on the top of the queue , returns null if the queue is empty
- peek: returns the element on the top of the queue , returns null if the queue is empty .
- What is the difference between ArrayList and Vector?
- What is the difference between ArrayList and LinkedList ?
- LinkedList implements Dqueue has additional methods .
- decendingIterator
- getFirst
- getLast
- addFirst
- addLast
- Add and remove is faster , get and set is slower in LinkedList.
- ArrayList stores data in contiguous locations LinkedList store data using pointers and reference
- LinkedList is better if we need to perform data manipulation , arrayList is better if we just want to send the data.
- Explain Legacy collection in java ?
- Vector , Stack (LIFO ) : dynamic array
- Dictionary (abstract) , Hashtable (concrete ), Properties ( string key and value , loaded to/from file) : key value pair ,null key not allowed
- Enumeration : Iterate over legacy collection
- How to make ArrayList thread safe.
- Use Collections.synchronizedList
- CopyOnWriteArrayList : can be modified during iteration but modified values will not be reflected .
- How do HashMap handles different key with same hashcode ?
- What is BlockingQueue
Thread-safe , does not accept null value . FIFO . It is used to implement producer / consumer issues .
- put : add the element to the queue , if space is not available it waits for the space to be available.
- take : returns element from head of the queue , if no elements wait for the element to come.
- What are the new features added in Java 7?
- Try with resources : the resources will be closed automatically (ARM),
- should implement AutoCloseable introduced in Java 7 .
- to support ARM Suppressed Exceptions are introduced .
- the exceptions originating at try block is propagated and the exceptions occurring in finally block is suppressed .
- addSuppressed and getSuppressed methods added in Throwable class
- Closeable interface now extends AutoClosable interface .
- Multiple Exception in single catch separated by | .
- Underscore in numeric literals
- Binary literals 0b,0B
- Switch case with string
- Type interface for generic instance.using <> .
- Java.nio.file package : used to create, delete , copy , move etc. files and directories.
- @SafeVarargs : used by programmer to mark that no unsafe operations are performed in the constructor with var args .
- String constant pool is stored in heap instead of perm gen.
- New features in Java 8
- Perm gen is removed.
- Functional interface
- Lambda support
- Interface default and static
- For each in iterator
- Java stream API.
- Optional class : avoid null pointer
- Explain interface default method :
- The implementing class does need not implement the default method.
- Sub interfaces can override or make it abstract .
- Explain method reference
Static method
Class name :: method name
Instance method
Object name:: method name
- How does multiple inheritance issue for default method is handled .
- What is optional class in Java 8
- Explain functional interface
@FunctionalInterface annotation will add a compile time check for an interface to have only one abstract method. It will be used for lambda expressions.
- Explain memory management in Java.
- Heap : stores object data.
- Permgen : stores class data ,method local initialization , string contact pool.
- Stack : store data required while returning from method
- How to manage memory ?
- System.gc() or Runtime.gc() : garbage collector may be called .
- Runtime.freeMemory() : free memory
- Runtime.totalMemory() : total memory available
- Runtime.maxMemory() : maximum memory that JVM is going to use
- What is just in time compiler(JIT)
- Explain Creational Design Patterns .
- Factory
- Abstract Factory
- Builder : create complex object , method chaining .
- Singleton
- Prototype
- Explain Structural Design Patter .
- Adapter
- Bridge
- Composite
- Decorator
- Facade
- Flywheel
- Proxy
- Explain Behavioral Design Patter .
- Chain of Responsibility
- Command
- Interpretor : how to include language elements
- Iterator
- Mediator : simplify communication between objects
- Observer : multiple objects notification
- State : modify the behavior on state change
- Template : abstract definition of algorithm
- Visitor : add polymorphic functions to class noninvasively .
- How can we make singleton class thread safe.
Null check for instance and creation of new object should be done inside a synchronized block.
- What is executor service?
- What is Thread.yeald?
- Explain serialization
- Serializable interface should be implemented .
- Serializable is a marker interface for identification .
- sub class must be serializable , super class need not be serializable .
- Transient and static attributes are not serialized.
- readresolve method can be implemented and same instance can be returned, while deserialization of singleton class.
- Write replace is called before write object .
- Explain serialVersionUID .
- if it not present java compiler adds it .
- it is static final and long .
- It is used to check the sender and receiver are compatible if there is mismatch in the serialVersionUID InvalidClassException is thrown .
- If there is no serialVersionUID and we have serialized an object , we added some field and now if we want to deserialize it we get InvalidClassException .
- Explain Externalizable interface
used for custom serialization
- readExternal
- writeExternal
- Explain classloaders
- Bootstrap : parent of all , first to be loaded
- extension : load all classes from .jar from java.ext.dirs
- system : loads the classes at java.class.path or classes specified at -classpath .
No comments:
Post a Comment