Java Basics Interview Questions
Q1. What is the Difference Between JDK, JRE, and JVM?
 |
| What is the Difference Between JDK, JRE, and JVM |
Q2. What is Synchronization?
 |
| What is Synchronization |
Q3. What is the difference between processes and threads?
 |
| What is the difference between processes and threads |
Q4. What are Wrapper classes?
 |
| What are Wrapper classes |
Q5. What are Boxing, Unboxing, and Autoboxing, Autounboxing in Java?
 |
| What is Boxing and Unboxing |
Q6. What is the use of final, finally, and finalize in Java?
 |
| final_finally_finalize keyword in java |
Q7. Difference between StringBuffer and StringBuilder?
 |
| Difference between StringBuffer and StringBuilder |
Q8. Difference between Stack and Heap Memory?
 |
| Difference between Stack and Heap Memory |
Q9. Difference between overloading and overriding?
 |
| Difference between overloading and overriding |
Q10. Difference between equals() and == operator ?
 |
| Difference between equals() and == operator |
Q11. Difference between Abstract classes and Interfaces?
 |
| Difference between Abstract classes and Interfaces |
Q12. can we override a private or static method?
 |
| can we override a private or static method |
Q13. What is multiple inheritance and does java support it?
 |
| What is multiple inheritance and does java support it |
Q14. What is Polymorphism?
 |
| What is Polymorphism |
Q15. What is runtime polymorphism?
 |
| What is runtime polymorphism |
Q16. Difference between transient and volatile keywords? **
Answer: These two keywords are used for different requirements. The transient keyword is used to mark a field for not serialized. Suppose a class implements Serializable marker interface then its fields and methods also will be serialized. Now we have a requirement not to serialize some filed(s) then we will mark that filed as transient.
On the other hand volatile keyword is used to mark the JVM and thread to read its value from primary memory and not utilize cached value present in the thread stack. It is used in concurrent programming in java.
Let's quickly go through some important differences between these two keywords:
|
Transient
|
Volatile
|
|
The Transient marked variable prevents it
from being serialized.
|
The Volatile marked variable follows happen-before relationship on
visibility in multithreaded java program which reduces the risk of memory
consistency errors.
|
|
It gives control and flexibility over to
exclude some object methods from serialization process.
|
It prevents
JVM from doing re-ordering which could compromise synchronization.
|
|
During deserialization they are initialized
with a default value.
|
They are not initialized with a default value.
|
|
It cannot be used with the static keyword
as static variable do not belong to individual instance. During
serialization, only object’s current state is concerned.
|
It can be
used with the static keyword.
|
|
It cannot be used with the final keyword.
Although JVM doesn’t complain about it but during deserialization one will
face problem of reinitializing a variable.
|
It can be used with the final keyword.
|
Q17. Why String is immutable or final in java?
In object-oriented programming, the immutable string or objects cannot be modified once it is created. But we can only change the reference to the object. We restrict to changing the object itself. The String is immutable in Java because of the security, synchronization, and concurrency, caching, and class loading. The reason for making string final is to destroy the mutability and to not allow others to extend it.
The String objects are cached in the String pool, and it makes the String immutable. The cached String literals are accessed by multiple clients. So, there is always a risk, where an action performed by one client affects all other clients. For example, if one client performs an action and changes the string value from "Pressure" to "PRESSURE", all remaining clients will also read that value. For the performance reason, caching of String objects was important, so to remove that risk, we have to make the String Immutable.
Q18. How HashMap works internally?
In
this article, I have explained very elaborately the internal working of hashmap in java.
Q19. Explain the internal Architecture of JVM?
This is one of the basic and important interview questions. Basically, it load, execute the java program and takes care of memory management. Please read
this article for a detailed explanation of JVM architecture.
Q20. What are the oops concepts in JAVA?
Java is an object-oriented language. The main object-oriented concepts are like,
- object: Any entity that has a state and behavior is known as an object. For example, a chair, pen, table, keyboard, bike, etc. It can be physical or logical.An Object can be defined as an instance of a class. An object contains an address and takes up some space in memory. Objects can communicate without knowing the details of each other's data or code. The only necessary thing is the type of message accepted and the type of response returned by the objects.
- class: A class can also be defined as a blueprint from which you can create an individual object. Class doesn't consume any space.
- inheritance: When one object acquires the properties and behaviors of a parent object, it is known as inheritance. It provides code reusability. It is used to achieve runtime polymorphism.
- polymorphism: If one task is performed in different ways, it is known as polymorphism.In Java, we use method overloading and method overriding to achieve polymorphism.
- abstraction: Hiding internal details and showing functionality is known as abstraction. For example phone call, we don't know the internal processing.
- encapsulation: Binding (or wrapping) code and data together into a single unit are known as encapsulation. A java class is an example of encapsulation. Java bean is the fully encapsulated class because all the data members are private here.
Q21: What is association, composition, and aggregation?
These three terms mainly tell about the relationships between two objects.
Association:
Association represents the relationship between the objects. Here, one object can be associated with one object or many objects. There can be four types of association between the objects:
- One to One
- One to Many
- Many to One, and
- Many to Many
Let's understand the relationship with real-time examples. For example, one country can have one prime minister (one to one), and a prime minister can have many ministers (one to many). Also, many MP's can have one prime minister (many to one), and many ministers can have many departments (many to many). Association can be undirectional or bidirectional.
Aggregation:
Aggregation is a way to achieve Association. Aggregation represents the relationship where one object contains other objects as a part of its state. It represents the weak relationship between objects. It is also termed a has-a relationship in Java. Like, inheritance represents the is-a relationship. It is another way to reuse objects.
Composition:
The composition is also a way to achieve Association. The composition represents the relationship where one object contains other objects as a part of its state. There is a strong relationship between the containing object and the dependent object. It is the state where containing objects do not have an independent existence. If you delete the parent object, all the child objects will be deleted automatically.
Q22. What is try with resources in java?
Support for try-with-resources — introduced in Java 7 — allows us to declare resources to be used in a try block with the assurance that the resources will be closed after the execution of that block.
The resources declared the need to implement the AutoCloseable interface.
Using try-with-resources
Simply put, to be auto-closed, a resource has to be both declared and initialized inside the try:
try (PrintWriter writer = new PrintWriter(new File("test.txt"))) {
writer.println("Hello World");
}
Q23. Difference between HashMap, HashSet, and HashTable?
Q24: ClassNotFoundException,IOException,SQLException,FileNotFoundException are what type of exceptions?
These are all checked exceptions and checked exception is a subclass of the Exception class. Checked exceptions are checked at compile-time only.
Q25: Throw vs Throws keyword in java?
Point of usage
throw keyword is used inside a function. It is used when it is required to throw an Exception logically.
throws keyword is in the function signature. It is used when the function has some statements that can lead to some exceptions.
Number of exceptions thrown
throw keyword is used to throw an exception explicitly. It can throw only one exception at a time.
throws keyword can be used to declare multiple exceptions, separated by comma. Whichever exception occurs, if matched with the declared ones, is thrown automatically then.
Syntax
Syntax of throw keyword includes the instance of the Exception to be thrown.
Syntax of throws keyword includes the class names of the Exceptions to be thrown.
Propagation of Exceptions
throw keyword cannot propagate checked exceptions. It is only used to propagate the unchecked Exceptions that are not checked using throws keyword.
throws keyword is used to propagate the checked Exceptions only.
Q26: