Java Interview Questions and Answers covered in this post:
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.
In your program, you have a static block, non-static block, constructor
and main(). What will be the sequence of execution?
-
The sequence of execution is as follows.
1.
Static Block : will be executed at the time of class loading
into memory.
2. Non-Static Block : Will be executed at the time of instantiation. But make
a note it depends if you are instantiating the class before printing any
statement in main. If you instantiate the class after printing the
statement.
3.
Constructor : Will be executed at the time of object
initializing.
4.
Main : Will
execute last if you haven’t instantiated the class.
5.
We can see more scenarios.
Scenarios 1:
package simplifiedjava.crackedInterview; public class BlockExecutionSequenceDemo {
static
{
System.out.println("Static Block Excecution");
}
{
System.out.println("Non Static Block Excecution"); }
public
BlockExecutionSequenceDemo() {
System.out.println("Constructor Excecution"); }
public
static
void
main(String[] args) {
System.out.println("Main Method Execution");
}
}
Output:
Static Block Excecution Main Method Execution
Scenarios 2: package simplifiedjava.crackedInterview; public class BlockExecutionSequenceDemo {
static
{
System.out.println("Static Block Excecution");
}
{
System.out.println("Non Static Block Excecution"); }
public
BlockExecutionSequenceDemo() {
System.out.println("Constructor Excecution"); }
public
static
void
main(String[] args) {
BlockExecutionSequenceDemo demo
= new
BlockExecutionSequenceDemo();
System.out.println("Main Method Execution");
}
}
Output :
Static Block Excecution
Non Static Block Excecution
Constructor Excecution Main Method Execution
Scenarios 3: package simplifiedjava.crackedInterview; public class BlockExecutionSequenceDemo {
static
{
System.out.println("Static Block Excecution");
}
{
System.out.println("Non Static Block Excecution"); }
public
BlockExecutionSequenceDemo() {
System.out.println("Constructor Excecution"); }
public
static
void
main(String[] args) {
System.out.println("Main Method Execution");
BlockExecutionSequenceDemo demo
= new
BlockExecutionSequenceDemo();
} }
Output:
Static Block Excecution
Main Method Execution
Non Static Block Excecution
Constructor Excecution |
What is the purpose to make a constructor private?
- Private constructor ensures that only one object can be created at a
time.
- Cannot instantiate the class from outside the class.
- Generally for singleton design pattern uses a private
constructor.
What is instanceOf in java? Where it can be used?
- instanceOf is an operator in java.
- instanceOf can be used to check the type of object.
- syntax for instanceOf operator is, (JavaInterview instanceOf
String);
package simplifiedjava.crackedInterview;
import
java.util.ArrayList; import java.util.List; public class InstanceOfDemo { public static void main(String[] args) {
List list = new
ArrayList();
list.add(new
String("Java"));
list.add(new
Integer(100)); list.add(new ITEmployee());
for(Object obj : list) {
if(obj instanceof
String) {
System.out.println((String)obj);
}else if(obj instanceof
Integer) {
System.out.println((Integer)obj);
}else if(obj instanceof
ITEmployee) {
System.out.println((ITEmployee)obj);
}
}
} } class ITEmployee{
} |
What is the difference between equals() and ==?
- Equals() is the method and can be used for content
comparison.
- == is an operator and can be used for reference comparison.
What is Comparable and Comparator? What is the difference between
Comparable and Comparator?
- Comparable and Comparator are interfaces that can be used for
sorting purposes.
- Comparable interface can be used for natural sorting
order.
- Comparator interface can be used to customize sorting
orders.
- Comparable has compareTo() method.
- Comparator has compare() method.
What is the difference between checked exceptions and unchecked
exceptions?
|
Checked Exception |
Unchecked Exception |
1. |
Checked Exceptions can occur at Compile time. |
Unchecked Exceptions can occur at run time. |
2. |
If you don’t handle checked exceptions then your code will not
compile. You will get a compile-time error. |
If you don’t handle unchecked exceptions you will get an exception
at run time but your program will be compiled. |
3. |
Checked Exceptions extends Throwable and Error but except
RuntimeException. |
Unchecked Exceptions extends RuntimeException. |
4. |
Examples :
IOException, SQLException, FileNotFoundEx ception |
Examples:
NullPointerException, ArithmeticException,
ArrayIndexOutOfBoundsException |
Can you explain why NullPointerException occurred and how to handle it?
- Calling a method on null reference or trying to access a field of a null
reference will trigger a NullPointerException.
- You can fix it either by making sure you are invoking the method non-null
object.
- Make sure before invoking the method or modifying the field object is
non-null.
- You can put a null check before invoking or performing any operation.
- You may use a try-catch block to handle NullPointerException.
- Please refer to the below scenario.
a. Trying to find the length of an array when it is null.
b. Calling the instance method on the null object.
c. Trying to modify the field of the null object.
d. Trying to count a length of String object when it is
null.
Demo 1: Put null check before invoking or performing any operation. package simplifiedjava.crackedInterview; public class NullPointerExceptionDemo { String str;
public static void
main(String[] args) {
new
NullPointerExceptionDemo().calculateLength(); }
public void
calculateLength() {
if(str != null) {
System.out.println(str.length());
}else {
System.out.println(" Variable is "+ str);
}
}
} Output: Variable is null Demo 2: Put code inside try catch block. package simplifiedjava.crackedInterview; public class NullPointerExceptionDemo { String str;
public static void
main(String[] args) {
new
NullPointerExceptionDemo().calculateLength(); }
public void
calculateLength() {
try {
str.length();
}catch(NullPointerException e) {
System.out.println("Null Pointer Exception Handled");
}
}
}
Output: Null Pointer Exception Handled |
What is a Thread and what is the process?
- Thread: Thread is a single independent path of execution of the
program.
- Process: Process is a program in execution.
What are the advantages and disadvantages of Serialization?
- Advantages of Serialization.
1. Convert the object onto the byte stream and transfer it through the
network.
2. A converted byte stream can save into a file or database.
3. Third-party services do not require implementing serialization.
4. Serialization allows java to perform Encryption, decryption, Authentication
etc.
- Disadvantages of Serialization.
1. Serialization is default serialization so unnecessary we have to implement
full serialization.
2. Serialization is inefficient when it comes to memory
utilization.
3. Sometimes the byte stream does not convert into objects completely which leads to error.
- 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 is platform independent? Why java is platform independent?
2. What is static import.
3. where the static variables are stored in java.
4. What is shadowing?
5. What is dimond problem in Java. Why java doesn't support multiple inheritance.
6. Can we skip finally block to execute. if yes then how.
7. What is unreachable catch block error.
8. What are the characteristics of Hashset.
9. What is the default size and load factor of HashSet?
10.Can you override default method.
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
Tricky question sets which is asking by interviewer.
ReplyDelete