Part 2
Why wait(), notify() and notifyAll() methods are in Object class even these methods are used in multithreading?
- Basically, wait(), notify(), notifyAll() methods are using by threads to
communicate with each other. But these methods belong to java.lang.Object
class.
- Every Object has its lock or Monitor.
- One thread can hold only one lock or monitor at a time while executing a
synchronized block or method.
- If wait(), notify() and notifyAll() methods are put in Thread class instead
of Object class then we would have faced Thread communication problem,
Synchronization on object won’t be possible. If each thread will have a
monitor we won’t have any way of achieving synchronization.
Can we access non-static class members
inside static inner class?
-
No, non-static class members are not allowed inside static nested or inner
class.
- Even it is applicable to the outer class as well. Inside the static area,
only static members are allowed.
Explain the internal working of
LinkedHashSet?
- LinkedHashSet internally uses the LinkedHashMap object to store the
elements.
- The elements you inserted are stored as a key of the LinkedHashMap
object.
- There are newly introduced two fields before and
after which are responsible for maintaining the Insertion order in the
LinkedHashSet object.
- LinkedHashMap internally used doubly linkedlist to store the
elements.
- Please refer to the below example.
package simplifiedjava.crackedInterview; import java.util.LinkedHashSet; public class LinkedHashSetDemo { public static void main(String[] args) {
LinkedHashSet<String> set = new
LinkedHashSet<String>();
set.add("Sun");
set.add("Mon");
set.add("Tue");
set.add("Wed");
set.add("Sun");
System.out.println(set);
}
} |
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.
What happen if hashmap object gets
full?
- Default size of the hashmap is 16 and the load factor is 75%.
- If the load factor is filled 75% then internally hashmap size gets changed
like other data structures like ArrayList, vector etc.
- Hashmap re-size itself by creating a new bucket array of size twice the
previous size of hashmap and then start putting all old elements into the
new bucket array this process is called rehashing.
What is the time Complexity for
map.get()?
- The time complexity of hashmap for get operation and put operation is
generally O(1).
- O(1) certainly isn't guaranteed - but it's usually what you should assume
when considering which algorithms and data structures to use.
What is the difference between Iterator
and Iterable?
|
Iterator |
Iterable |
1. |
Iterator has three methods to iterate.
1. hasNext();
2. next();
3. remove();
|
Iterable has only one method.
1. Iterator(); |
2. |
Return type of
1. hasNext() method is Boolean.
2. Next() method is E.
3. Remove method is void. |
Return type of iterator() method is Iterator<T> (T for
Type). |
Iterator of Concurrent Collection
object is Fail Safe or Fail Fast?
- Iterators of Concurrent Collections are Fail-Safe.
- Fail-safe means iterator doesn’t fail if you are trying to modify the
element at the time of iteration.
- The Iterator will not fail while updating Concurrent Collection objects at
the time of iteration.
How will you crate a parameterized
class using generics?
- Please refer to the below example.
package simplifiedjava.crackedInterview; public class GenericTemplate<T> { T obj;
public
GenericTemplate(T obj) {
this.obj = obj;
}
public
T getobj() {
return obj;
}
public void
show() {
System.out.println("Type of Object "+ obj.getClass().getName());
} } package simplifiedjava.crackedInterview; public class GenericDemo {
public static void
main(String[] args) {
GenericTemplate<String> str = new
GenericTemplate<String>("Yogesh");
str.show();
System.out.println(str.getobj());
GenericTemplate<Integer> numbers = new
GenericTemplate<Integer>(100);
numbers.show();
System.out.println(numbers.getobj());
GenericTemplate<Double> dblNum = new
GenericTemplate<Double>(25.50);
dblNum.show();
System.out.println(dblNum.getobj());
}
}
Output:
Type of Object java.lang.String
Yogesh
Type of Object java.lang.Integer
100
Type of Object java.lang.Double
25.5 |
What are the PC Registers? What is the
role of PC registers in JVM?
- PC stands for Program Counter.
- If that method is not native, the pc register contains the address of the
Java Virtual Machine instruction currently being executed.
- If the method currently being executed by the thread is native, the value
of the Java Virtual Machine's pc register is undefined.
- The Java Virtual Machine's pc register is wide enough to hold a returnAddress or a native pointer on the specific platform.
- 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
Upcoming questions in next post:
21. What is Thread Starvation.
22. What is the difference between Starvation and Deadlock.
23. Annonymous class means name without a class then how will you create a object without using name.
24. What is NavigableSet?
25. Can we insert null in TreeSet. If not then why it is not possible.
26. What will happen while iterating any collection object including map trying to perform some operations like read or write on the object.
27. What is Generics? Can you list out some advantages of Generics.
28. What is the difference between HashMap and SortedMap.
29. What is singleton class? Write a program for the same?
30. Will you consider Array is bydefault generics. If yes then why.
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