Part 3
Core java interview questions and answers for 3-5 years experience covered in this post:
Can constructor have access specifiers? Which access specifiers are applicable to a constructor?
- Yes of course. Constructors can have access specifiers.
- Following access specifiers are applicable to the
constructors.
1. Public
2. Private
3. Protected
4. Default
- Following access specifiers are not applicable to the
constructors.
1. Abstract
2. Final
3. Native
4. Static
5. Synchronized.
How should be the sequence of List of Exception classes in the catch block?
- When you are declaring multiple catch blocks then the child class must
declares first and then its parent and then parents parent and so on.
- Please refer to the below example for more clarity.
package
simplifiedjava.crackedInterview; public class MultipleCatchBlockDemo {
public
static
void
main(String[] args) { try {
}catch(ArrayIndexOutOfBoundsException ae) {
ae.printStackTrace();
}catch(IndexOutOfBoundsException ie) {
ie.printStackTrace();
}catch(Exception e) {
e.printStackTrace();
}
}
}
Explanation:
ArrayIndexOutOfBoundsException is a child class of
IndexOutOfBoundsException and IndexOutOfBoundsException is a child
class of Exception class. If you shuffle then you will get a
compile-time error. Most child classes should be first and then next
child and then parent class. |
What is the difference between interrupted() and isInterrupted() method in java?
|
Interrupted() |
isInterrupted() |
1. |
interrupted() method is a static method of class thread checks
the current thread and clear the interruption "flag". i.e. a
second call to interrupted() will return false. |
isInterrupted() method is an instance method; it reports the
status of the thread on which it is invoked. it does not clear the
interruption flag. |
2. |
Syntax:
if(t.interrupted()) {
System.out.println("inside interrupted");
} |
Syntax:
if(t.isInterrupted()) {
System.out.println("inside isInterrupted.");
} |
Can you serialize private and static variables?
- Private Variable: We can serialize private variables. There is no
concern at all whether a variable is private, public or protected. It is
simply used for serialization because it’s a part of an object.
- Static Variable: Static variables belong to a class, not an
object. So static variables are not a part of the object state. So static
variables cannot consider for serialization.
Can we declare inner class as a private?
- Inner class can be declared as a private but cannot declare outer class
as a private class.
What is the purpose of Arrays utility class?
- Array utility class contained in java.util.Arrays package.
- This class provides some utility methods for manipulating the arrays like
sorting, searching, converting an array into a collection etc.
What is the difference between HashSet and SortedSet?
|
HashSet |
SortedSet |
1. |
HashSet is a concrete class. |
SortedSet is an interface. |
2. |
HashSet doesn’t preserve insertion order or doesn’t insert order
according to some type of sorting. |
SortedSets implementation classes insert the records according to
some natural sorting order. |
3. |
The underlying data structure is HashTable. |
The underlying data structure is a RED-BLACK tree. |
4. |
The complexity of HashSet is O(1) meaning it will do basic
operations independent of the size of input data in a constant
time. |
The complexity of SortedSet is log(N) meaning depending on the
size of input it will do the basic operations logarithmic. |
What is the difference between HashMap and ConcurrentHashMap?
|
HashMap |
ConcurrentHashMap |
1. |
HashMap is not thread-safe. |
ConcurrentHashMap is thread-safe. |
2. |
Only one null key and multiple null values are allowed. |
ConcurrentHashMap doesn’t allow null keys or values either. It
will throw NullPointerException at runtime. |
3. |
The performance of HashMap is high as compared to
ConcurentHashMap because multiple threads can access the same
object. |
The performance of ConcurrentHashMap is relatively low because it
is thread-safe. |
4. |
Thread gets a lock of the whole object and then execute. |
In ConcurrentHashMap thread won’t take whole objects lock, Thread
will get Segment lock or bucket lock. |
5. |
If multiple threads are trying to perform write or read
operations on the same collection object then we will get
ConcurrentModifcationException. |
Multiple threads are allowed to perform a read operation and 16
or more threads are allowed to perform the write operation. |
6. |
HashMap has only one lock available. |
ConcurrentHashMap can have one or multiple locks we call it a
Concurrency level. |
7. |
Iterator is Fail-Fast. |
Iterator is Fail-Safe. |
8. |
When multiple threads access the same resource at a time then
iterator fails immediately is called Fail-Fast. |
When multiple threads access the same resources at a time,
Iterator won’t fail is called Fail-Safe Iterator. |
How Stack and Heap correlated to each other while creating object?
- JVM has bifurcated memory into two parts. The first one is stack memory
and the second one is heap memory.
- Stack memory is mainly used for storing the order of method execution and
local variables.
- Heap memory is mainly used for storing actual objects created with a new
keyword.
- Stack and heap memory are correlated while managing the objects life
cycle. Once created object on heap memory will be referenced to the
reference variable which is stored on stack memory.
What are the pre-defined functional
interfaces?
- Java 8 provides some functional interfaces inbuilt are called pre-defined
functional interfaces.
- Following are the pre-defined functional interfaces.
1. Predicate functional interface.
2. Function functional interface.
3. Consumer functional interface.
4. Supplier functional interface.
5. Bi-Predicate functional interface.
6. Bi-Function functional interface.
- 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