Java Interview Questions and Answers covered in this post:
What is the difference between Composition and Aggregation?
|
Composition |
Aggregation |
|
||
1. |
Tightly couple relationship. |
Loosely coupled relationship. |
2 |
|
|
Can we overload or override the main method?
- We can overload the main() method but we can’t override the main() method.
The main () method can’t override because the static method can’t be
overridden. The static method belongs to the class, not the object.
package simplifiedjava.crackedInterview; public class Parent {
public
static
void
main(String[] args) {
// TODO
Auto-generated method stub }
public
static
void
main(String[] args, int
num) {
// TODO
Auto-generated method stub
} } package simplifiedjava.crackedInterview; public class Child extends Parent{
public
static
void
main(String[] args) {
// TODO
Auto-generated method stub
}
} |
What is the difference between String s = "abc" and String s = new
String("abc")?
- String s=”abc”: On this line String object will not create on heap
memory. ‘abc’ string will be directly stored on the string constant
pool.
- String s = new String(“abc”): new String(“abc”): On this line String object will be created on heap memory and ‘abc’ will be stored in string constant pool.
What is SerialVersionUID? Who provides this SerialVersionUID? How will
you create it?
- SerialVersionUID is a unique identifier of the class.
- SerialVersionUID represent the version of the class. This comes into the
picture while serialization and deserialization.
- JVM uses it to compare the versions of the class ensuring that the same
class was used during Serialization is loaded during Deserialization.
- the default serialVersionUID computation is highly sensitive to class
details that may vary depending on compiler implementations, and can thus
result in unexpected InvalidClassExceptions during deserialization.
- You can refer the below image to generate the SerialVerionUID.
What is the difference between Annonymous Inner class and Lambda
Expression?
|
Annonymous Inner class |
Lambda Expression |
1. |
It is a class without a name. |
It is a function without a name. |
2. |
An anonymous inner class can extend an abstract class or concrete
class. |
Lambda expression cannot extend an abstract class or concrete
class. |
3. |
An anonymous inner class can implement an interface that has
multiple abstract methods as well as a functional interface. |
Lambda expression can implement only a functional interface. |
4. |
Inside the inner class, we can declare an instance variable. |
Inside lambda expression, we can declare only local variables. |
5. |
An anonymous inner class can be instantiated. |
Lambda expression cannot be instantiated. |
6. |
An anonymous interface is the best choice if we want to handle
multiple methods. |
Lambda expression is the best choice if we want to handle the
interface with a single abstract method. |
7. |
Inside the Anonymous inner class, ‘this’ always refers current
anonymous inner class object but not the outer class object. |
Inside lambda expression, this always refers current outer class
object. |
8. |
For an anonymous inner class at the time of compilation, a
separated .class file will be generated. |
For the lambda expression, separate .class file won’t be
generated. |
Explain the internal working of HashMap?
- Hashmap internally stores mapping in the form of Map.Entry object which
contains key and value.
- Hashmap works on hashing principle.
- We use the put() method to insert elements in hashmap and get() method to
retrieve elements from the hashmap object.
- Internal working of inserting an element in hashmap.
1. When we use the put() method to insert key and value objects
into a hashmap, internally hashmap uses hashing technique to identify the
bucket reference.
2. Once the bucket is identified object is stored in Map.Entry
object in the form of key and value.
3. It may happen multiple objects can have the same hashcode so
multiple objects will be stored in the same bucket.
4. If one bucket has multiple objects then those objects are
stored in the form of a LinkedList.
5. We must override hashcode() and equals() method.
6. Hashcode() method calculates the hashcode as an integer value
and the equals() method is used to identify the key if the key is matching
then it will replace the existing element otherwise that element will be
stored next to the current element.
- Internal working of retrieving element in hashmap.
1. When you want to retrieve an object from hashmap then you can
call the get() method and pass the key object.
2. Hashmap generates the hashcode for that key and finds the
appropriate bucket.
3. If there is only one element available in that bucket then
simply that element will be returned.
4. If we want to retrieve an object from this linked list, we
need an extra check to search for the correct element, this is done by the
equals() method.
- Please refer to the below example.
package simplifiedjava.crackedInterview; import java.util.HashMap; public class HashMapDemo { public static void main(String[] args) {
HashMap<Integer, String> hMap = new
HashMap<Integer, String>();
hMap.put(1, "One");
hMap.put(10, "Ten");
hMap.put(100, "Hundred"); hMap.put(1000, "Thousand");
System.out.println(hMap);
System.out.println(hMap.get(100));
}
}
Output:
{1=One, 100=Hundred, 1000=Thousand, 10=Ten} Hundred |
Graphical Representation of Hashmap.
Can we store objects in decending order in SortedMap? If yes then, how
can I store objects in decending order in SortedMap?
- We can store objects in descending order in SortedMap.
- There are a couple of ways we can do it in the case of
SortedMap.
1. The first way is to implement Comparator in TreeMap Constructor.
2. The second way is you can use the Collections class reverseOrder()
method.
- Please refer to the below two examples.
1. Using Comparator Interface. package simplifiedjava.crackedInterview;
import
java.util.Collections;
import
java.util.Comparator;
import
java.util.SortedMap; import java.util.TreeMap; public class SortedMapDemo {
public static void
main(String[] args) {
SortedMap<Integer, String> sMap = new
TreeMap<>(new
DecendingOrder());
sMap.put(1000, "Thousand");
sMap.put(1, "One");
sMap.put(10000, "Ten Thousand");
sMap.put(100, "Hundred");
sMap.put(10, "Ten");
System.out.println(sMap);
} } class DecendingOrder implements Comparator<Integer>{
@Override
public int
compare(Integer i1, Integer i2) {
return i2.compareTo(i1);
}
} Output: {10000=Ten Thousand, 1000=Thousand, 100=Hundred, 10=Ten, 1=One} 2. Using reverseOrder() of Collections class. package simplifiedjava.crackedInterview;
import
java.util.Collections;
import
java.util.Comparator;
import
java.util.SortedMap; import java.util.TreeMap; public class SortedMapDemo {
public static void
main(String[] args) {
SortedMap<Integer, String> sMap = new
TreeMap<>(Collections.reverseOrder());
sMap.put(1000, "Thousand");
sMap.put(1, "One");
sMap.put(10000, "Ten Thousand");
sMap.put(100, "Hundred");
sMap.put(10, "Ten");
System.out.println(sMap);
}
}
Output:
{10000=Ten Thousand, 1000=Thousand, 100=Hundred, 10=Ten,
1=One} |
What is the difference between HashSet and LinkedHashSet?
|
HashSet |
LinkedHashSet |
1. |
HashSet implements using a HashTable. |
LinkedHashSet implements using LinkedList + HashSet. |
2. |
Insertion order never preserved. |
LinkedHashSet insertion order preserved. |
3. |
Unique elements without insertion order. |
Unique elements with insertion order. |
4. |
Introduced in Java 1.2 |
Introduced in Java 1.4 |
What is the difference between Traditional Collection and Concurrent
Collection?
|
Traditional Collection |
Concurrent Collection |
1. |
All traditional collections are not thread-safe. |
All Concurrent collections are thread-safe. |
2. |
Thread-safe traditional collections performance is low as compared
to Concurrent collections. |
Concurrent Collections performance is high as compared to the
traditional collection. |
3. |
While one thread is executing the collection object no other thread
is allowed to access the same collection object. |
In Concurrent collection, while one thread is executing a
collection object another thread is allowed to access the same
collection object. |
4. |
ConcurrentModificationException can be thrown when multiple threads
execute the same resources. |
ConcurrentModificationException cannot occur when multiple threads
executing the same resources. |
5. |
Traditional Thread-safe classes are follows.
1. Vector
2. Stack
3. Hashtable
4. Properties |
Concurrent collections are as follows.
1. ConcurrentHashMap
2. CopyOnWriteArrayList
3. CopyOnWriteArraySet |
How would you implement Multiple Inheritance in java?
- One way you can implement multiple interfaces.
- The second one is, one interface extends multiple interfaces and class implements the first 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
Upcoming questions in next post:
1. What are the access modifiers and its scope?
2. Can you explain public static void main(String[] args) Method.
3. What is ternary operator in java.
4. Can you specify the scenario where you can implement String, StringBuffer and StringBuilder.
5. How to achieve Thread safety in java.
6. What is deadlock and How to avoid a situation of dead lock.
7. What is NotSerializableException. When it can be thrown.
8. What is HashSet? Can you explain internal working of Hashset?
9. What is rehashing?
10.What will happen if I declared multiple abstract methods in a functional interface.
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