Java Interview Questions and Answers covered in this post:
Can we create a method with the same name as the class name with return
type? Will the code compile?
- Yes, we can create the method name the same as the class name. Code will also compile. But remember this is not a good practice. We have a
constructor with the same class name.
- Please refer to the below example.
package simplifiedjava.crackedInterview; public class Parent {
public
static
void
main(String[] args) {
new
Parent().Parent(); }
public
void
Parent() {
System.out.println("Hello");
}
}
Output: Hello |
Can we ask JVM to collect the garbage or destroy the objects?
- Yes we can just recommend destroying unused objects but cannot force them
to do it.
- System.gc() method can be used to recommend the garbage collection.
What is the default size of StringBuffer and StringBuilder?
- The default size of StringBuffer and StringBuilder is 16.
- Please refer the below example of String Buffer and String Builder.
package simplifiedjava.crackedInterview; public class StringBuilderSizeCalculationDemo {
public static void
main(String[] args) {
String s = "abc";
StringBuffer sb = new
StringBuffer();
System.out.println("String Buffer Size = "+ sb.capacity());
StringBuilder sb1 = new
StringBuilder(s);
System.out.println("String Builder Size = "+ sb1.capacity());
}
} |
What is the difference between exception and error?
|
Error |
Exception |
1. |
Error is an invalid condition. |
An exception is an abnormal condition. |
2. |
An error cannot be handled. |
An exception can be handled. |
3. |
Error stops your program immediately. |
If you handle exceptions then your program may execute
smoothly. |
4. |
An error can occur only run time but not compile time. |
An exception can occur compile time and run time. |
5. |
Error belongs to java.lang.Error |
Exeception belongs to java.lang.Exception. |
6. |
e.g. AssertionError, StackOverflowError, ExceptionInInitializerError, NoClassDefFoundError, NoSuchMethodError,
|
e.g. NullPointerException,
FileNotFoundException, ArithmeticException, IndexOutOfBoundException, ArrayIndexOutOfBoundException, IllegalStateException, ClassCastException, IllegalArgumentException,
|
There are 3 Thread Thread1, Thread2 and Thread3 how would you ensure
the thread execution for Thread1, Thread2 and then Thread3?
- If a thread wants to wait until completing some other thread then we should
go for the join(); method.
- In above example, Thread1.join(Thread2) and Thread2.join(Thread3) in the above case Thread3 will execute first and then thread2 and then thread1.
- Please refer the below example for more clarification.
package simplifiedjava.crackedInterview; public class ThreadJoinMethodDemo { public static void main(String[] args) {
ThreadJoin t1 = new
ThreadJoin();
ThreadJoin t2 = new
ThreadJoin();
ThreadJoin t3 = new
ThreadJoin();
t1.setName("First Thread");
t1.start();
try { t1.join();
}catch(Exception e) {
e.printStackTrace(); }
t2.setName("Second Thread");
t2.start();
try { t2.join();
}catch(Exception e) {
e.printStackTrace(); }
t3.setName("Third Thread");
t3.start();
try {
t3.join();
}catch(Exception e) {
e.printStackTrace();
}
}
}
class ThreadJoin extends Thread{
public void
run() { for(int i=0; i<2 ; i++) {
try {
System.out.println("The current thread name is: "
+ Thread.currentThread().getName()); Thread.sleep(1000);
}catch(InterruptedException e) {
e.printStackTrace();
}
}
} } Output:
The current thread name is: First Thread
The current thread name is: First Thread
The current thread name is: Second Thread
The current thread name is: Second Thread
The current thread name is: Third Thread
The current thread name is: Third Thread |
What is the difference between preemptive scheduling and time
slicing?
|
Preemptive Scheduling |
Time Slicing |
1. |
Executes highest priority task executes until it highest priority
task comes into existence or thread enters into waiting state or
dead state. |
Task executes for a predefined slice of time and then reenters into
the pool of ready tasks. The scheduler will decide which task needs
to execute first according to priority. |
Why java.lang.Object class doesn't implement Serializable
interface?
- Serializable is marker interface, which is empty, but when a class is
marked Serializable that means its objects are Serializable.
- The reason the java.lang.Object didn’t implement Serializable is because,
what if you do not want to make certain fields as Serializable and you may
missed to add transient to that field, then will be a havoc.
Can we transfer object through network if serialization mechanism is
not available?
- Yes, we can transfer object through network if serialization mechanism is
not available.
1. JSON: We can convert the java object into JSON object. JSON object
can be transferred through the network.
2. XML: We can convert the java object into XML and then we can transfer through the network.
When you will get UnSupportedOperationException?
- UnSupportedOperationException is a runtime exception.
- When you are trying to modify the unmodifiable list then you will get UnSupportedOperationException.
- Please refer the below example to clarify.
package simplifiedjava.crackedInterview;
import
java.util.ArrayList;
import
java.util.Collections; import java.util.List; public class UnmodifiableListDemo { public static void main(String[] args) {
List<Integer> list = new
ArrayList<Integer>();
list.add(10);
list.add(20);
list.add(30);
list.add(30);
list.add(10); System.out.println("Before Unmodified "+ list); list = Collections.unmodifiableList(list);
list.remove(2);
System.out.println("After Unmodified "+ list);
} }
Output:
Before Unmodified [10, 20, 30, 30, 10]
Exception in thread "main" java.lang.UnsupportedOperationException
at
java.util.Collections$UnmodifiableList.remove(Unknown Source)
at
simplifiedjava.crackedInterview.UnmodifiableListDemo.main(UnmodifiableListDemo.java:21)
|
Explain Big-O notation with an example?
- The Big-O notation depicts the performance of an algorithm as the number of
elements in ArrayList.
- A developer can use Big-O notation to choose the collection implementation.
- It is based on performance, time and memory.
- For example, ArrayList get (index i) is a method to perform a constant-time
operation. It does not depend on the total number of elements available in
the list. Therefore, the performance in Big-O notation is O (1).
- 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