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

Search Interview Questions


 3193 questions in repository.
 There are more than 200 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 'Array initialization' - 6 question(s) found - Order By Newest

 Q1. Can we declare an array without assigning the size of an array? Core Java2018-07-22 07:36:26

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


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

String[] strArray = new String[2];
strArray = new String[5];
Core Java2018-07-25 08:01:42

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


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

String[] strArray = new String[];
Core Java2018-07-25 07:52:50

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


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

String[] strArray = new String[]{"Buggy","Bread"};
Core Java2018-07-25 07:54:15

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


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

String[] strArray = new String[3]{"Buggy","Bread"};
Core Java2018-07-25 07:55:04

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


 Q6. Is this code valid

String[] strArray = new String[2];
strArray.length = 5;
Core Java2018-07-25 08:00:02

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




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: