Each class variable, instance variable, or array component is initialized with a default value when it is created. For example;
int[] intArray = new int[10];
This allocates the memory for an array of size 10. This size is immutable.
Java populates our array with default values depending on the element type - 0 for integers, false for booleans, null for objects, etc. Let's see more of how we can instantiate an array with values we want.
The slow way to initialize your array with non-default values is to assign values one by one:
Answer is B.
To test:
public static void main(String[] args) {
int numbers[];
numbers = new int[2];
numbers [0] = 10;
numbers [1] = 20;
numbers = new int [4];
numbers [2] = 30;
numbers [3] = 40;
for (int x : numbers) {
System.out.println(" " + x);
}
}
The correct one is B, because a new reference is given to the numbers list, which overwrites the old one and 30 is added to position 2 and 40 to position 3 of the list, as it was reset, position 0 and 1 had no numbers, so they won the value of 0 because when an int has no reference to a number, 0 is given for the pattern, so when printing the list it outputs "0 0 30 40"
A voting comment increases the vote count for the chosen answer by one.
Upvoting a comment with a selected answer will also increase the vote count towards that answer by one.
So if you see a comment that you already agree with, you can upvote it instead of posting a new comment.
mvpVN
Highly Voted 5 years, 9 months agoMPignaProTech
Most Recent 1 month, 2 weeks agofvelazqueznava
1 year, 2 months agoakbiyik
2 years agocarloswork
2 years, 1 month agoiSnover
2 years, 2 months agonesreenmhd123
4 years, 4 months agopg13
4 years, 5 months agoSamAru
4 years, 5 months agov323rs
4 years, 11 months agomuksa
4 years, 12 months agorasifer
5 years, 5 months agopawankalyan
5 years, 6 months ago