Skip to content

Commit 3428f17

Browse files
committed
Update README.md
1 parent b3ceaf1 commit 3428f17

File tree

1 file changed

+14
-93
lines changed

1 file changed

+14
-93
lines changed

README.md

Lines changed: 14 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Java, J2EE, JSP, Servlet, Hibernate Interview Questions
1+
# Java Interview Questions
22

33
*Click <img src="assets/star.png" width="18" height="18" align="absmiddle" title="Star" /> if you like the project. Pull Request are highly appreciated.*
44

@@ -28,10 +28,13 @@ Exception is an error event that can happen during the execution of a program an
2828
**2. Unchecked Exception**: The classes which inherit `RuntimeException` are known as unchecked exceptions e.g. ArithmeticException, NullPointerException, ArrayIndexOutOfBoundsException etc. Unchecked exceptions are not checked at compile-time, but they are checked at runtime.
2929
**3. Error**: Error is irrecoverable e.g. OutOfMemoryError, VirtualMachineError, AssertionError etc.
3030

31-
**Hierarchy of Java Exception classes**
32-
The java.lang.Throwable class is the root class of Java Exception hierarchy which is inherited by two subclasses: Exception and Error.
31+
**Hierarchy of Java Exception classes:**
3332

34-
<img src="assets/exception.png" alt="Java Exception" />
33+
The java.lang.Throwable class is the root class of Java Exception hierarchy which is inherited by two subclasses: Exception and Error.
34+
35+
<p align="center">
36+
<img src="assets/exception.png" alt="Exception in Java" width="500px" />
37+
</p>
3538

3639
Example:
3740

@@ -91,8 +94,6 @@ public class CustomExceptionExample {
9194

9295
## Q. ***What is the difference between aggregation and composition?***
9396

94-
<img src="assets/aggregation.png" alt="Aggregation" />
95-
9697
**Aggregation**: We call aggregation those relationships whose **objects have an independent lifecycle, but there is ownership**, and child objects cannot belong to another parent object.
9798

9899
Example: Since Organization has Person as employees, the relationship between them is Aggregation. Here is how they look like in terms of Java classes
@@ -126,6 +127,10 @@ class Engine {
126127
}
127128
```
128129

130+
<p align="center">
131+
<img src="assets/aggregation.png" alt="Aggregation" width="400px" />
132+
</p>
133+
129134
<table class="alt">
130135
<tbody><tr><th>Aggregation</th><th>Composition</th></tr>
131136
<tr><td>Aggregation is a weak Association.</td><td>Composition is a strong Association.</td></tr>
@@ -299,13 +304,6 @@ class HybridCar extends Car {
299304
//...
300305
}
301306
```
302-
<div align="right">
303-
<b><a href="#table-of-contents">↥ back to top</a></b>
304-
</div>
305-
306-
## Q. ***Can we import same package/class two times? Will the JVM load the package twice at runtime?***
307-
308-
We can import the same package or same class multiple times. The JVM will internally load the class only once no matter how many times import the same class.
309307

310308
<div align="right">
311309
<b><a href="#table-of-contents">↥ back to top</a></b>
@@ -378,14 +376,6 @@ JVM is a program which takes Java bytecode and converts the byte code (line by l
378376
<b><a href="#table-of-contents">↥ back to top</a></b>
379377
</div>
380378

381-
## Q. ***What will be the initial value of an object reference which is defined as an instance variable?***
382-
383-
The object references are all initialized to `null` in Java. However in order to do anything useful with these references, It must set to a valid object, else you will get NullPointerExceptions everywhere you try to use such default initialized references.
384-
385-
<div align="right">
386-
<b><a href="#table-of-contents">↥ back to top</a></b>
387-
</div>
388-
389379
## Q. ***How can constructor chaining be done using this keyword?***
390380

391381
Java constructor chaining is a method of calling one constructor with the help of another while considering the present object. It can be done in 2 ways –
@@ -507,16 +497,6 @@ Cannot override the final method from Test.
507497
<b><a href="#table-of-contents">↥ back to top</a></b>
508498
</div>
509499

510-
## Q. ***What is the difference between the final method and abstract method?***
511-
512-
Final method is a method that is marked as final, i.e. it cannot be overridden anymore. Just like final class cannot be inherited anymore.
513-
514-
Abstract method, on the other hand, is an empty method that is ought to be overridden by the inherited class. Without overriding, you will quickly get compilation error.
515-
516-
<div align="right">
517-
<b><a href="#table-of-contents">↥ back to top</a></b>
518-
</div>
519-
520500
## Q. ***What is the difference between compile-time polymorphism and runtime polymorphism?***
521501

522502
There are two types of polymorphism in java:
@@ -580,14 +560,6 @@ Overriding Method
580560
<b><a href="#table-of-contents">↥ back to top</a></b>
581561
</div>
582562

583-
## Q. ***Can you achieve Runtime Polymorphism by data members?***
584-
585-
No, we cannot achieve runtime polymorphism by data members. Method is overridden not the data members, so runtime polymorphism can not be achieved by data members.
586-
587-
<div align="right">
588-
<b><a href="#table-of-contents">↥ back to top</a></b>
589-
</div>
590-
591563
## Q. ***Can you have virtual functions in Java?***
592564

593565
In Java, all non-static methods are by default **virtual functions**. Only methods marked with the `keyword final`, which cannot be overridden, along with `private methods`, which are not inherited, are non-virtual.
@@ -665,24 +637,18 @@ Subclass
665637
<b><a href="#table-of-contents">↥ back to top</a></b>
666638
</div>
667639

668-
## Q. ***Can there be an abstract method without an abstract class?***
669-
670-
Yes. because methods in an interface are also abstract. so the interface can be use to declare abstract method.
671-
672-
<div align="right">
673-
<b><a href="#table-of-contents">↥ back to top</a></b>
674-
</div>
675-
676640
## Q. ***Can we use private or protected member variables in an interface?***
677641

678642
The java compiler adds public and abstract keywords before the interface method and **public, static and final keyword** before data members automatically
643+
679644
```java
680645
public interface Test {
681646
public string name1;
682647
private String email;
683648
protected pass;
684649
}
685650
```
651+
686652
as you have declare variable in test interface with private and protected it will give error. if you do not specify the modifier the compiler will add public static final automatically.
687653
```java
688654
public interface Test {
@@ -790,13 +756,6 @@ public class CopyFile {
790756
}
791757
}
792758
```
793-
<div align="right">
794-
<b><a href="#table-of-contents">↥ back to top</a></b>
795-
</div>
796-
797-
## Q. ***Can you access non static variable in static context?***
798-
799-
No, non-static variable cannot be referenced in a static context directly one needs to use object.
800759

801760
<div align="right">
802761
<b><a href="#table-of-contents">↥ back to top</a></b>
@@ -838,14 +797,6 @@ public class Example {
838797
<b><a href="#table-of-contents">↥ back to top</a></b>
839798
</div>
840799

841-
## Q. ***Can we have multiple public classes in a java source file?***
842-
843-
A Java source file can have only one class declared as **public**, we cannot put two or more public classes together in a **.java** file. This is because of the restriction that the file name should be same as the name of the public class with **.java** extension. If we want to multiple classes under consideration are to be declared as public, we have to store them in separate source files and attach the package statement as the first statement in those source files.
844-
845-
<div align="right">
846-
<b><a href="#table-of-contents">↥ back to top</a></b>
847-
</div>
848-
849800
## Q. ***What is the difference between abstract class and interface?***
850801

851802
Abstract class and interface both are used to achieve abstraction where we can declare the abstract methods. Abstract class and interface both can't be instantiated.
@@ -977,17 +928,11 @@ class Test {
977928
}
978929
```
979930
Output
931+
980932
```
981933
boolean
982934
Test
983935
```
984-
<div align="right">
985-
<b><a href="#table-of-contents">↥ back to top</a></b>
986-
</div>
987-
988-
## Q. ***What is the default value of the local variables?***
989-
990-
There is no default value for local variables, so local variables should be declared and an initial value should be assigned before the first use.
991936

992937
<div align="right">
993938
<b><a href="#table-of-contents">↥ back to top</a></b>
@@ -2988,30 +2933,6 @@ public class MainClass {
29882933
<b><a href="#table-of-contents">↥ back to top</a></b>
29892934
</div>
29902935

2991-
## Q. ***What code coverage tools are you using for your project?***
2992-
2993-
* <a href="https://cobertura.github.io/cobertura/" target="_blank">Cobertura</a>
2994-
2995-
<div align="right">
2996-
<b><a href="#table-of-contents">↥ back to top</a></b>
2997-
</div>
2998-
2999-
## Q. ***Scenario of browser’s browsing history, where you need to store the browsing history, what data structure will you use.?***
3000-
3001-
* use `stack`
3002-
3003-
<div align="right">
3004-
<b><a href="#table-of-contents">↥ back to top</a></b>
3005-
</div>
3006-
3007-
## Q. ***Scenario where in we have to download a big file by clicking on a link, how will you make sure that connections is reliable throughout?***
3008-
3009-
* use `persistent MQueues`
3010-
3011-
<div align="right">
3012-
<b><a href="#table-of-contents">↥ back to top</a></b>
3013-
</div>
3014-
30152936
## Q. ***What are methods of Object Class?***
30162937

30172938
The Object class is the parent class of all the classes in java by default.

0 commit comments

Comments
 (0)