Skip to content

Commit e5133f4

Browse files
committed
Added new core java questions
1 parent 7707fba commit e5133f4

File tree

3 files changed

+127
-15
lines changed

3 files changed

+127
-15
lines changed

.github/workflows/gen-toc.yml

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
name: Generate table of contents
2+
3+
on:
4+
pull_request:
5+
push:
6+
branches:
7+
- master
8+
9+
env:
10+
BRANCH_NAME: ${{ github.head_ref || github.ref_name }}
11+
12+
jobs:
13+
gen-toc:
14+
runs-on: ubuntu-latest
15+
steps:
16+
- name: Checkout code
17+
uses: actions/checkout@v3
18+
with:
19+
ref: ${{ github.head_ref }}
20+
- name: Install dependencies
21+
run: npm install
22+
- name: Generate table of contents
23+
run: npm run gen
24+
- name: 'Commit changes if required'
25+
run: |
26+
if ! git diff --quiet README.md; then
27+
git config user.email "github-actions[bot]@users.noreply.github.com"
28+
git config user.name "GitHub Actions"
29+
git add README.md
30+
git commit -m "[auto] regenerate table of contents"
31+
git push
32+
echo "[info] Table of contents updated and committed."
33+
else
34+
echo "[info] No changes to table of contents."
35+
fi
36+
working-directory: ${{ github.workspace }}

README.md

Lines changed: 91 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ Frequently asked Java Interview questions
33

44
### Table of Contents
55

6+
<!-- TOC_START -->
67
| No. | Questions |
78
| --- | ------------------------------------------------------------------------------------------------------------------------------------------------------------- |
89
| 1 | [How does JVM works](#how-does-jvm-works) |
@@ -16,7 +17,32 @@ Frequently asked Java Interview questions
1617
| 9 | [What is the difference between abstract class and interface](#what-is-the-difference-between-abstract-class-and-interface) |
1718
| 10 | [What are marker interfaces](#what-are-marker-interfaces) |
1819

19-
1. ### How does JVM works
20+
<!-- TOC_END -->
21+
22+
<!-- QUESTIONS_START -->
23+
1. ### What are the differences between JVM, JRE and JDK?
24+
25+
The main differences between JVM, JRE and JDK are listed below,
26+
27+
**JVM:** Java Virtual Machine(JVM) is an abstract machine that provides a runtime environment for the execution of Java ByteCode. i.e, It is part of Java Runtime Environment(JRE) and responsible for converting bytecode into machine-readable code. JVM is platform dependent but it interprets the bytecode which is platform independent. As a result, Java applications to run on different platforms and operating systems.
28+
29+
**JRE:** Java Runtime Environment (JRE) is an installation package that provides Java Virtual Machine (JVM), class libraries and other components to run the Java program or application on any machine.
30+
31+
**JDK:** Java Development Kit (JDK) is a software development kit used to develop and execute Java programs. It includes tools for developing, monitoring and debugging java programs, along with JRE to execute those respective programs.
32+
33+
**[⬆ Back to Top](#table-of-contents)**
34+
35+
2. ### Why Java is platform-independent language
36+
37+
Java is platform-independent language because java programs are compiled to a bytecode that can be run on any device or OS which has a Java Virtual Machine(JVM). This is possible due to JVM's "Write Once, Run Anywhere"(WORA) capability. Due to this reason, you can write a Java program on one platform such as Windows machine and then run it on a different platform such as macOS or Linux machine without making any modifications to the code.
38+
39+
Below diagram explains the platform independence feature of Java,
40+
41+
![Screenshot](images/JavaIndependent.png)
42+
43+
**[⬆ Back to Top](#table-of-contents)**
44+
45+
3. ### How does JVM works
2046

2147
JVM(Java Virtual Machine) is an abstract runtime engine to run java applications. It is part of Java Runtime Environment(JRE) to execute the bytecode generated by java compilers. JVM brings WORA(Write Once Read Anywhere) behavior in which you can write java program on one system and expected to run on any java enabled systems without any modifications. Upon compiling .java file to .class file(contains byte code), .class file goes into severl steps described by JVM.
2248

@@ -40,15 +66,15 @@ Frequently asked Java Interview questions
4066
4. Whether .class is related to Class, Interface or Enum.
4167

4268

43-
JVM also creates an object of **Class** type(available from **java.lang** package) to represent the file in the heap memory. But it will be created only on the very first time when the class file is loaded into JVM. This object is helpful for the developers to get class level information.
69+
JVM also creates an object of **Class** type(available from **java.lang** package) to represent the file in the heap memory. But it will be created only on the very first time when the class file is loaded into JVM. This object is helpful for the developers to get class level information.
4470

4571
2. **Linking:** Linking is the process of taking a class or interface and combining it into the runtime state of the Java virtual machine, preparing it for execution. It involves the following steps:
4672

47-
1. **Verification:** This phase ensure the correctness of `.class` file by checking whether the file is properly formatted or not, and the code generated by a valid compiler or not. If the validation is failed, you will receive `java.lang.VerifyError` through **ByteCodeVerifier**.
73+
1. **Verification:** This phase ensure the correctness of `.class` file by checking whether the file is properly formatted or not, and the code generated by a valid compiler or not. If the validation is failed, you will receive `java.lang.VerifyError` through **ByteCodeVerifier**.
4874

49-
2. **Preparation:** In this step, JVM allocates memory for all static variables within classes/interfaces and assign them with default values.
75+
2. **Preparation:** In this step, JVM allocates memory for all static variables within classes/interfaces and assign them with default values.
5076

51-
3. **Resolution:** In this step, all symbolic references to classes or interfaces are replaced with their actual memory locations. This transformation is known as **Dynamic Linking**.
77+
3. **Resolution:** In this step, all symbolic references to classes or interfaces are replaced with their actual memory locations. This transformation is known as **Dynamic Linking**.
5278

5379
3. **Initialization:** This is the last phase of class loading, where all static variables are replaced with their original values and static block gets executed.
5480

@@ -119,7 +145,7 @@ Frequently asked Java Interview questions
119145

120146
**[⬆ Back to Top](#table-of-contents)**
121147

122-
2. ### What are the main features of Java
148+
4. ### What are the main features of Java
123149

124150
Java has many important features which makes it unique among the popular programming languages. Some of those features are:
125151
1. **Simple:** Java is a simple programming language and easy to understand without any complexities unlike in the prior programming languages.
@@ -163,19 +189,19 @@ Frequently asked Java Interview questions
163189

164190
**[⬆ Back to Top](#table-of-contents)**
165191

166-
3. ### What is string constant pool
192+
5. ### What is string constant pool
167193

168194
String constant pool is a storage space inside the heap memory area where string literals are stored. It is also known as String pool or String Intern Pool. This is privately maintained by String class and it is empty by default. Whenever you create a new string object, JVM checks for presence of string object in the string pool. If the string value is already present, the same object reference is shared with the variable, else a new string is created in the pool with the variable reference stored in stack area.
169195

170196
**[⬆ Back to Top](#table-of-contents)**
171197

172-
4. ### Why strings are immutable
198+
6. ### Why strings are immutable
173199

174200
Strings are immutable, that means the contents of string objects can't be modified after their creation. i.e, When you try to alter the string, it creates a new string. Due to this behavior, the internal state of a string remains the same throughout the execution of the program. This characteristic of immutability helps with the benefits of caching, security, synchronization, and performance.
175201

176202
**[⬆ Back to Top](#table-of-contents)**
177203

178-
5. ### What is the difference between StringBuffer and StringBuilder
204+
7. ### What is the difference between StringBuffer and StringBuilder
179205

180206
String is an immutable class to represent sequence of characters. Java also provided mutable version of String class through StringBuffer and StringBuilder. Even though both these classes are mutable, there are many differences between StringBuffer and StringBuilder.
181207

@@ -189,11 +215,39 @@ Frequently asked Java Interview questions
189215

190216
**[⬆ Back to Top](#table-of-contents)**
191217

192-
6. ### What is the importance of hashCode() and equals() methods
218+
8. ### What is the importance of hashCode() and equals() methods
219+
220+
Since both `equals()` and `hashCode()` methods are available in Object class(i.e, Java.lang.object), every java class gets the default implementation of equals and hashCode methods. These methods work together to verify if two objects have the same value or not.
221+
222+
**1. equals:** The `equals()` method is used to compare equality of two Objects. By default their implementation compares the identities of the object. The Object class defined `equals()` method as follows,
223+
224+
```java
225+
public boolean equals(Object obj) {
226+
return (this == obj);
227+
}
228+
```
229+
230+
There are some rules need to be followed to use equals method:
231+
1. **Reflexive:** For any object x, `x.equals(x)` should return `true`.
232+
2. **Symmetric:** For any two objects x and y, `x.equals(y)` should return `true` if and only if `y.equals(x)` returns `true`.
233+
3. **Transitive:** For multiple objects x, y, and z, `if x.equals(y)` returns `true` and `y.equals(z)` returns `true`, then `x.equals(z)` should return `true`.
234+
4. **Consistent:** For any two objects x and y, multiple invocations of `x.equals(y)` should return same result(`true` or `false`), unless any of the object properties is modified that is being used in the equals() method implementation.
235+
236+
**2. hashCode:** The `hashCode()` method returns the integer hash code value of the object. This method must be overridden in every class which overrides `equals()` method. The general contract of hashCode is:
237+
238+
1. While execution of the application, the multiple invocations of `hashCode()` on the object should return the same integer value, unless the object property used in the `equals()` method is being modified.
239+
2. During the multiple executions of the application, the object's hashCode can change.
240+
3. If two objects are equal based on `equals()` method, then their object's hash code must be same.
241+
4. If two objects are unequal based on `equals()` method, their hash code value may or may not be equal.
242+
243+
Together, the implementation of `equals()` and `hashCode()` should follow these rules.
244+
245+
1. If `x.equals(y)` is true, then `x.hashCode() === y.hashCode()` should always be true.
246+
2. If `x.hashCode() === y.hashCode()` is true, then it doesn't required to be `x.equals(y)` always true.
193247

194248
**[⬆ Back to Top](#table-of-contents)**
195249

196-
7. ### What is the difference between checked and unchecked expceptions
250+
9. ### What is the difference between checked and unchecked expceptions
197251

198252
An exception is an event that interrupts the normal flow the program execution. There are two types of exceptions,
199253
1. Checked exceptions
@@ -264,7 +318,7 @@ Frequently asked Java Interview questions
264318
265319
**[⬆ Back to Top](#table-of-contents)**
266320
267-
8. ### What are wrapper classes
321+
10. ### What are wrapper classes
268322
269323
Wrapper classes provides the mechanism to convert primitive types into object types and vice versa. Since Java is an object-oriented programming language, and many APIs and libraries in Java require objects instead primitive types. For example, data structures in collection framework, utility classes from `java.utils.*`,cloning process, Serialization, Synchronization etc require object type.
270324
@@ -318,7 +372,7 @@ Frequently asked Java Interview questions
318372
319373
**[⬆ Back to Top](#table-of-contents)**
320374
321-
9. ### What is the difference between abstract class and interface
375+
11. ### What is the difference between abstract class and interface
322376
323377
Both Abstract class and interface are used to define contracts in object-oriented programming. But there are some key differences between them as shown below,
324378
@@ -334,7 +388,7 @@ Frequently asked Java Interview questions
334388

335389
**[⬆ Back to Top](#table-of-contents)**
336390

337-
10. ### What are marker interfaces
391+
12. ### What are marker interfaces
338392

339393
Marker interfaces are interfaces that don't have any fields, methods, or constants inside of it. They are also known as empty interfaces or tag interfaces. Examples of marker interface are Serializable, Cloneable and Remote interface. The purpose of marker interfaces are to provide run-time type information about an object to JVM and Compiler. They are mainly used in API development and in frameworks like Spring to provide additional information to the class.
340394

@@ -388,4 +442,26 @@ Frequently asked Java Interview questions
388442
1. **Internal flags:** They can be used in place of marker interface to indicate any specific operation.
389443
2. **Annotations:** They are used as tags to represent the metadata attached to classes, interfaces, or methods to indicate additional information required by JVM.
390444

391-
**[⬆ Back to Top](#table-of-contents)**
445+
**[⬆ Back to Top](#table-of-contents)**
446+
447+
13. ### What are collections in Java?
448+
449+
Collections in Java is a unified framework that provides architecture for storing and manipulating a group of objects.
450+
451+
**[⬆ Back to Top](#table-of-contents)**
452+
453+
14. ### What are the differences between arraylist and vector?
454+
455+
Both Vector and ArrayList use dynamically resizable array as their internal data structure and implement the List interface. But there are couple of differences between them as listed below,
456+
457+
| ArrayList | Vector |
458+
| ------------------------ | -------------------- |
459+
| This data structure is part of Collection framework, introduced in JDK 1.2 | This is a legacy class |
460+
| The capacity increment of ArrayList is 50% if the number of elements exceeds the current size| The capacity increment of Vector is 100% if the number of elements exceeds the current size |
461+
| It is not synchronized by default. That means multiple threads can access it concurrently. | It is synchronized by default. That means only one thread can access it at a time. |
462+
| It is quite fast because it is not synchronized | It is quite slow due to synchronization |
463+
| ArrayList uses the Iterator interface to traverse over the elements. | Vector uses both the Iterator and Enumeration interfaces to traverse over the elements. |
464+
465+
**[⬆ Back to Top](#table-of-contents)**
466+
467+
<!-- QUESTIONS_END -->

images/JavaIndependent.png

34.2 KB
Loading

0 commit comments

Comments
 (0)