Java - Interview Questions and Answers for 'Initialization' | Search Java Interview Question - javasearch.buggybread.com
Javasearch.buggybread.com
Share

Search Java Interview Questions


 2098 questions in repository.
 There are more than 200 unanswered questions.
Click here and help us by providing the answer.
Label / Company      Label / Company / Text

   



Interview Questions and Answers for 'Initialization' - 18 question(s) found - Order By Newest

 Q1. What is the difference between declaration, instantiation and initialization ?Core Java2016-05-26 14:17:48

Ans. Declaration is intimation to the compiler about the nature of Data a reference is going to hold.

For example - List myList;

Instantiation is reservation of memory.

For example

myList = new ArrayList();

Initialization or construction is setting the default values for member elements.

For example

myList = new ArrayList(mySet);

** Example 2nd is both for instantiation as well as initialization. The only difference is that 2nd will initialized the member elements to their default values whereas 3rd will initialized it with the elements from set.


 Help us improve. Please let us know the company, where you were asked this question :   

   Like      Discuss      Correct / Improve     declaration   instantiation   initialization   construction      basic        frequent


Frequently asked question in companies using Hibernate.
  Q2. What is Lazy Initialization in Hibernate ?Hibernate
Admin
info@buggybread.com
Ans. It's a feature to lazily initialize dependencies , relationship and associations from the Database. Any related references marked as @OneToMany or @ManyToMany are loaded lazily i.e when they are accessed and not when the parent is loaded.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like      Discuss      Correct / Improve     hibernate   lazy loading hibernate   lazy initialization hibernate   architecture     overstock.com  BridgePoint Technologies  Medgate  Dex Media  Diversant  Tieto  Oracle  Bank of America Merrill LYNCH  NEC Japan  HCL Technologies      Basic        frequent

Try 2 Question(s) Test


 Q3. What are the ways to avoid LazyInitializationException ?Hibernate
Admin
info@buggybread.com
Ans. 1. Set lazy=false in the hibernate config file.

2. Set @Basic(fetch=FetchType.EAGER) at the mapping.

3. Make sure that we are accessing the dependent objects before closing the session.

4. Force initialization using Hibernate.initialize

5. Using Fetch Join in HQL.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like      Discuss      Correct / Improve     hibernate   lazy loading hibernate   lazy initialization hibernate   lazyinitializationexception   architecture     Squelch  Overstock.com

Try 2 Question(s) Test


 Q4. If everything is an object , Cant we declare every object as

Object obj = new String();
Core Java2016-12-02 14:45:05

Ans. Yes, we can do that. Compiler wont complain. But using object reference we can only access methods which have been defined for object class i.e clone(), equals(), hashCode(), toString() etc.

We cannot access methods defined in String class or in any class in hierarchy between String and Object.

For example - we cannot do obj.append("abc") as it will now give compile time error.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like      Discuss      Correct / Improve     object initialization  object declaration   runtime polymorphism


 Q5. Variable of the boolean type is automatically initialized as?Core Java
Admin
info@buggybread.com
Ans. The default value of the boolean type is false.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like      Discuss      Correct / Improve     java   data type   boolean   initialization   default object construction   default constructor   default value


 Q6. After which Hibernate version , related Entities are initialized lazily ?Hibernate
admin
info@buggybread.com
Ans. After Hibernate 3.0

 Help us improve. Please let us know the company, where you were asked this question :   

   Like      Discuss      Correct / Improve     hibernate   lazy loading hibernate   lazy initialization hibernate


 Q7. What is the difference between constructor and instance initialization blocks ?Core Java2016-07-19 11:25:30

Ans. Constructor has the same name as class name whereas instance initialization block just have a body without any name or visibility type.

instance initialization blocks are useful if we want to have some code run regardless of which constructor is used or if we want to do some instance initialization for anonymous classes.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like      Discuss      Correct / Improve     instance initialization blocks  constructor  constructor vs instance initialization blocks     BMC Software  Ericsson

Try 1 Question(s) Test


 Q8. Is this code legal in Java ?

public class BuggyBread {
{
System.out.println("HelloWorld.");
}
}
2016-07-19 11:26:59

Ans. Yes, It's an instance initialization block.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like      Discuss      Correct / Improve     code  instance initialization block

Try 1 Question(s) Test


 Q9. What are the points to be considered if we move from Eager initialization to Lazy Initialization in Hibernate ?Hibernate2017-01-20 16:00:11

Ans. Make sure that the properties of dependent Hibernate entities are not accessed and if yes, better wrap the whole code within single transaction.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like      Discuss      Correct / Improve     lazy loading  lazy initialization



Do you think these are the Best Java Frameworks ?

OpenXavaSPRING MVCApache StripesCheck everything
that is Best in Java

Click Here



 Q10. How does Lazy Initialization helps improving performance of an application ?Hibernate2017-01-20 16:18:52

Ans. Lazy Initialization means , Load Dependencies when required. Which means less load on application resources as only required data is loaded upfront. It's not only good for better performance but for better resource utilization too.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like      Discuss      Correct / Improve     lazy loading  lazy initialization        frequent


 Q11. What does the following exception means

org.hibernate.LazyInitializationException: could not initialize proxy - no Session
Hibernate2017-02-16 11:28:15

Ans. The error states that Hibernate is not able to initialize proxy / dependent entity objects as there is no session or transaction present. Very likely we are trying to load the dependent entities lazily but the call to dependent object property is not wrapped within the session or transaction.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like      Discuss      Correct / Improve     Lazy Loading  Lazy Initialization  org.hibernate.LazyInitializationException


 Q12. If you are given choice to avoid LazyInitializationException using any of the following measures, which are the ones you will choose and why ?

1. Set lazy=false in the hibernate config file.
2. Set @Basic(fetch=FetchType.EAGER) at the mapping.
3. Make sure that we are accessing the dependent objects before closing the session.
4. Force initialization using Hibernate.initialize
Hibernate2017-02-16 13:32:41

Ans. First resolution is a big No as it conveys no lazy loading in complete app. even second is advocating the same but for a particular mapping.

third one is most appropriate if loading and dependent entity property access is closer to each other in code and can be accomplished.

I don't mind using 4th too.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like      Discuss      Correct / Improve     lazy loading  lazy initialization  LazyInitializationException


 Q13. Is it advisable to keep session or transaction open for long time just to avoid LazyInitializationException ?Hibernate2017-02-16 20:33:52

 This question is still unanswered. Can you please provide an answer.


 Help us improve. Please let us know the company, where you were asked this question :   

   Like      Discuss      Correct / Improve     LazyInitializationException  Lazy Loading


 Q14. What will be the output of executing following class ?

public class BuggyBread {

static {
System.out.println("Static Block");
}

{
System.out.println("Initialization Block");
}

BuggyBread(){
System.out.println("Constructor");
}

public static void main(String[] args){
System.out.println("Main Method");
}
}
Core Java
a. Static Block
Main Method
b. Static Block
Instance Initialization Block
Main Method
c. Static Block
Constructor
Main Method
d. Static Block
Instance Initialization Block
Constructor
Main Method

Ans.a. Static Block
Main Method

 Q15. What will be the output upon executing following class ?

public class BuggyBread {

static {
System.out.println("Static Block");
}

{
System.out.println("Instance Initialization Block");
}

BuggyBread(){
System.out.println("Constructor");
}

public static void main(String[] args){
System.out.println("Main Method");
new BuggyBread();
}
}
Core Java
a. Instance Initialization Block
Constructor
Static Block
Main Method
b. Static Block
Instance Initialization Block
Constructor
Main Method
c. Main Method
Static Block
Instance Initialization Block
Constructor
d. Static Block
Main Method
Instance Initialization Block
Constructor

Ans.d. Static Block
Main Method
Instance Initialization Block
Constructor

 Q16. What is Lazy Initialization in Hibernate ?Hibernate
a. Feature to load the dependencies from Cache
b. Feature to load all objects and relationships in advance before they can be used
c. Feature to not load dependencies and relationship in advance and load when required
d. Feature to not load the dependencies and relationships at all

Ans.c. Feature to not load dependencies and relationship in advance and load when required

 Q17. Which of the following is not the benefit of Lazy Initialization in Hibernate ?Hibernate
a. Laod When required provides better performance
b. Object stays lighter
c. Less number of Database calls
d. Less load on Database

Ans.c. Less number of Database calls

 Q18. Which of following is not the resolution for preventing LazyInitializationException?Hibernate
a. Set fetch=FetchType.EAGER for Dependent entity mapping
b. Make sure that we are accessing the dependent objects before closing the session
c. Make sure that we are accessing the dependent objects after closing the session
d. Wrap the complete access within Transaction

Ans.c. Make sure that we are accessing the dependent objects after closing the session



Subscribe to Java News and Posts. Get latest updates and posts on Java from Buggybread.com
Enter your email address:
Delivered by FeedBurner



comments powered by Disqus
 

Help us and Others Improve. Please let us know the questions asked in any of your previous interview.

Any input from you will be highly appreciated and It will unlock the application for 10 more requests.

Company Name:
Questions Asked:
         

X Close this

X Close this

Help Us Improve.
Please share your
interview experience.

Company Name:   


Questions Asked: