Part 11
Core java interview questions and answers for 3-5 years experience covered in this post:
How do you share the data between two
threads?
- We should declare such variables as
static and
volatile.
- Volatile variables are shared across multiple threads. This means that
individual threads won’t cache their copy in the thread-local. But every
object would have its own copy of the variable so threads may cache value
locally.
- We know that static fields are shared across all the objects of the class,
and it belongs to the class and not the individual objects. But, for static
and non-volatile variables also, threads may cache the variable locally.
Which changes are considered compatible
and incompatible in serialization mechanism?
-
Compatible Changes are as follows.
1.
Changing access modifier:
If you change public, protected, default, private there will be no effect on
serialization.
2.
Adding New fields:
Adding new fields is also compatible. There is no restriction to adding new
fields in serialization.
3.
Changing the field from static to non-static:
It is kind of adding new fields in serialization.
4.
Changing the field from transient to non-transient:
If you are removing the transient keyword then that field will be eligible
for serialization. It’s like adding a new field that is compatible with
change.
-
Incompatible Changes are as follows.
1. Changing the field from non-static to static: It is equivalent to deletion of the filed.
2. Changing the field from non-transient to transient:
It is equivalent to deletion of the field.
Can you declare local inner class as a
static class?
- No, we cannot declare local an inner class as a static class.
What is LinkedHashSet? What is the
purpose of LinkedHashSet?
- LinkedHashSet is a child class of HashSet but, LinkedHashSet preserves the
insertion order.
- LinkedHashSet is a combination of LinkedList and HashSet.
- You can say LinkedHashSet is the ordered version of HashSet.
- LinkedHashSet doesn’t have any method, it inherits all the methods from its
superclass i.e. HashSet.
- If you want to maintain the insertion order then you can go for LinkedHashSet.
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 Properties Class? What is the
role of properties class?
- Properties class is used to read the values from multiple sources like
config file, properties file etc.
- The main advantage of this approach is if there is a change in the
properties file to reflect that change in java class then just re-deployment
is required, Not required to rebuild the project.
- In our project, if any changes are required frequently like username,
password, the port number is not recommended to hardcode in the java
program, If there is any change to reflect that change requires
recompilation, rebuild and redeployment. Even sometimes server restart may
require which may impact the business.
- To overcome this problem we can set values in the properties file and then
just redeploy is required if any change is there.
- Please refer to the below examples.
1. In the first example, we are reading values from the properties file.
2. In the second example, we are setting values to the
properties file.
1. First example we are reading values from properties file. package simplifiedjava.crackedInterview;
import
java.io.FileInputStream;
import
java.util.Enumeration; import java.util.Properties; public class PropertiesFileDemo {
public static void
main(String[] args){ Properties properties = new Properties();
try {
FileInputStream fis = new
FileInputStream("G:\\Core
Java\\InterviewPrograms\\src\\javaproperties.properties");
properties.load(fis);
} catch
(Exception e) {
System.out.println("File Not Found");
e.printStackTrace(); }
Enumeration enumeration = properties.propertyNames();
while(enumeration.hasMoreElements()) {
String propName
= (String)enumeration.nextElement();
String value = properties.getProperty(propName);
System.out.println(propName + " Value is = " + value);
}
}
}
Output:
port Value is = 8080
password Value is = yogi@12345 username Value is = yogi 2. Second example we are setting values to properties file. package simplifiedjava.crackedInterview;
import
java.io.FileOutputStream;
import
java.io.IOException; import java.util.Properties; public class PropertiesFileDemo {
public static void
main(String[] args){ Properties properties = new Properties();
try {
FileOutputStream fos = new
FileOutputStream("G:\\Core
Java\\InterviewPrograms\\src\\javaproperties.properties");
properties.setProperty("url", "thin:driver:oracle:4848");
properties.store(fos, "URL added");
} catch
(IOException e) {
e.printStackTrace();
}
}
}
Output:
javaproperties.properties:
#URL added
#Mon Nov 01 14:08:17 IST 2021
url=thin\:driver\:oracle\:4848 |
What is the return type of test method
in Predicate functional interface?
- Return type of test() method is Boolean.
- Please refer below example.
@FunctionalInterface
public interface
Predicate<T> {
boolean
test(T t);
} |
What is the return type of get() method
in Supplier functional interface?
- The return type of Supplier interface can be any type of object.
What is the difference between Consumer
and Bi-Consumer?
|
Consumer |
Bi-Consumer |
1. |
The consumer interface function just consumes a single value,
perform a certain operation and doesn’t return anything. |
Bi-Consumer interface function just consumes a couple of values,
perform a certain operation and doesn’t return anything. |
2. |
@FunctionalInterface
public
interface
Consumer<T>{
} |
@FunctionalInterface
public
interface
BiConsumer<T, U>{
} |
What is the purpose of Method
Reference?
-
Code reusability can be achieved with method reference.
-
Method reference means we use the readymade method.
- 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