Java Interview Questions and Answers covered in this post:
What is the class loader and what are the types of class loaders?
-
ClassLoader loads the classes dynamically into memory whenever required by
Java Runtime Environment.
-
ClassLoader is an abstract class in Java Runtime
Environment.
-
There are three types of class loaders available in java.
a.
BootStrap Class Loader :
This is a parent loader of all existing loaders. Bootstrap loaders load all JDK files from jre/lib/rt.jar.
b.
Extension Class Loader :
Extension class loader loads the extensions of java core classes from jre/lib/ext directory. Extension class loader is a child loader of Bootstrap class loader.
c.
System Class Loader :
System class loader loads the application level classes. System class
loader is a child loader of Extension class loader.
How have you implemented Encapsulation in the Project?
- You can create POJO(Plain Old Java Object) class.
- You have to declare all members with private access modifier and provide
public getters and setters method. So the original value of that variable
cannot be change cause they are private and outsiders can use it cause we
have provided public getters and setters methods.
package simplifiedjava.crackedInterview; public class EncapsulationImplementation {
private int id; private String name;
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;
}
} |
Can we override the overloaded method?
- Yes. We can override the overloaded method cause within a class overloading
is possible but for overriding, you must have a child class where you
defined the overriding method.
- For your reference, I have created one simple example.
package simplifiedjava.crackedInterview; public class OverrideOverloadedMethodDemo {
public static void
main(String[] args) {
Parent p = new
Parent();
Child c = new
Child();
c.check("Brother");
} } package simplifiedjava.crackedInterview; public class Parent {
public void
check() {
System.out.println("Hello"); }
public void
check(String relation) {
System.out.println("Parent Class check() method.");
} } package simplifiedjava.crackedInterview; public class Child extends Parent{
public void
check(String relation) {
System.out.println("Child Class check() method.");
} }
O/P : Child Class check() method.
|
What is the default constructor and what is the difference between the
default constructor and no-arg constructor and parameterized
Constructor?
- Default constructor is a type of constructor provided by the compiler when
there is no constructor declared in the class.
- Default constructor is a non-parameterized constructor.
- Default constructor doesn’t have any parameters. Access specifiers are
default.
- No-arg constructor doesn’t have any parameter. But we can declare no-arg
constructor as public, private, protected and default.
- Parameterized constructor must have at least one parameter. The
parameterized constructor can be declared as public, private, protected and
default.
What is an abstract class? Can we instantiate abstract class?
- A class declared with an abstract keyword is called an abstract class.
- If you declare a class as an abstract then we can declare the abstract
method and concrete method as well.
- If there is a single abstract method declared inside a class then it is
mandatory to declare the class as an abstract class.
- It is not mandatory to declare at least a single abstract method inside an
abstract class. In short, if you already declared class as abstract then it
is not mandatory to the declared abstract method.
What is a co-variant return type?
- Covariant return means that when one overrides a method, the return type of
the overriding method is allowed to be a subtype of the overridden method's
return type.
- In the below example, There are two classes Human and Men. Men class
extends the Human class.
- Both class has
dressingStyle()
method. This method is overriding in the Men class. Both methods doesn't
have the same return type but if you observe then the parent class method
returns the actual Human class object and the child class method returns the
Child class(Men Class) object when the return type is Men. This is called
covariant return type.
package simplifiedjava.crackedInterview; public class Human {
public
Human dressingStyle() {
return new
Human();
}
} package simplifiedjava.crackedInterview; public class Men extends Human {
public
Men dressingStyle() {
return new
Men();
}
} |
What is the use of the intern() method?
- Intern() method returns a canonical representation of String.
- In simple word, intern() method return String object from SCP(String Constant Pool).
What is a multi-catch block? In which java version it has been
introduced?
- In one catch block we can handle multiple exceptions is called a
multi-catch block.
- Pipe Operator “|” can be used to segregate multiple
exceptions.
- Multi catch block has been introduced in Java 7.
- Please refer to the below example for a multi-catch
block.
package simplifiedjava.crackedInterview; public class Aeroplane {
public void
setLocation(String locationName){
try {
if(locationName.equals(null)) {
throw new
NullPointerException();
}
}catch(NullPointerException | ArithmeticException e) {
e.printStackTrace();
}
}
} |
How will you convert collection object to thread-safe collection
object?
- We have a utility class that has some utility methods to convert
non-thread-safe collections to the thread-safe collection.
- Each collection has its utility method to convert
non-thread-safe to thread-safe.
- Following are the collections and its
utility methods.
1. Collection has Collections.synchronizedCollection().
2. Map has Collections.synchronziedMap().
3. SortedMap has Collections.synchronizedSortedMap().
4. NavigableMap has Collections.navigableMap().
5. List has Collections.synchronizedList().
6. Set has Collections.synchronizedSet().
7. NavigableSet has Collections.synchronizedNavigableSet().
8. SortedSet has Collections.synchronizedSortedSet().
What is Metaspace introduced in java 8?
- PermGen:
1. PermGen (Permanent Generation) is a special heap space separated from the
main memory.
2. The JVM keeps track of class metadata in the PermGen. Also, the JVM stores
all the static content in this.
3. Due to limited memory size, PermGen can throw OutOfMemoryError.
- Metaspace:
1. Metaspace is new memory space.
2. It has replaced the older PermGen memory space.
3. It can now handle memory allocation.
4. Metaspace grows automatically by default.
- 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 HEAP memory and Stack Memory. What is the difference between these two.
2. Why java doesn't support Multiple Inheritance?
3. Can I declare static method inside a interface.
4. Java.lang.Object class has equals() method then why there is a need to override equals() method in user defined class.
5. Can you override static method.
6. Can constructor declared exception with throws keyword.
7. What is upcasting and downcasting.
8. how many types we can handle the exception.
9. Can you explain why ClassNotFoundError occurred and how to handle it?
10.What is autoclosable resource.
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