Java Interview Questions
These are tried and tested questions with answers that an Interview tries to put a Java Back-end Engineer into. Feel free to comment your views and discuss each question in the comments section:-
Its one of my fav question that I like to ask to a fresher or an entry level engineer. If the interviewee says its a programming language he’s already inclined towards rejection form my side.
However the correct answer is that Java is a programming language along with a runtime environment which is platform independent. By platform independent I mean that Java code complied on one platform say Linux can easily run on another platform like Windows.
equals() is used to determine whether two objects are meaningfully the same or not. Whereas hashcode() is used to group various objects into a finite number of sections.
No. Two nonequal objects can also have the same hashcode. However, if two objects are equal then they should have the same hashcode. And it depends on how we have overridden the hashcode method
the final keyword is a way of suggesting due compiler that this resource ie class/ method/ reference cannot be modified further.
their use at various levels is as follows:-
- class level:- if used at class level it suggests the compiler that this class cannot be sub-classed further.
- method level:- if used at one method level it restricts the compiler for allowing that particular method to be overridden in any of the subclasses
- reference level:- if used at the reference level it restricts the compiler for allowing the new keyword from being called at that reference again.
Exceptions in java are basically classified into two categories:- Checked and unchecked. Their usage is as follows:-
1) Checked Exceptions:-These are the exceptions for which we can do something at the time of writing our code i. e. at compile time. For e. g. using try-catch blocks or throws clause
2) Unchecked Exceptions:- These are those exceptions that we cannot handle at the time of writing our code i. e. at compile time. The appropriate thing that we can do is to place checks to ensure these exceptions do not occur in the first place for e.g. placing null checks to avoid NullPointerException.
No. If we want to see the latest value every time we should synchronize our reads and writes by either using the synchronize keyword that java offers or synchronizing the getters and setters beer that variable.
We can also make that variable volatile.
No, we can’t throw broader exceptions while overriding methods. Reason being that it would break the polymorphic behavior of parent-child classes.
No, we can’t throw broader exceptions while overriding methods. Reason being that it would break the polymorphic behavior of parent-child classes.
JUnit is a unit testing library inbuilt in Java.
JUnit serve the following purposes:-
- They establish one contract for your classes
- They help you test the key business functionality before actually entering the integration phase
- They help in over camming the commonly occurring exceptions by fallowing a fail fast approach.
JUnit is a unit testing library inbuilt in Java.
JUnit serve the following purposes:-
- They establish one contract for your classes
- They help you test the key business functionality before actually entering the integration phase
- They help in over camming the commonly occurring exceptions by fallowing a fail fast approach.
In test-driven development, we first write JUNIT test cases for the classes/methods that we are designing before actually writing the implementation of our classes/ methods. So, initially, all the test cases are failing, we then continue to add implementations to make test cases pass one by one thereby completing our development when all the test cases pass.
A crisp answer to this question is, the addresses of the references is passed by value. So its kind of amalgamation between the two.
- Memory is divided into stack and heap.
- Stack is further subdivided into per thread stack.
- Variables local to the thread live on the stack as they need not be shared with any other thread.
- Heap is the common place where object live so that they can be share amogst different threads each having its own stack.
- For concurrent updation of objects in different threads we must synchronise before accessing the objects
- Streams: – the core benefit that streams offer is that streams are inherently lazy. Meaning that the iteration does not actually start until are iterable value is actually required. Another benefit is that it makes its own code a bit cleaner. Also, parallel streams help us leverage multiprocessor capability.
Lambdas: – lambda expressions are basically anonymous functions. The benefit of using them is that if a method is to be used only once, replacing it with lambda expression will move over to implementations where they are actually called.
Optionals:- optional are introduced to provide null safety. We generally wrap the nullable values is optional so at the time of accessing them, the caller is reminded to check if value is present or not.
Method reference:- introduction of method reference enables us to pass methods as arguments. This feature was introduced to compete with python.
-
the solid principle provides us with a guideline for designing classes.
The single responsibility:-A class should only partake in one responsibility.
Open closed:- classes should be open for enhancement but closed for modification.
Liskov’s substitution: objects of a superclass shall be replaceable with objects of its subclasses without breaking the application
Interface segregation:- Interfaces should be designed such that they can be segregated into one common interface.
Dependency injection:- classes should not be instantiating their dependencies themselves. This task should be handed over to a container class.
-
Our databases should be designed such that they possess their characteristics: –
Atomicity:- all the writes to a database should complete in one go.
Consistency: – any changes once made should not get lost.
Isolation:- all the writes should operate independently of each other.
Dependability:- Data fetched from the database should be correct and error-free.
-
Array List is used for faster random access and LinkedList is used for faster removal from the middle of the list.
15 comments