Part 1
What is the difference between
Synchronized and ReentrantLock?
|
Synchronized |
ReentrantLock |
1. |
Synchronized does not support fairness.
Synchronized never offer chance longest waiting thread. |
ReentrantLock support fairness. ReentrantLock has fairness property
which provides a lock to the longest waiting thread. |
2. |
Synchronized doesn’t provide any method like tryLock(). |
ReentrantLock provides the tryLock() method which acquires a lock
only if it's available or not held by any other thread. |
3. |
In a Synchronized mechanism, a thread can be blocked for an
infinite period. |
In ReentrantLock thread cannot be blocked for infinite time because
ReentrantLock provides a lockInterruptibly()method which can be used
to interrupt the thread.
|
Can we customize serialization process?
Can you override default serialization process if yes then how. Can you
tell me the steps?
- Yes, We can customize the Serialization.
- We can use Externalization to customize the
serialization.
- Steps needs to implement for Externalization.
1. A class must implement an Externalizable interface.
2. We have to override writeExternal(ObjectOutputStream ooo) and
readExternal(ObjectOutputStream oos).
3. Inside the main method, we have to create a file object to
store the serialised data.
4. Create FileOutputStream object to write the file. We have to
pass the file object in the FileOutputStream class constructor.
5. Create ObjectOutputStream object to and pass fileOutputstream
object to its constructor.
6. With the ObjectOutputStream object, you can write the
data.
Can we access all members of outer
class including private in inner class?
- Yes, we can access all members including private members of the outer class in the inner class.
What is the default size of ArrayList?
What will happen if ArrayList gets fulled?
- Default size of ArrayList is 10.
- Once ArrayList reaches its max capacity then a new ArrayList object will be
created with the new size and copy all the elements from the old ArrayList
to the new ArrayList. Reference variable pointing to new ArrayList and old
ArrayList gets deleted.
- Old ArrayList will be eligible for garbage collection.
- Java people set some formula to create new space for array object which is
newly created.
- New Capacity = ((Current Capacity * 3) / 2)+ 1
What is importance of emptySet() method
in collection framework?
- An emptySet() method can be used to get the set which is empty.
What will happen if two different
objects have the same hashcode?
- If the two different object have same hashcode, bucket location would be
same that time collision will occur in HashMap Since HashMap uses LinkedList
to store object internally, this entry (object of Map.Entry contains key and
value) will be stored in LinkedList.
Can we use any custom object as a key
in HashMap?
- We can set any custom object as a key in the hashmap.
- You have to override hashcode() and equals() methods into
that class.
- Please refer to the below example.
package simplifiedjava.crackedInterview; public class Employee {
private
String empName;
private
String dept;
private
Double salary; private String designation;
public
Employee(String empName, String dept, Double salary, String designation) {
super();
this.empName = empName;
this.dept = dept;
this.salary = salary;
this.designation = designation;
}
public
Employee(String empName) {
this.empName = empName;
}
public
String getEmpName() {
return empName;
}
public
String getDept() {
return dept;
}
public void
setDept(String dept) {
this.dept = dept; }
public
Double getSalary() {
return salary;
}
public void
setSalary(Double salary) {
this.salary = salary;
}
public
String getDesignation() {
return designation;
}
public void
setDesignation(String designation) {
this.designation = designation;
}
public void
setEmpName(String empName) {
this.empName = empName;
}
@Override
public
String toString() { return "Employee [empName=" + empName + ", dept=" + dept + ", salary=" + salary + ", designation=" + designation + "]";
}
@Override
public int
hashCode() {
final int prime
= 31;
int result
= 1;
result = prime * result
+ ((dept == null) ? 0 : dept.hashCode());
result = prime * result
+ ((designation == null) ? 0 : designation.hashCode());
result = prime * result
+ ((empName == null) ? 0 : empName.hashCode());
result = prime * result
+ ((salary == null) ? 0 : salary.hashCode());
return result;
}
@Override
public boolean
equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if
(getClass() != obj.getClass())
return false;
Employee other
= (Employee) obj;
if (dept == null) {
if (other.dept != null)
return false;
} else if (!dept.equals(other.dept))
return false;
if (designation == null) {
if (other.designation != null)
return false;
} else if (!designation.equals(other.designation))
return false;
if (empName == null) {
if (other.empName != null)
return false;
} else if (!empName.equals(other.empName))
return false;
if (salary == null) {
if (other.salary != null)
return false;
} else if (!salary.equals(other.salary))
return false;
return true;
} } package simplifiedjava.crackedInterview; import java.util.HashMap; public class HashMapEmpHashCodeDemo {
public static void
main(String[] args) {
HashMap<Employee,Integer> empMap = new
HashMap<Employee,Integer>();
empMap.put(new
Employee("Yogesh","IT",100000.00,"System Analyst"),101);
empMap.put(new
Employee("Arpita","Mangement",1200000.00,"Trustee"),102);
empMap.put(new
Employee("Shweta","DevOps",45000.00,"Jenkin Engineer"),103);
empMap.put(new
Employee("Shruti","IT",65000.00,"DB Admin"),104); empMap.put(new Employee("Priyanka","IT",35000.00,"Test Engineer"),105);
System.out.println(empMap.size());
}
}
Output: 5 |
What is the difference between peek()
and poll() method of Queue?
|
Peek() |
Poll() |
1. |
Only return the head element of the queue without removing it. |
Only return the head element of the queue and removed it as
well. |
What is the Iterator and why do we
require iterator in Collection framework?
- Basically, Iterator is an interface.
- Iterator can be used to traverse throughout the collection
object.
- Iterator is a universal iterator that can apply to all types of collection
like list, set, map.
- The iterator has three methods to perform the operation.
1. hasNext();
2. next();
3. remove();
What the the new methods introduced in
ConcurrentHashMap to performing read and write operation?
- There are 3 methods has introduced in ConcurrentHashMap which
are as follows.
1. putIfAbsent(): If the key is present then there won’t
be any change in collection.
package simplifiedjava.crackedInterview; import java.util.concurrent.ConcurrentHashMap; public class ConcurrentHashMapPutIfAbsentDemo { public static void main(String[] args) {
ConcurrentHashMap<Integer, String> map = new
ConcurrentHashMap<Integer, String>();
map.put(101,"Shweta");
map.put(102,"Shruti");
map.put(103,"Yogesh");
map.putIfAbsent(103,"Arpita"); // In this case 103 key was present
System.out.println(map);
map.putIfAbsent(104,"Ananya"); // In this case 104 key was not present.
System.out.println(map);
}
}
Output:
{101=Shweta, 102=Shruti, 103=Yogesh} {101=Shweta, 102=Shruti, 103=Yogesh, 104=Ananya} |
2. remove(): This method won’t remove elements until and unless key and value match.
package simplifiedjava.crackedInterview; import java.util.concurrent.ConcurrentHashMap; public class ConcurrentHashMapRemoveDemo {
public static void
main(String[] args) {
ConcurrentHashMap<Integer, String> map = new
ConcurrentHashMap<Integer, String>();
map.put(101,"Shweta");
map.put(102,"Shruti");
map.put(103,"Yogesh");
map.remove(103,"Arpita"); // In this case 103 key is present but value was present.
System.out.println(map);
map.remove(103,"Yogesh"); // In this case key 103 and value Yogesh was present so
removed.
System.out.println(map);
} }
Output:
{101=Shweta, 102=Shruti, 103=Yogesh}
{101=Shweta, 102=Shruti} |
3. replace(): This method won’t replace value until and
unless key and value match.
package simplifiedjava.crackedInterview; import java.util.concurrent.ConcurrentHashMap; public class ConcurrentHashMapReplaceDemo {
public static void
main(String[] args) {
ConcurrentHashMap<Integer, String> map = new
ConcurrentHashMap<Integer, String>();
map.put(101,"Shweta");
map.put(102,"Shruti");
map.put(103,"Yogesh");
map.replace(103,"Arpita");
System.out.println(map);
map.replace(103,"Arpita","Updated Arpita");
System.out.println(map);
}
}
Output:
{101=Shweta, 102=Shruti, 103=Arpita}
{101=Shweta, 102=Shruti, 103=Updated Arpita} |
- 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:
11. Why wait(), notify() and notifyAll() methods are in Object class even these methods are used in multithreading.
12. Can we access non-static class members inside static inner class.
13. Explain the internal working of LinkedHashSet.
14. Explain the defined method in Queue.
15. What happen if hashmap object gets full?
16. What is the time Complexity for map.get();
17. What is the difference between Iterator and Iterable.
18. Iterator of Concurrent Collection object is fail safe or fail fast.
19. How will you crate a parameterized class using generics.
20. What are the PC Registers. What is the role of PC registers in JVM.
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