Part 10
Core java interview questions and answers for 3-5 years experience covered in this post:
Can we convert a normal thread into a
daemon thread once the thread started or not started in both
cases?
- Yes, we can convert a normal thread into a daemon thread.
- Daemon thread is a very lightweight thread.
- But, a Normal thread can convert into a daemon thread but it is not
possible after the thread is running state. It is possible before the thread
is running state.
- Normal thread can convert by invoking setDaemon() thread.
Can you Serialize Singleton class and
at the time of deserialization object should be in the same state as it
was during serialization?
- Yes, we can serialize the singleton class.
- An object returned by deserialization process is in same state as it was
during the serialization process.
- We can implement readResolve() method to ensure that we don’t break
singleton pattern during deserialization.
- Signature of readResolve() method.
private
Object readResolve() throws
ObjectStreamException {
return INSTANCE;
} |
private void
readObject(ObjectInputStream ois) throws
IOException,ClassNotFoundException{
ois.defaultReadObject();
synchronized
(SingletonClass.class) {
if (INSTANCE == null) {
INSTANCE = this;
}
} } |
Can we declare local inner class with
Public, Private and Protected?
- No. Public, Private and Protected are not applicable to local
inner class.
- Only final, abstract and strictfp are applicable to local inner class.
How will you convert Array to ArrayList and vice a versa?
- Java has provided some readymade methods in utility class for such kinds of
operations.
- We have Arrays and Collections utility classes for these types of
operations.
- Arrays utility class has a couple of methods that convert the array to
ArrayList and ArrayList to array.
- Convert Array to ArrayList we can use Arrays.asList() method.
- Convert ArrayList to Array we can use Arrays.toArray() method.
- Please refer to the below examples.
package simplifiedjava.crackedInterview;
import
java.util.ArrayList; import java.util.List; public class ConvertArrayListToArrayDemo { public static void main(String[] args) {
List<String> nameList = new
ArrayList<String>();
nameList.add("Yogesh");
nameList.add("Arpita");
nameList.add("Shweta");
nameList.add("Shruti");
nameList.add("Rusty");
String[] updatedArray = new
String[nameList.size()];
updatedArray = nameList.toArray(updatedArray);
for(String s : updatedArray) {
System.out.println(s);
}
}
}
Output:
Arpita
Yogesh
Shweta Shruti package simplifiedjava.crackedInterview;
import
java.util.ArrayList; import java.util.List; public class ConvertArrayListToArrayDemo { public static void main(String[] args) {
List<String> nameList = new
ArrayList<String>();
nameList.add("Yogesh");
nameList.add("Arpita");
nameList.add("Shweta");
nameList.add("Shruti");
nameList.add("Rusty");
String[] updatedArray = new
String[nameList.size()];
updatedArray = nameList.toArray(updatedArray);
for(String s : updatedArray) {
System.out.println(s);
}
}
}
Output:
Yogesh
Arpita
Shweta
Shruti
Rusty |
Is it possible to store elements in
decending order in TreeMap?
- Yes, it is possible to store elements in treeMap in
descending order.
- There are a couple of ways we can store elements in
descending order which is as follows.
1. You can provide a Comparator object in the TreeMap
constructor.
2. You can use Collections.reverse() method to it.
package simplifiedjava.crackedInterview;
import
java.util.Collections;
import
java.util.Comparator; import java.util.TreeMap; public class TreeMapDemo { public static void main(String[] args) {
TreeMap<Integer, String> tMap = new
TreeMap<Integer, String>(new
ReverseOrderTreeElements());
tMap.put(100, "Hundred");
tMap.put(1, "One");
tMap.put(1000, "Thousand");
tMap.put(10, "Ten");
System.out.println(tMap);
} } class ReverseOrderTreeElements implements Comparator<Integer>{
@Override
public int
compare(Integer o1, Integer o2) {
return o2.compareTo(o1);
}
} Output: {1000=Thousand, 100=Hundred, 10=Ten, 1=One} package simplifiedjava.crackedInterview;
import
java.util.Collections;
import
java.util.Comparator; import java.util.TreeMap; public class TreeMapDemo { public static void main(String[] args) {
TreeMap<Integer, String> tMap = new
TreeMap<Integer,
String>(Collections.reverseOrder());
tMap.put(100, "Hundred");
tMap.put(1, "One");
tMap.put(1000, "Thousand");
tMap.put(10, "Ten");
System.out.println(tMap);
}
}
Output:
{1000=Thousand, 100=Hundred, 10=Ten, 1=One} |
What is the difference between
ArrayList and CopyOnWriteArrayList?
|
ArrayList |
CopyOnWriteArrayList |
1. |
ArrayList is not thread-safe. |
CopyOnWriteArrayList is thread-safe. |
2. |
In ArrayList, clone copy doesn’t create for every update
operation. |
In CopyOnWriteArrayList clone copy creates for each update
operation. |
3. |
While executing one thread no other thread allowed accessing the
same resources. If you try to modify then it will throw
ConcurrentModificationException. |
While executing one thread other threads are allowed to perform an
operation on the same resources it won’t throw
ConcurrentModificationException. |
4. |
Iterator is Fail-Fast. |
Iterator is Fail-Safe. |
5. |
While traversing the list using iterator we can perform a remove
operation. |
While traversing CopyOnWriteArrayList using iterator we cannot
perform removes operation otherwise, it will throw
UnSupportedOperationException. |
What is the difference between
NoSuchMethodError and NoSuchMethodException?
- NoSuchMethodException is thrown when you try and get a method that doesn’t
exist with the reflection.
- NoSuchMethodError is thrown when the virtual machine cannot find the method
you are trying to call. This happens when a particular method was present at
compile time but not available at run time.
What are the default methods defined in
Predicate functional interface?
- There are 3 default methods declared inside Predicate functional
interface.
1. Negate(): negate() returns the logical negation of
existing result.
package simplifiedjava.crackedInterview.java8;
import
java.util.Arrays;
import
java.util.List; import java.util.function.Predicate; public class PredicateDefaultMethodDemo {
public static void
main(String[] args) { List<String> list = Arrays.asList("Yogesh","Arpita","Shweta","Shruti","Ananya","Asmita","Yogita");
Predicate<String> p1
= s
-> s.startsWith("S"); Predicate<String> p3 = p1.negate();
for(String s : list) {
if(p1.test(s)) {
System.out.println(s);
}
}
}
} |
2. And(): And() works like logical && just combine two
conditions.
package simplifiedjava.crackedInterview.java8;
import
java.util.Arrays;
import
java.util.List; import java.util.function.Predicate; public class PredicateDefaultMethodDemo {
public static void
main(String[] args) { List<Integer> list = Arrays.asList(10,20,30,40,50,60,70,80,90,100);
Predicate<Integer> p1
= n
-> n
> 50; Predicate<Integer> p2 = n -> n < 100; Predicate<Integer> p3 = p1.and(p2);
for(Integer s : list) {
if(p3.test(s)) {
System.out.println(s);
}
}
}
}
Output:
60
70
80
90 |
3. Or(): Or() works like logical || just combine tow conditions.
package simplifiedjava.crackedInterview.java8;
import
java.util.Arrays;
import
java.util.List; import java.util.function.Predicate; public class PredicateDefaultMethodDemo {
public static void
main(String[] args) { List<String> list = Arrays.asList("Yogesh","Arpita","Shweta","Shruti","Ananya","Asmita","Yogita");
Predicate<String> p1
= s
-> s.startsWith("S"); Predicate<String> p2 = s -> s.startsWith("Y");
Predicate<String> p3
= p1.negate(); Predicate<String> p4 = p1.or(p2);
for(String s : list) {
if(p4.test(s)) {
System.out.println(s);
}
}
}
}
Output:
Yogesh
Shweta
Shruti Yogita |
What is the difference between Consumer
and Supplier?
|
Consumer |
Supplier |
1. |
Consumer interface always takes some input and perform a certain
operation and never return anything. |
The Supplier interface represents an operation that takes no
argument and returns a result. |
2. |
@FunctionalInterface
public interface
Consumer<T> {
void accept(T t);
} |
@FunctionalInterface
public interface
Supplier<T> {
T get();
} |
3. |
Default method is andThen() |
There is no default method in Supplier. |
What is Optional? What is the purpose
of Optional?
- An optional is a public final class that is used to deal with
NullPointerException in java applications.
- Optional class is mostly used in Stream API where we are applying some
filters and retrieved the filtered data.
- Optional class is mainly used for handling NullPointerException.
package
simplifiedjava.crackedInterview.java8;
import
java.util.Optional;
public class
OptionalClassDemo {
public static void
main(String[] args) {
String[] strArray = new
String[5];
strArray[0] = "Core";
strArray[1] = "Java";
strArray[2] = "Spring";
Optional<String> opt
= Optional.ofNullable(strArray[3]);
if(opt.isPresent()) {
System.out.println("Value is Present");
}else {
System.out.println("Value is not Present");
}
}
}
Output: Value is not Present |
1.
- 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:
101. How do you share the data between two threads.
102. Which changes are considered compatible and incompatible in serialization mechanism.
103. Can you declare local inner class as a static class.
104. What is LinkedHashSet? What is the purpose of LinkedHashSet.
105. What is Blocking Queue and What are the tyes of Blocking Queue.
106. What is Properties Class? What is the role of properties class?
107. What is the return type of test method in Predicate functional interface.
108. What is the return type of get() method in Supplier functional interface.
109. What is the difference between Consumer and Bi-Consumer.
110. What is the purpose of Method Reference.
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