Java Interview Questions and Answers covered in this post:
What do you mean by thread priority?
- Thread priorities are nothing but a number between 1 to
10.
- Each thread has some priority.
- Default priority is 5.
- Scheduler schedules the thread according to their
priority.
- There are 3 constants defined in thread priorities.
1. MIN_PRIORITY
2. NORM_PRIORITY
3. MAX_PRIORITY
What is static synchronization?
- Static synchronized method will acquire a lock of the class instead of an
object.
- Static belongs to the class, not the object so Thread acquires a lock
from class.
- Suppose there are multiple static synchronized methods (m1, m2, m3, m4)
in a class, and suppose one thread is accessing m1, then no other thread
at the same time can access any other static synchronized methods.
What is Serialization? Explain the need of serialization?
- Serialization is a process of converting the state of object into byte
stream.
- Serialization takes care by JVM.
- In Serialization it’s mandatory to save total object.
- Need for the serialization is as follow:
1. Transfer the state of object through the network.
2. Can save the state of object into file.
- Please refer to the below example.
package simplifiedjava.crackedInterview; import java.io.Serializable; public class Student implements Serializable {
private int id;
private static
String name; private String dept;
public
Student(int id, String name, String dept) {
super();
this.id
= id;
this.name
= name;
this.dept
= dept;
}
public int
getId() {
return id;
}
public void
setId(int id) {
this.id
= id; }
public
String getName() {
return name;
}
public void
setName(String name) {
this.name
= name;
}
public
String getdept() {
return dept;
}
public void
setdept(String dept) {
this.dept
= dept;
} } package simplifiedjava.crackedInterview;
import
java.io.File;
import
java.io.FileInputStream;
import
java.io.FileOutputStream;
import
java.io.IOException;
import
java.io.ObjectInputStream; import java.io.ObjectOutputStream; public class SerializationDemo { public static void main(String[] args) throws IOException, ClassNotFoundException { Student s1 = new Student(100,"Yogesh","IT");
File file1
= new
File("stud1.ser");
FileOutputStream fos
= new
FileOutputStream(file1);
ObjectOutputStream oos
= new
ObjectOutputStream(fos);
oos.writeObject(s1);
File file2
= new
File("stud1.ser");
FileInputStream fis
= new
FileInputStream(file2);
ObjectInputStream ois
= new
ObjectInputStream(fis);
Student s2
= (Student) ois.readObject();
System.out.println("Student ID :"
+ s2.getId());
System.out.println("Student Name :"+ s2.getName());
System.out.println("Student Dept :"+ s2.getdept());
}
}
Output:
Student ID :100
Student Name :Yogesh
Student Dept :IT |
Can you serialize primitive data types?
- Yes, All primitives data types are part of Serialization.
Can we declare a class inside a method?
- Yes, we can declare the class inside a method which is called method local
inner class.
What are Legacy Classes?
- A legacy class means old classes.
- Earlier versions of Java didn’t include the Collection framework.
- From java version 1.2 started using these Legacy classes. These classes
were reengineered to support the collection interface. These classes are
known as Legacy classes.
- Following are the Legacy classes.
1. Vector.
2. Stack.
3. Dictionary.
4. HashTable.
5. Properties.
Is Collection object by default thread-safe?
- All collections are not thread-safe by default.
- Following collection objects are thread-safe rest are not
thread-safe.
1. Vector.
2. HashTable.
3. ConcurrentHashMap.
4. CopyOnWriteArryList.
5. CopyOnWriteArraySet.
What is the initial capacity of Vector?
- Vectors initial capacity is 10.
Why we need to override hashcode() and equals() in HashMap
implementation?
- If the two objects are equals by the .equals() method then their hashcode()
must be equal. Two equivalent objects have the same hashcode.
- If(obj1.equals(obj2)) is true then obj1.hashCode() == obj2.hashCode()
always true.
- If the two objects are not equal by the .equals() method then the hashcode
of the two objects may or may not be equal.
- If(!obj1.equals(obj2)) is true then obj1.hashCode() == obj2.hashCode() may
be true or may be false.
- The objects will be stored in the same bucket which has the same
hashcode.
- If one bucket has multiple objects then internally it is stored in the form
of LinkedList.
- If you override the hashcode() method then your object will be placed in
the correct bucket and if you override the equals() method then hashmap will
check all the keys and if the key exists then it will replace the new value
and if the key is not present then that object will be placed next to the
current object in the same bucket.
- If you don’t override the hashcode() method then your object may place in
the wrong bucket.
- If you don’t override the equals() method then your object will not place at the correct location.
What is the difference between HashMap and LinkedHashMap?
|
HashMap |
LinkedHashMap |
1. |
The Data Structure of HashMap is HashTable. |
The Data Structure of LinkedHashMap is based on LinkedList +
HashTable. |
2. |
HashMap doesn’t preserve the Insertion order. |
LinkedHashMap preserves the insertion order. |
3. |
Introduced in Java 1.2 |
Introduced in Java1.4 |
- 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
Questions covered in upcoming post:
1. What is the difference between Local variable and Instance variable.
2. Can you declare constructor inside interface.
3. Can we declare a class as Abstract without having single abstract method?
4. What is the purpose of final keyword where it can be applicable.
5. What is boxing and unboxing.
6. How to create immutable class.
7. Which JVM Parameter is used to control stack size of thread.
8. What is the use of ExecutorService interface.
9. What is the role of Future interface in Multithreading.
10.What is the fill ratio in collection framework and what is the default fill ratio.
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.
|
1 Comments
Excellent Compilation for java interview.
ReplyDelete