Hi friends,
In this post, I am sharing interview questions and answers asked in ThalesGroup round-1.
Question 1:
Is the following source code is valid override?
class MyClass{
void add(int i, int j){
}
}
class Subclass extends MyClass{
public void add(int i, int j){
}
}
Answer:
Yes, the source code is valid override, as we can increase the visibility of overridden method in subclass.
Question 2:
Is the following source code is valid override?
class MyClass{
private void add(int i, int j){
}
}
class SubClass extends MyClass{
public void add(int i, int j){
}
}
Answer:
Yes, answer is same as for question 1.
But we cannot use below code for above code:
MyClass myClass = new SubClass();
myClass.add(1,2); // add() is private , cannot call.
Question 3:
Is the following source code is valid override?
class MyClass{
static void add(int i, int j){
}
}
class SubClass extends MyClass{
static void add(int i, int j){
}
}
Answer:
It is valid, but static methods cannot be overridden. It is called method hiding.
So, when we use below code:
MyClass myClass = new SubClass();
myClass.add(1,2); // Calls add() method of class MyClass.
Question 4:
What are different types of caches in hibernate?
Answer:
Hibernate uses two different caches for objects:
- First-Level cache: It is associated with session object.
- Second-Level cache It is associated with SessionFactory object.
Question 5:
How does hibernate second-level cache work?
Answer:
Hibernate always tries to first retrieve the objects from session[first-level-cache] and if it fails, then it tries to retrieve them from second-level cache. If this fails again, the objects are directly loaded from the database.
Hibernate's static initialize() method which populates a proxy object, will attempt to hit the second-level cache before going to the database. The hibernate class provides static methods for manipulation of proxies.
Question 6:
What is version checking in hibernate?
Answer:
Version checking is used hibernate when more than one thread try to access same data.
e.g.:
User A tries to update a row in table, in the same time , user B also tries to update the row in a table and click on update after making changes. Now user A clicks on update. This way, changes made by user B are gone.
It is called stale object updation. And in hibernate , we can avoid stale object updation using version checking.
How it works:
Check the version of the row when you are updating the row. Get the version of the row, when we are fetching the row from the table. At the time of updation, just fetch the version number and match with our version number. This way, we can prevent stale object updation.
Question 7:
What is the general contract between hashcode() and equals() method?
Answer:
- Whenever hashcode is invoked on the same object multiple times, it should return same integer value during the execution of a program, provided no info in equals() method is changed. This integer need not be same from one execution of a program to another execution of the same program.
- If two objects are equal according to equals() method, then their hashcodes must be same.
- It is not required that if two objects are unequal , then their hashcodes must be different. However programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hashtables.
Question 8:
Suppose we have a real time big application and one service is calling another service for fetching real time data. And another service is slow somehow, then what approach will be used to ensure that first service should bet real time data for each request?
Answer:
We can implement a messaging queue between these two services and store every message in that queue and second service can easily consume these messages according to it's speed.
That's for this post.
Thanks for reading!!
No comments:
Post a Comment