Part 6
Interview questions on Collections in java for experienced covered in this post:
What is Blocking Queue? What are the types of Blocking Queue?
-
A blocking queue is a queue that blocks when you try to
dequeue from it.
-
The queue is empty or if you try to enqueue objects to it and
the queue is already full.
-
A Queue that additionally supports operations that wait for
the queue to become non-empty when retrieving an element, and wait for space
to become available in the queue when storing an element.
-
A BlockingQueue does not accept null elements.
Implementations throw NullPointerException on attempts to add, put or offer
a null. A null is used as a sentinel value to indicate the failure of poll
operations.
-
BlockingQueue implementations are designed to be used
primarily for producer-consumer queues, but additionally, support the
Collection interface.
-
For example, it is possible to remove an arbitrary element
from a queue using remove(x). However, such operations are in general not
performed very efficiently, and are intended for only occasional use, such
as when a queued message is cancelled.
-
BlockingQueue implementations are thread-safe.
-
All queuing methods achieve their effects atomically using
internal locks or other forms of concurrency control.
-
A BlockingQueue does not intrinsically support any kind of
"close" or "shutdown" operation to indicate that no more items will be
added.
-
A BlockingQueue belongs to java.util.concurrent.BlockingQueue
package.
-
Following are the
types of BlockingQueue.
1.
PriorityBlockingQueue.
2.
LinkedBlockingQueue.
3.
ArrayBlockingQueue.
What is CircularQueue? What are the advantages of CircularQueue?
-
Circular Queue is a linear data structure in which the
operations are performed based on FIFO (First In First Out) principle.
-
The last element is connected back to the first element to
make a circle.
-
CircularQueue is also called 'Ring Buffer'.
-
Circular queue is used in memory management and Process
Scheduling.
-
Advantages of
Circular Queue:
1.
All operations occur in constant time.
2.
Doesn’t use dynamic memory.
3.
No Memory leaks.
4.
Simple Implementation.
5.
Plays an important role in memory management.
Which data structure has implemented LIFO strategy?
-
Stack data structure has implemented LIFO (Last In First Out)
strategy.
-
Please refer to the below example.
package simplifiedjava.crackedInterview; import java.util.Stack; public class StackDemo {
public
static
void
main(String[] args) {
Stack<Integer> s
= new
Stack<Integer>();
s.push(10);
s.push(20);
s.push(30);
System.out.println(s);
s.pop();
System.out.println(s);
}
}
Output:
[10, 20, 30]
[10, 20] |
- Graphical representation of Stack implementation.
Explain the defined method in Stack?
-
In Stack there are 5 main methods can be used to perform
basic operations.
1.
Push(Object O):
To insert an object into stack.
2.
Pop(): To remove
and return top of the stack.
3.
Peek(): To
return top of the object without removing it.
4.
Empty(): return
true if the stack is empty.
5.
Search():
returns offset if the element is available otherwise, returns -1.
Which data structure has implemented FIFO strategy?
-
Queue data structure has implemented FIFO (First In First
Out) strategy.
-
Please refer to the below example.
package simplifiedjava.crackedInterview;
import
java.util.LinkedList; import java.util.Queue; public class QueueDemo {
public
static
void
main(String[] args) {
Queue<Integer> queue
= new
LinkedList<Integer>();
queue.offer(10);
queue.offer(20);
queue.offer(30);
System.out.println(queue);
queue.peek();
System.out.println(queue);
queue.poll();
System.out.println(queue);
}
}
Output:
[10, 20, 30]
[10, 20, 30]- Pick() just retrieved the value but not
removed.
[20, 30] – Poll() retrieved the value and removed as well.
|
- Graphical Representation of Queue.
Explain the defined method in Queue?
-
There are 5 main methods defined in Queue.
1.
Offer(Object o):
Add object in queue.
2.
Peek(): return
head element of queue. If queue is null then it will return null.
3.
Element():
return head element of queue. If the queue is null the it will throw
NoSuchElementException.
4.
Poll(): returns
head element of queue and remove. If the queue is empty then it will return
null.
5.
Remove():
returns head element of queue and remove. If queue is empty then it will
throw NoSuchElementException.
Explain deque interface? What is the importance of deque interface?
-
A Deque is a double-ended-queue.
-
A double-ended-queue is a linear collection of elements that
supports the insertion and removal of elements at both endpoints.
-
The Deque interface is a richer abstract data type than both
Stack and Queue because it implements both stacks and queues at the same
time.
-
The Deque interface, defines methods to access the elements
at both ends of the Deque instance.
-
Methods are provided to insert, remove, and examine the
elements.
-
Predefined classes like ArrayDeque and LinkedList implement
the Deque interface.
-
Note that the Deque interface can be used both as
last-in-first-out stacks and first-in-first-out queues.
-
Advantages of
Deque.
1.
Deques are faster in adding/removing elements to the end or
beginning.
2.
Lists are faster in adding/removing elements to any other
'middle' position.
3.
You can also use insert (index, value) on lists, while on
deques you can only append left or right.
-
Java interview questions and answers all MNC - Click here
-
Basic core java interview questions and answers for freshers
- Click here
-
Core java interview questions for 3 years experience
- Click here
-
Core java interview questions and answers for 3-5 years exp
- Click here
-
Core java interview questions and Answers for 5 - 7 Years exp
- Click here
-
Basic Java Interview Questions and Answers - Click here
-
Java interview questions and answers on oops - Click here
-
Java interview questions and answers on Strings - Click here
-
Java interview questions on exception handling - Click here
-
Interview questions on multithreading in java for experienced
- Click here
-
Interview questions on serialization in java for experienced
- Click here
-
Interview questions on inner class in java for experienced
- Click here
-
Interview questions on Collections in java for experienced
- Click here
Thank you techies for visiting this blog. I hope you enjoyed this blog and got more technical knowledge. I have tried to cover all types of questions and provided examples tested on eclipse as much as I can. Guys, please don’t just mug up the questions and answers. Try to clear your concepts with examples. Try to write a code on eclipse once you read the concepts. It will help you to memorize the concepts for a very long time. Simultaneously you will be prepared for interview programs as well. It will help you to survive in the IT sector for a long time. It may be easy to crack an interview but it's really tough to survive in the IT industry with inadequate knowledge and skills. I have collected all the questions from actual interviews attended by my friends, colleagues, college mate and classmate. I have covered frequently asked questions as well as challenging questions. I have included many programs to understand the concept thoroughly. I will try to explain the concept with the help of a real-time program in eclipse. You can share more questions which are not covered in this blog or post. Always welcome your suggestions and queries. I will definitely try to resolve it. Please comment your queries and new set of questions under the comment sections. I will create a new post for those questions.
My total experience is 10. Initially I had worked
on some support projects and then I moved to java
projects. I had worked with many multi-national
companies like R-Systems, Tata Consultancy Services,
Cybage Softwares. Fortunately, TCS and
Cybage has given me an opportunity to take
interviews for experienced candidates. I have
conducted more than 1000 interviews by now. Mock sessions will be conducted for minimal charges. I will guide you personally on how to crack interviews. All sessions will be online sessions only. My interview book will be provided free of cost if you enroll for personal training. Once you have done practice then I assured you will definitely crack almost all java interviews. I have published my book named with "All MNC Java Interview" which is available on amazon. You can go through it or you can directly contact to me if you are interested to read the book. I will give you special discount. I have covered near about 550 questions and answers with program tested on eclipse and necessary diagram. I have covered interview questions asked in all reputed MNC like TCS, Infosys, Capgemini, Tech Mahindra, Cognizant, City Bank, Barclays, Amdocs, Mindtree etc. My purpose behind this book is, help you to get a job in your dream company. I can understand this is a struggling period for job seekers and freshers. Everybody must have gone from this phase. so never give up. Keep upgrading yourself. I am sure you will get a job in your dream company. All the best!!! Please reach out to me for personal training for interview preparation.
You can reach out to me at mncjavainterview@gmail.com.
|
0 Comments