Java Interview Questions and Answers covered in this post:
What is a Class?
-
Class is a blueprint or template, from object can be created.
- E.g. I have a class with the name Person. I have created '10' objects of
the Person class. Each object has its copy of each member variable.
package
simplifiedjava.crackedInterview;
public
class
Person {
private
int
age;
private
int
height;
private
int
weight;
private
String name;
private
String education;
// Getter and setter methods.
}
|
What is an Object?
- Generally, an Object is a real-time entity. Objects are the behaviour of the class.
- Technically an Object is the combination of data and functions.
What is the role of finalize() method?
- The role of finalize() method is to perform clean up activity.
- Garbage collector calls finalize() method just before destroying an
object. finalize() method releases the resources acquired by the objects
which don’t have any references.
- At last, all objects which don’t have references will be deleted from
memory.
Can you explain why ClassCastException occurred and how to handle
it?
- When you are trying to cast incompatible objects it triggers
ClassClassException.
- You can say ClassCastException occurs when you are improperly converting
a class from one type to another type.
- When you are trying to convert a Human object into an Animal object which
is incompatible.
- You can use instanceof operator to avoid ClassCastException.
- Please refer to below example to handle ClassCastException.
package simplifiedjava.crackedInterview;
import
java.util.ArrayList; import java.util.List; public class ClassCastExceptionHandleDemo {
public static void
main(String[] args) {
List objList
= new
ArrayList();
objList.add(new
Integer(10));
objList.add(new
Integer(10));
objList.add(new
Double(10.00)); objList.add(new String("abc"));
for(Object obj: objList) {
if(obj instanceof
Integer) {
System.out.println("Integer Type");
}else if(obj instanceof
Double) {
System.out.println("Double Type");
}else if(obj instanceof
String) {
System.out.println("String Type");
}
}
}
}
Output:
Integer Type
Integer Type
Double Type
String Type |
What is marker interface? Can you tell me the examples of marker
interface?
-
Empty interfaces are called marker interfaces.
- Marker interfaces don’t have any method, variable or constant.
- Marker interfaces are used to inform the JVM that a class implementing
this interface will have some special behaviour.
- There are some marker interfaces in java which are as
follows.
1. Serializable Interface.
2. Clonable Interface.
3. Remote Interface.
What is LinkedList?
- Basically, LinkedList is a type of data structure that can be used to
save the data and address of the next node.
- LinkedList is an implementation of the List interface.
- Linkedlist has three types.
1. Singly LinkedList.
2. Doubly LinkedList.
3. Ciruclar LinkedList.
- Please refer to the below example.
package simplifiedjava.crackedInterview; import java.util.LinkedList; class SinglyLinkedList { public static void main(String[] args) {
LinkedList<Integer> linkedList
= new
LinkedList<Integer>();
linkedList.add(50);
linkedList.add(100);
linkedList.add(150);
linkedList.add(200); linkedList.add(250);
for(Integer i
: linkedList) {
System.out.println(i);
}
}
} |
- Graphically representation of linked list.
What is the difference between ArrayList and LinkedList?
|
ArrayList |
LinkedList |
1. |
ArrayList is an index base collection.
|
LinkedList is a node base collection. |
2. |
ArrayList implements RandomAccess Interface.
|
LinkedList doesn’t implement the RandomAccess interface. |
3. |
ArrayList elements are stored on consecutive memory.
|
LinkedList elements are not stored on consecutive memory. |
4. |
There is no singly or doubly concept for ArrayList. Each
ArrayList has only one index.
|
LinkedList can be Singly LinkedList or Doubly LinkedList. |
5. |
ArrayList is the best option if you want to perform searching,
sorting and retrieval operations.
|
LinkedList is the best option if you want to perform insertion,
deletion operations in between of LinkedList. |
What is ConcurrentModificationException?
-
When one thread is performing a read operation other threads are not
allowed to perform any operation on the same object still you are
attempting the same then it will throw
ConcurrentModificationException.
- Please refer to the below example.
package simplifiedjava.crackedInterview;
import
java.util.HashMap;
import
java.util.Iterator; import java.util.Map; public class ConcurrentModificationExceptionDemo {
public static void
main(String[] args) {
HashMap<Integer,String> m
= new
HashMap<Integer,String>();
m.put(101,"Shweta"); m.put(102,"Shruti");
Iterator itr
= m.entrySet().iterator();
while(itr.hasNext()) {
m.put(103, "Anjali");
Map.Entry<Integer, String> pair
= (Map.Entry<Integer, String>)itr.next();
System.out.println("Key = "+ pair.getKey()+ "\t Value = "+ pair.getValue());
}
}
}
Output:
Exception in thread "main" java.util.ConcurrentModificationException
at
java.util.HashMap$HashIterator.nextNode(Unknown Source)
at
java.util.HashMap$EntryIterator.next(Unknown Source)
at
java.util.HashMap$EntryIterator.next(Unknown Source)
at
simplifiedjava.crackedInterview.ConcurrentModificationExceptionDemo.main(ConcurrentModificationExceptionDemo.java:17)
|
What is the difference between Web Server and Application Server?
|
Web Server |
Application Server |
1. |
EJB and JMS cannot be deployed on a Web Server.
|
EJB and JMS can be deployed on the Application Server. |
2. |
Web server is one component of Application Server.
|
The application server is a parent of the web server. |
3. |
An example of a Web Server is Apache Tomcat.
|
An example of an Application Server is JBoss. |
What is functional Interface?
- An Interface with only one abstract method is called a functional
interface.
- @FunctionalInterface
annotation can be used to indicate.
- If an interface has more than one abstract method then it
is not a functional interface it is a regular interface.
- Functional interface can have multiple default and static
methods but only one abstract method.
- Lambda expression can work with a functional interface
only. Lambda expression cannot work with a regular interface that has
multiple abstract methods.
- Please refer to the below example for the functional
interface.
package simplifiedjava.crackedInterview.java8;
@FunctionalInterface
public interface
FunctionalInterfaceDemo {
public void
add();
} |
- 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 the class loader and what are the types of class loaders?
2. How you have implemented Encapsulation in Project?
3. Can we override the overloaded method.
4. What is the default constructor and what is the difference between default constructor and no-arg constructor and parameterized Constructor.
5. What is abstract class. Can we instantiate abstract class.
6. What is covarient return type.
7. What is intern() method.
8. What is multicatch block. In which java version it has been introduced.
9. How will you convert collectoion object to thread safe collecton object.
10.What is Metaspace introduced in java 8.
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.
|
7 Comments
Very helpful thanks yogesh
ReplyDeleteGood compilation of questions, keep it up Yogesh!🙂 Eager to read the full book.
ReplyDeleteThank you Anurag!!!
DeleteNice set of questions Yogesh, waiting for the next set!!
ReplyDeleteGood questions
ReplyDeleteThank you Yogesh !!
ReplyDeleteThank you so much. will keep updating same type of questions for another companies.
ReplyDelete