Java Interview Questions and Answers covered in this post:
What is JVM? What is the difference between JDK, JVM and JRE?
- JVM stands for Java Virtual Machine.
- JVM provides a runtime environment in which byte code can be executed.
|
JDK |
JVM |
JRE |
1. |
JDK Stands for Java Development Kit. |
JVM Stands for Java Virtual Machine. |
JRE Stands for Java Runtime Environment. |
2. |
JDK Physically exist in System |
JVM exist virtually but not physically. |
JRE exists physically. |
3. |
JDK = JRE + JVM |
JVM is a run time environment in which bytecode can execute. |
JRE is an Implementation of JVM. |
Will the code compile If the parent class method is not
throwing any exception but the child class method is throwing a checked
exception?
- No Class won’t be compiled. We will get a compile-time
error.
- We can see the below example.
package simplifiedjava.crackedInterview; public class Parent {
public void
check(String relation) {
System.out.println("Parent Class check() method.");
} } package simplifiedjava.crackedInterview; import java.io.FileNotFoundException; public class Child extends Parent{
public void
check(String relation)throws
FileNotFoundException {
System.out.println("Child Class check() method.");
}
}
Output : Compile time error:
Exception FileNotFoundException is not compatible with throws clause
in Parent.check(String) |
What is a constructor? What are the types of constructors?
- Constructor is a function that is used to initialize the
object.
- Constructor doesn’t have a return type.
- Constructor name must be the same as a class name.
- We can have multiple constructors in one class.
- We can declare the constructor as private, public and
protected.
- There are three types of constructors.
1. Default Constructor: If you don’t provide any
constructor compiler will provide you with, default constructor.
2. No-arg constructor: If you have declared a constructor
without a parameter. That constructor is called the no-arg constructor.
3. Parameterized Constructor: If you declared a
constructor with a parameter then it is called the parameterized
constructor.
Can we declare static method and static variables in abstract
class?
- Yes. You can declare static method and static variables inside an abstract class.
What is object cloning?
- Object cloning means creating a duplicate copy of an
object.
- clone() can be used to creating the object clone.
What is the difference between StringBuilder and StringBuffer?
|
StringBuilder |
StringBuffer |
1. |
StringBuilder is not a thread-safe. |
StringBuffer is a thread-safe. |
2. |
The performance of StringBuilder is good as compare to
StringBuffer. |
The performance of StringBuffer is lower as compare to
StringBuilder cause StringBuffer is thread-safe. |
3. |
StringBuilder sb = new StringBuilder(“ac”); |
StringBuffer sb = StringBuffer(“ac”); |
Can we declare an empty catch block?
- Yes you can declare an empty catch block. But it's mandatory to declare any
exception class in the catch block parameter.
- Please refer to the below example.
package simplifiedjava.crackedInterview; public class CustomException extends Exception {
public static void
main(String[] args) { try { }catch(Exception e) {
}
}
}
It is mandatory to declare the Exception class name in the catch
method signature. But inside the catch block, you can keep it blank. If you missed to declared an Exception in the catch method signature then you will get a compile-time error. |
What is Thread-Local class? What is the purpose to have this
class?
- ThreadLocal in Java had been introduced on JDK 1.2 but was later generified in JDK 1.5 to introduce type safety on ThreadLocal variable.
- The Java ThreadLocal class enables you to create variables that can only be read and written by the same thread.
- If two threads are executing the same code, and the code has a
reference to the same ThreadLocal
variable, the two threads cannot see each other's ThreadLocal
variables.
- We can create thread-safe classes that don’t share states between threads
by making their fields thread-local.
- Thread-Local is most commonly used to implement per-request global
variables in app servers or servlet containers where each user or client
request is handled by a separate thread.
- For example, Spring, Spring Security and JSF each have the concept of a
"Context" through which functionality of the framework is accessed. And in
each case, that context is implemented both via dependency injection (the
clean way) and as a Thread-Local in cases where DI can't or won't be
used.
- Please refer the below example.
package simplifiedjava.crackedInterview;
import
java.util.Arrays; import java.util.List; public class ThreadLocalDemo1 extends Thread { private final List<Integer> numList = Arrays.asList(10,20,30,40,50);
@Override
public void
run() {
for(int n : numList) {
System.out.println(n);
}
} } package simplifiedjava.crackedInterview;
import
java.util.Arrays; import java.util.List; public class ThreadLocalDemo2 extends Thread{ private final List<String> wordList = Arrays.asList("Hi","Hello","GM","GA","GN");
@Override
public void
run() {
for(String str : wordList) {
System.out.println(str);
}
}
} |
What is the difference between Regular Inner Class and Anonymous Inner
Class?
|
Regular Inner Class |
Anonymous Inner Class |
1. |
Regular inner class can implement any number of interfaces at a
time. |
Anonymous inner class can implement only one interface at a
time. |
2. |
Regular inner class can extend only one class and implements
multiple interfaces. |
Anonymous inner class either can extend a class or implement only
one interface. |
3. |
Regular inner class can have multiple constructors. |
Anonymous inner class cannot have any constructor. |
4. |
If the requirement is not temporary use then go for regular inner
class. |
If the requirement is temporary use then go for anonymous inner
class. |
Why Collection interface doesn't extend Clonable and Serializable
interface?
- Collection
interface is a root interface of the collection hierarchy.
- A lot of concrete implementation classes implements the Collection
interface.
- If Collection implements Clonable and Serializable interface then all
concrete implementer classes mandatorily implement these two interfaces.
- If there is no requirement for these then every class has to implement that
functionality unnecessarily.
- It may impact the performance or work redundancy may happen.
- To avoid all these problems Collections doesn’t implement Clonable and Serializable 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 do you mean by thread priority.
2. What is static synchronization.
3. What is Serialization? Explain the need of serialization.
4. Can you serialize primitive data types.
5. Can we declare a class inside method.
6. What are Legacy Classes.
7. Is Collection object bydefault threadsafe.
8. What is the initial capacity of Vector.
9. Why we need to override hashcode() and equals() in HashMap implementation.
10.What is the difference between HashMap and LinkedHashMap.
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