Java Interview Questions and Answers covered in this post:
Which is the top most Parent class in Java? What the methods of Object
Class?
- Java.lang.Object is the parent class of java hierarchy.
- Following are the defined methods in Object class.
1. public final Class getClass() : returns the Class
class object.
2. public String toString(): Convert object to
String.
3. public boolean equals(): Comparing this object to
another object.
4. public int hashCode(): return the hash code of
object.
5. protected Object clone(): return the duplicate
object.
6. public final void wait(): put the thread into waiting
area.
7. public final void notify(): notify the waiting or
sleeping thread.
8. public final void notifyAll(): notify all the waiting and sleeping thread.
Can you explain the types of Inheritance supports in java?
- Java Supports 3 types of Inheritance.
1.
Single Inheritance:
In Single Inheritance, only one class can extend at a time.
In single inheritance, there is only one child class and parent class.
Please refer to the below example.
package simplifiedjava.crackedInterview; public class Human {
} public class Men extends Human{
} |
2.
Multilevel Inheritance:
When one class extends another class and that another class extends a
subsequent class this type of inheritance is called Multilevel
Inheritance.
Every child class becomes a parent class of its subsequent child class.
Please refer to an example below.
package simplifiedjava.crackedInterview;
public class
Human {
} public class Men extends Human{ } public class Boy extends Men{
} |
3.
Hierarchical Inheritance:
When two or more classes extends one class is called Hierarchical
Inheritance.
Please refer to the below example.
package simplifiedjava.crackedInterview; public class Human {
} public class Men extends Human{
} public class Women extends Human{
} |
What is the difference between static block and instance block?
|
Static Block |
Non Static Block |
1. |
The Static block declared with the static keyword. |
Non-Static block declared with no keyword. |
2. |
The Static block gets executed at the time of class loading. |
Non-Static block gets executed at the time of object
instantiation. |
3. |
The Static block executes before Non-static block. |
Non-static block executes after the static block. |
What is a marker interface? Can you tell me the example of a marker
interface?
- An empty interface is called a marker interface.
- Marker interface doesn’t have any method or variable.
- List of marker interfaces.
1. Serializable interface.
2. Cloneable interface.
3. Remote Interface.
What is ENUM? What is the purpose of ENUM?
- Enum stands for Enumeration.
- Enum is a data type that is a set of constants.
- Enum can be declared inside or outside the class.
- Please refer to the below example for enum
implementation.
package simplifiedjava.crackedInterview; public class EnumDemo { public enum months {JANURAY,FEBRUARY,MAR,APRIL,MAY,JUNE,JULY,AUGUST,SEPTEBER,OCTOBER,NOVEMBER,DECEMER};
public static void
main(String[] args) {
for(MONTHS month
: months.values()) {
System.out.print(" "+ month);
}
}
}
Output: JANURAY FEBRUARY MAR APRIL MAY JUNE JULY AUGUST SEPTEBER OCTOBER
NOVEMBER DECEMER |
What is an immutable class?
- Immutable class means the value or content cannot be changed once an object
is created.
- Best example of immutable in java is the String class. The String object
cannot be changed once it is created.
- All wrapper classes are immutable classes. Wrapper classes are Integer,
Float, Double, Boolean, Byte, Short etc.
What is the role of the printStackTrace() method? This method belongs
to which class?
- printStackTrace() method is used to diagnosing the exceptions.
- This method points out exactly where the exception occurred. It will print
the function name and line number as well.
- printStackTrace() method belongs to the Throwable class which is the super
parent class of the Exception hierarchy.
- For reference to the printStackTrace() method you can refer to the previous example.
Can we have multiple catch blocks?
- Yes. You can have multiple catch block but there is some condition to
declared multiple catch blocks.
What is Vector? What are the characteristics of Vector?
- A vector can be defined as a dynamic array that can grow or shrink on its
own.
- Vector will grow when more elements are added to it and will shrink when
elements are removed from it.
- But similar to arrays, vector elements can be accessed using integer
indices.
- Default size of vector is 10.
- Vector is a legacy class.
- Please refer to the below example.
package simplifiedjava.crackedInterview;
import
java.util.Iterator; import java.util.Vector; public class VectorDemo {
public static void
main(String[] args) {
Vector<Integer> v = new
Vector<Integer>();
v.add(10);
v.addElement(20);
v.add(30); v.addElement(40);
// Using foreach.
for(Integer i : v) {
System.out.println(i);
}
// Using iterator
Iterator<Integer> itr = v.iterator();
while(itr.hasNext()) {
Integer i = itr.next();
System.out.println("Value of Vector "+ i);
}
}
} |
What is Priority Queue?
- If we want to represent a group of individual objects prior to processing
according to some priority then it is called Priority Queue.
- Priority can be natural sorting order or customized sorting order defined
by the comparator.
- Insertion order is not preserved. It is based on some priority.
- Duplicate objects are not allowed.
- Null is not allowed.
- If we are depending on natural sorting order then objects must implement a
comparable interface or it should be homogeneous. Otherwise, we will get
ClassCastException.
- If we are defining our own Comparator then there is no need to have
homogeneous object.
- Please refer to the below two examples.
package simplifiedjava.crackedInterview;
import
java.util.Comparator; import java.util.PriorityQueue; public class PriorityQueueDemo {
public static void
main(String[] args) {
PriorityQueue<String> q = new
PriorityQueue<>(new
DecendingQueueComparator());
q.offer("Shweta");
q.offer("Shruti"); q.offer("Arpita");
while(!q.isEmpty()) {
System.out.println(q.poll().toString());
}
} } class DecendingQueueComparator implements Comparator<String>{
@Override
public int
compare(String s1, String s2) {
return s2.compareTo(s1);
}
}
Output:
Shweta
Shruti Arpita // Ascending Order. package simplifiedjava.crackedInterview;
import
java.util.Comparator; import java.util.PriorityQueue; public class PriorityQueueDemo {
public static void
main(String[] args) {
PriorityQueue<String> q = new
PriorityQueue<>(new
AscendingQueueComparator());
q.offer("Shweta");
q.offer("Shruti"); q.offer("Arpita");
while(!q.isEmpty()) {
System.out.println(q.poll().toString());
}
} } class AscendingQueueComparator implements Comparator<String>{
@Override
public int
compare(String s1, String s2) {
return s1.compareTo(s2);
}
}
|
- 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 role of break and continue keywords in java.
2. Is Java Pass by value or Pass by Reference. How will you prove java is call by value?
3. Can you write a code to customize Employee Sorting order by Salary or Designation.
4. List out some inbuilt methods of String class.
5. What is the difference between ClassNotFoundException and NoClassDefFoundError.
6. What is the difference between final, finally and finalize.
7. What is daemon thread.
8. What is the difference between runnable and callable.
9. Which scenario are the best example to implement of LinkedList and ArrayList.
10.What is SortedMap? How objects will be stored in SortedMap bydefault.
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