Part 3
What is Thread Starvation?
- Starvation is a situation where the thread is in a waiting state for a very
long time because it is not getting access to shared resources because
higher priority threads are coming.
- When all the low priority processes got blocked, while the high priority
processes execute then this situation is termed Starvation.
- This happens when shared resources are made unavailable for long periods by "greedy" threads.
What is the difference between Deadlock and Starvation.
|
Deadlock |
Starvation |
1. |
Deadlock is a situation that occurs when one of the processes got
blocked. |
When all the low priority processes got blocked, while the high
priority processes execute then this situation is termed as
Starvation. |
2. |
There is starvation in every deadlock. |
Not every starvation needs to be a deadlock. |
3. |
Deadlock is an infinite process. |
Starvation is a long waiting but it is not an infinite
process.
|
Anonymous class means name without a
class then how will you create an object without using name?
- Please refer to the below example.
package simplifiedjava.crackedInterview;
public class
AnonymousClass {
public void
eat() {
System.out.println("Ear First Time.");
}
}
package
simplifiedjava.crackedInterview; public class AnnonymousInnerClassDemo { public static void main(String[] args) {
AnonymousClass anonymous = new
AnonymousClass() {
public void
eat() {
System.out.println("Second Time");
}
};
anonymous.eat();
}
}
Output: Second Time |
What is NavigableSet?
- The Java NavigableSet interface, java.util.NavigableSet, is a subtype of
the Java SortedSet interface.
- Therefore the NavigableSet behaves like a SortedSet, but with an additional
set of navigation methods available in addition to the sorting mechanisms of
the SortedSet.
- A NavigableSet may be accessed and traversed in either ascending or
descending order.
- The descendingSet() method returns a view of the set with the senses of all
relational and directional methods inverted.
- NavigableSet reverse = original.descendingSet(); also returns the reverse
order.
- This interface additionally defines methods pollFirst and pollLast that
return and remove the lowest and highest element, if one exists, else
returning null.
- Methods subSet, headSet, and tailSet differ from the like-named SortedSet
methods in accepting additional arguments describing whether lower and upper
bounds are inclusive versus exclusive.
- Implementation class of NavigableSet is java.util.TreeSet class.
- Subsets of any NavigableSet must implement the NavigableSet interface.
- Create a NavigableSet.
1. NavigableSet navigableSet = new TreeSet();
- Please refer to the below example.
package simplifiedjava.crackedInterview;
import
java.util.NavigableSet; import java.util.TreeSet; public class NavigableSetDemo {
public static void
main(String[] args) {
NavigableSet<Integer> set = new
TreeSet<Integer>();
set.add(50);
set.add(10);
set.add(30);
set.add(20);
set.add(40);
System.out.println("Actual Set : " + set);
System.out.println("Lower value of 30 using lower()= "+ set.lower(30));
System.out.println("Higher value of 30 using floor()= "+ set.floor(30));
System.out.println("Lowest value of 30 using ceiling()="+ set.ceiling(30));
System.out.println("Lowest value of 30 using higher()="+ set.higher(30));
System.out.println("Lowest value of 30 using pollFirst()="+ set.pollFirst());
System.out.println("Lowest value of 30 using pollLast()="+ set.pollLast());
NavigableSet<Integer> reverse = set.descendingSet();
System.out.println("Reverse Set : " + reverse);
}
}
Output:
Actual Set : [10, 20, 30, 40, 50]
Lower value of 30 using lower() =
20
Higher value of 30 using floor() =
30
Lowest value of 30 using ceiling() = 30
Lowest value of 30 using higher() = 40
Lowest value of 30 using pollFirst() = 10
Lowest value of 30 using pollLast() = 50 Reverse Set : [40, 30, 20] |
Can we insert null in TreeSet? If not
then why it is not possible?
- We cannot insert null in TreeSet.
- TreeSet internally stores the elements according to some
sorting order.
- While inserting records into the tree set JVM always compare
the two values and while comparing you might get NullPointerException.
What will happen while iterating any
collection object including map trying to perform some operations like
read or write on the object?
- It will throw ConcurrentModificationException.
What is Generics? Can you list out some
advantages of Generics?
- Generic provides type safety and resolve type casting
problems.
- Advantages of Generics.
1. No explicit type casting is required.
2. Reduce chances to throw ClassCastException.
3. With the help of generics, we can identify which type of list
is there.
4. If you are trying to add a heterogeneous object then you will
get a compile-time error.
- Please refer to the below example.
package simplifiedjava.crackedInterview;
import
java.util.ArrayList; import java.util.List; public class GenericsDemoForStringList {
public static void
main(String[] args) {
List<String> list = new
ArrayList<String>();
list.add("Shweta");
list.add("Shruti");
list.add("Arpita");
System.out.println(list);
}
} Output: [Shweta, Shruti, Arpita] If you are trying to add other thanString you will get compile time error.
|
What is the difference between HashMap
and SortedMap?
|
HashMap |
SortedMap |
1. |
HashMap is a concrete Class. |
SortedMap is an interface. |
2. |
HashMap never preserves the insertion order. |
SortedMap implementation class preserves the natural sorting
order. |
What is Singleton Class? Write a
program for the same?
- Singleton means only one object can be created.
- Following are the steps that need to take to create a
Singleton class.
1. Create a class variable with private and static.
2. Create a private constructor.
- Please refer to the below example.
1. Lazy Loading package simplifiedjava.crackedInterview; public class Singleton { private static Singleton INSTANCE;
private
Singleton() { }
public static
Singleton getInstance() {
if(INSTANCE == null) {
INSTANCE = new
Singleton();
}
return INSTANCE; }
public void
m1() {
System.out.println("m1() method called.");
} } package simplifiedjava.crackedInterview; public class SingletonDemo {
public static void
main(String[] args) {
Singleton obj
= Singleton.getInstance();
System.out.println(obj1.hashCode());
System.out.println(obj2.hashCode());
obj1.m1();
}
}
Output:
2018699554
2018699554
m1() method called.
2. Eagar loading. package simplifiedjava.crackedInterview; public class Singleton { private static Singleton INSTANCE = new Singleton();
private
Singleton() { }
public static
Singleton getInstance() {
if(INSTANCE != null) {
return INSTANCE;
}
return null; }
public void
m1() {
System.out.println("m1() method called.");
} } package simplifiedjava.crackedInterview; public class SingletonDemo {
public static void
main(String[] args) {
Singleton obj
= Singleton.getInstance();
System.out.println(obj1.hashCode());
System.out.println(obj2.hashCode());
obj1.m1();
obj.m1();
}
}
Output:
2018699554
2018699554
m1() method called. |
Will you consider Array is bydefault
generics? If yes then why?
- Yes, I will consider Array as by default generic class.
- Array never accepts heterogeneous objects, Array always accepts homogeneous
objects.
- If any collection object has the same type of object then it will never through ClassCastException.
- 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:
31. What will be the impact if hashcode() method return constant value everytime. What will be the size of hashmap. E.g.In Employee class I have overridden equals and hashcode method and hashcode method always return 1. I have 5 employee objects in hashmap. What will be the size.
32. What will be the impact if I don't override equals() method.
33. Why String and wrapper classes are considered good keys?
34. What is LinkedHashMap? Can you explain internal working of LinkedHashMap.
35. Why map is not a part of collection hierarchy. or Why map doesn't extends Collection interface.
36. Can you write a code to iterate a hashMap object.
37. What is hash-collision in Hashtable and how will you handled in Collection API.
38. Which data structure has implemented for HashMap.
39. Which data structure has implemented for TreeMap.
40. Can I convert Map into List?
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