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

Search Interview Questions


 More than 3000 questions in repository.
 There are more than 900 unanswered questions.
Click here and help us by providing the answer.
 Have a video suggestion.
Click Correct / Improve and please let us know.
Label / Company      Label / Company / Text

   



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

 Q1. What is the difference between declaration, instantiation and initialization ?Core Java
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   declaration vs instantiation   instantiation vs initialization   declaration vs initialization     Asked in 1 Companies      basic        frequent


Frequently asked question in companies using Hibernate.
  Q2. What is Lazy Initialization in Hibernate ?Hibernate
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.

  Sample Code for Lazy Initialization

 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     Asked in 77 Companies      Basic        frequent

Try 2 Question(s) Test


 Q3. What are the ways to avoid LazyInitializationException ?Hibernate
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     Asked in 2 Companies

Try 2 Question(s) Test


 Q4. If arrays cannot be resized , Why is this code valid

String[] strArray = new String[2];
strArray = new String[5];
Core Java
Ans. We are not resizing the first array here but assigning the reference strArray to a new Array with size 5.

So after line 2, We have 2 arrays in memory, one with size 2 and other with size 5 with strArray referring to second array with size 5.

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

   Like         Discuss         Correct / Improve     arrays  array initialization


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

Object obj = new String();
Core Java
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  object oriented programming (oops)  oops concepts


 Q6. Can we declare an array without assigning the size of an array? Core Java
Ans. No, It will throw compile time error saying "must provide either dimension expressions or an array initializer"

Alternatively we can provide array initializer like

String[] strArray = new String[]{"Buggy","Bread"};

which will initialize it to size 2 with values as "Buggy" and "Bread"


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

   Like         Discuss         Correct / Improve     arrays  array initialization


 Q7. Variable of the boolean type is automatically initialized as?Core Java
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


 Q8. After which Hibernate version , related Entities are initialized lazily ?Hibernate
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


 Q9. What is the difference between constructor and instance initialization blocks ?Core Java
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     Asked in 2 Companies

Try 1 Question(s) Test


 Q10. Is this code legal in Java ?

public class BuggyBread {
{
System.out.println("HelloWorld.");
}
}
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


 Q11. What are the points to be considered if we move from Eager initialization to Lazy Initialization in Hibernate ?Hibernate
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


 Q12. How does Lazy Initialization helps improving performance of an application ?Hibernate
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


 Q13. What does the following exception means

org.hibernate.LazyInitializationException: could not initialize proxy - no Session
Hibernate
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


 Q14. 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
Hibernate
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


 Q15. Is it advisable to keep session or transaction open for long time just to avoid LazyInitializationException ?Hibernate
Ans. No. It's a resource and performance overhead.

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

   Like         Discuss         Correct / Improve     LazyInitializationException  Lazy Loading


 Q16. Is this array declaration correct ? If not , Why ?

String[] strArray = new String[];
Core Java
Ans. No, We haven't specified the size of array to be initialized.

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

   Like         Discuss         Correct / Improve     arrays  array initialization


 Q17. Is this array initialization correct ? If Yes, What will be the size of array ?

String[] strArray = new String[]{"Buggy","Bread"};
Core Java
Ans. Yes, size of the array will be 2.

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

   Like         Discuss         Correct / Improve     arrays  array initialization


 Q18. Is this array initialization correct ? If Yes, What will be the size of array ?

String[] strArray = new String[3]{"Buggy","Bread"};
Core Java
Ans. No. It will result in error saying "Cannot define dimension expressions when an array initializer is provided"

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

   Like         Discuss         Correct / Improve     arrays  array initialization


 Q19. Is this code valid

String[] strArray = new String[2];
strArray.length = 5;
Core Java
Ans. It will give compile time error saying "The final field array.length cannot be assigned"

Arrays once initialized cannot be resized.

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

   Like         Discuss         Correct / Improve     arrays  array initialization


 Q20. 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

 Q21. 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

 Q22. 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

 Q23. 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

 Q24. 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


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: