Friends below are some interview questions you might be asked . If you get familiar with these you can crack interview
Java basics for Freshers - Just try to understand Highlighted In red Selenium related question I will update in this blog. These are the question are asked when I take an interview . Remenber if you can crack basic Java question you interview will be easy
for experience All questions are relavent
Q12 which methods you need to override to use any object as key in HashMap?
To use any object as key in HashMap, it needs to implement equals() and hashCode() method
Java basics for Freshers - Just try to understand Highlighted In red Selenium related question I will update in this blog. These are the question are asked when I take an interview . Remenber if you can crack basic Java question you interview will be easy
for experience All questions are relavent
Q6 what is the difference between List and Set?
Set
|
List
|
Set allows only unique elements
|
List allows duplicate elements
|
Set is unordered while List is ordered
|
List maintains the order in which the objects are added
|
Q7 what is the difference between Map and Set?
Set
|
Map
|
Set contains unique keys but does not allow duplicate values
|
Map contains unique key but allows duplicate values
|
Q8 what are the classes implementing List and Set interface?
List
|
Set
|
ArrayList, Vector, Linked List
|
HashSet, Tree Set
|
Q9 what is an iterator?
Iterator is an interface. It is found in java.util package. It provides methods to iterate over any Collection.
Iterator is an interface. It is found in java.util package. It provides methods to iterate over any Collection.
Q10 what is the difference between Iterator and Enumeration?
Iterator
|
Enumeration
|
Iterator you can remove element(allows read/write operations)
|
Doesn’t support remove(supports readonly operations)
|
Q12 which methods you need to override to use any object as key in HashMap?
To use any object as key in HashMap, it needs to implement equals() and hashCode() method
What it static key word : **this is important most interview this questions will be asked****
Answer
1. the variable or method declared as static can be directly called using call name
2. Any block of code that needs to executed before main method you put that code in static block
static{
your code goes here, this block of code will be exe used before main method starts execution
}
static main ()
Q13 what is the difference between Queue and Stack?
Queue
|
Stack
|
First In First out
|
Last In First Out
|
Q14 How to reverse the List in Collections?
Collections.reverse(listobject);
Q15 How to convert the array of strings into the list?
asList()
Q16 what is the difference between ArrayList and Vector?
Vector
|
ArrayList
|
Vector is Synchronized
|
Array List is not synchronized
|
Vector is slow
|
ArrayList is fast
|
Vector increases the capacity twice of its initial size
|
ArrayList increases its ArraySize by 50%
|
Vector supports both Enumeration and Iterator
|
Array List support only Iterator for traversing
|
Q17 what is the difference between HashMap and Hashtable? - This is must
Commenly asked interview question
HashMap
|
Hashtable
|
HashMap allows null key value insertion
|
Hashtable does not allow null keys and null values.
|
HashMap is not synchronized or thread-safe
|
Hashtable is synchronized or thread-safe.
|
Q20 what is the difference between Array and ArrayList in Java? ** Very
important for sure you will be asked
Array
|
ArrayList
|
Array is static in size
|
ArrayList is dynamic in size.
|
Array can contain primitive data types
|
ArrayList cannot contain primitive data types.
If declared primitive data type JVM through Auto boxing(converting primitives to equivalent objects internally)
|
Fast performance
|
Slow performance
|
does not guarantee ordered elements.
|
does not guarantee ordered elements.
|
Hashing: How Hash Map Works In Java Or How Get() Method Works Internally
HashMap works on the principle of Hashing. To understand Hashing, we should understand the three terms first i.e Hash Function , Hash Value and Bucket .
HashMap get(Key k) method calls hashCode method on the key object and applies returned hashValue to its own static hash function to find a bucket location(backing array) where keys and values are stored in form of a nested class called Entry (Map.Entry)
ArrayList
|
Linked list
|
Manipulation with ArrayList is slow
|
Manipulation with LinkedList is faster
|
Ddataccess is faster.Better performance becaue of Index based search
|
Data access is slower. Performance is slower
As Linked list inherits Double Linked list which requires the traversal through all the elements for searching an element.
|
One should go for Arraylist implemententation when there are more get calls
|
Comparator
Is an interface which is used for sorting objects in Java.
Compare to method
Comparable
|
Comparator
|
Comparable provides single sorting sequence. In other words, we can sort the collection on the basis of single element such as id or name or price etc.
|
Comparator provides multiple sorting sequence. In other words, we can sort the collection on the basis of multiple elements such as id, name and price etc.
|
Comparable affects the original class i.e. actual class is modified.
|
Comparator doesn't affect the original class i.e. actual class is not modified.
|
Comparable is found in java.lang package.
|
Comparator is found in java.util package.
|
We can sort the list elements of Comparable type byCollections.sort(List) method.
|
We can sort the list elements of Comparator type byCollections.sort(List,Comparator) method.
|
HashSet vs TreeSet
HashSet
|
TreeSet
|
Removes duplicates
|
Removes duplicates
|
Don’t sort element
|
Sorts elements in Ascending order
|
Iterator vs List Iterator
Iterator
|
List Iterator
|
Iterator is used for traversing List and Set both.
|
We can use List Iterator to traverse List only, we cannot traverse Set using ListIterator.
|
We can traverse in only forward direction using Iterator.
|
Using ListIterator, we can traverse a List in both the directions (forward and Backward)
|
HashMap vs TreeMap
HashMap
|
Tree Map
|
Return elements as it on how they are added
|
Return Sorted in Ascending order of Keys
|
HashMap vs tree map
Binary tree
Enums
Hash map usage
- Create an entry set
- Create a iterator object over entry set
- Then iterate
LinkedHashMap
Same as HashMap just preserves the insertion order
What are concurrentCollectionClasses?
In jdk1.5 , Java Api developers had introduced new package called java.util.concurrent that have thread-safe collection classes as they allow collections to be modified while iterating
Ex : Concurrent Hash Map class
How will you make Collections readOnly ?
Collections.unmodifiableMap(Map m)
Collections.unmodifiableList(List l)
Collections.unmodifiableSet(Set s)
Difference between Stack vs Heap in Java
Stack
|
Heap
|
Stack memory is used to store variables and functions
|
Heap memory is used to store Objects
|
What is reflection?
The ability to examine or modify the runtime behaviour of applications running in the Java virtual machine
- Examine an object's class at runtime
- Construct an object for a class at runtime
- Examine a class's field and method at runtime
- Invoke any method of an object at runtime
- Change accessibility flag of Constructor, Method and Field
Binary Tree
A binary tree is a tree data structure in which each node has at most two children, which are referred to as the left child and the right child
PreOrder traversal - In PreOrder traversal, each node is processed before either of its sub-trees. In simpler words, Visit each node before its children.
InOrder traversal : In In-Order traversal, each node is processed between subtrees.In simpler words,Visit left subtree, node and then right subtree.
PostOrder traversal: In PostOrder traversal,each node is processed after subtrees traversal. In simpler words, Visit left subtree, right subtree and then node.
Level order traversal : In Level order traversal, tree is traversed by each level. It is same as breadth first search.
Spiral/Zigzag order traversal : In spiral order traversal, tree is traversed in spiral shape.
PreOrder traversal - In PreOrder traversal, each node is processed before either of its sub-trees. In simpler words, Visit each node before its children.
InOrder traversal : In In-Order traversal, each node is processed between subtrees.In simpler words,Visit left subtree, node and then right subtree.
PostOrder traversal: In PostOrder traversal,each node is processed after subtrees traversal. In simpler words, Visit left subtree, right subtree and then node.
Level order traversal : In Level order traversal, tree is traversed by each level. It is same as breadth first search.
Spiral/Zigzag order traversal : In spiral order traversal, tree is traversed in spiral shape.
Binary Search
Binary search is a fast search algorithm. This search algorithm works on the principle of divide and conquer. For this algorithm to work properly the data collection should be in sorted form.
Binary search a particular item by comparing the middle most item of the collection. If match occurs then index of item is returned. If middle item is greater than item then item is searched in sub-array to the right of the middle.
What is immutable class in Java?
Immutable classes are those class, whose object cannot be modified once created. Immutable and mutable objects are, String and StringBuffer.
Class can be made immutable by making it Final.
Benefits
Benefits
- Thread Safe
Stack
Stack is a subclass of Vector collection
Push pop
Sorting
Bubble Sort
Comparing each pair of adjacent items and swapping them
Selection Sort
Tries to identify the smallest and the largest elements and swapping the positions
Insertion Sortd
Quick sort in java.
Quicksort or partition-exchange sort, is a fast sorting algorithm, which is using divide and algorithm. Quicksort first divides a large list into two smaller sub-lists: the low elements and the high elements. Quicksort can then recursively sort the sub-lists.
Merge sort in java.
Divided into unsorted n no of patterns and
What is difference between Singly Linked List and Doubly Linked List data structure?
Main difference between singly linked list and doubly linked list is ability to traverse. In a single linked list, node only points towards next node, and there is no pointer to previous node, which means you cannot traverse back on a singly linked list. On the other hand doubly linked list maintains two pointers, towards next and previous node, which allows you to navigate in both direction in any linked list.
Main difference between singly linked list and doubly linked list is ability to traverse. In a single linked list, node only points towards next node, and there is no pointer to previous node, which means you cannot traverse back on a singly linked list. On the other hand doubly linked list maintains two pointers, towards next and previous node, which allows you to navigate in both direction in any linked list.
Difference between ArrayList and LinkedList:
ArrayList
|
LinkedList
|
1) ArrayList internally uses dynamic array to store the elements.
|
LinkedList internally uses doubly linked list to store the elements.
|
2) Manipulation with ArrayList is slow because it internally uses array. If any element is removed from the array, all the bits are shifted in memory.
|
Manipulation with LinkedList is faster than ArrayList because it uses doubly linked list so no bit shifting is required in memory.
|
3) ArrayList class can act as a list only because it implements List only.
|
LinkedList class can act as a list and queue both because it implements List and Deque interfaces.
|
4) ArrayList is better for storing and accessing data.
|
LinkedList is better for manipulating data.
|
Thread.sleep vs Thread.wait vs Thread.Yield
Thread.sleep cannot be revoked
Thread.wait can notify thread to continue
Thread.yieid will go to runnable/ ready state
Thread.Wait releases the monitor or lock it was holding on that object, but when a thread calls the sleep() method, it never releases the monitor even if it is holding.
Thread.yield() method pauses the currently executing thread temporarily for giving a chance to the remaining waiting threads of the same priority to execute. If there is no waiting thread or all the waiting threads have a lower priority then the same thread will continue its execution
Thread.yield() method pauses the currently executing thread temporarily for giving a chance to the remaining waiting threads of the same priority to execute. If there is no waiting thread or all the waiting threads have a lower priority then the same thread will continue its execution
Volatile
Any way the volatile keyword in Java is used as an indicator to Java compiler and Thread that do not cache value of this variable and always read it from main memory. ...Java volatile keyword cannot be used with method or class and it can only be used with variable.
So if you want to share any variable in which read and write operation is atomic by implementation e.g. read and write in an int or a boolean variable then you can declare them as volatile variable.