11 Questions
What is the correct way to create an array with 20 double elements?
double[] array = new double[20];
Given the declaration 'int[] array = {4, 3, 5, 2, 0};', what is the value of 'array[2]'?
5
Which statement would cause a run-time error for the declaration 'int[] array = {4, 3, 5, 2, 0};'?
double[] array = new double;
What output is generated by the code segment below:
int[] values = new int; for (int k = 0; k < values.length; k++) { values[k] = k * 2; } System.out.println(values);
[2]
To initialize array elements with values from 1 to 100, which statement should be inserted in the code fragment below?
int[] nums = new int[100]; for (int i = 0; i < nums.length; i++) { // INSERT STATEMENT HERE }
nums[i] = i+1;
What is the correct way to initialize an array in Java?
a.array = 1;
In the given code, what would be the output of System.out.print(values); System.out.print(" " + numbers);?
[0, 2, 4, 6, 8] [0, 2, 4, 6, 8]
What does the statement 'a.numbers[k] = k + 1;' inside the for loop in the given code segment do?
It assigns the value of k+1 to numbers array element at index k.
What is the purpose of the following code segment: 'System.out.println(number);'?
It prints the value of 'number' in the console.
What would be the output of the following code segment: 'System.out.print(count);'?
4
Assuming 'values' is an array of integer values, what does the condition 'values[k] % 2 == 0' in a for loop check for?
Check if the element at index k in 'values' is divisible by 2.
Study Notes
Array Declaration and Initialization
- To create an array with 20 double elements, use
double[] array = new double[20];
- The correct syntax for declaring and initializing an array is
int[] array = {4, 3, 5, 2, 0};
Array Element Access
- The value of
array
in the declarationint[] array = {4, 3, 5, 2, 0};
is4
at index0
,3
at index1
,5
at index2
,2
at index3
, and0
at index4
. - Accessing an array element out of bounds causes a runtime error.
Looping and Array Initialization
- The code segment
int[] values = new int[10]; for (int k = 0; k < values.length; k++) { values[k] = k * 2; }
initializes an array with values0
,2
,4
, ...,18
. - The missing statement in the code fragment
int[] numbers = new int[100]; for (int k = 0; k < numbers.length; k++) { ________________________ }
isnumbers[k] = k + 1;
Array Variables and Enhanced for Loop
- The code segment
int[] values = { 1, 3, 1, 3, 1 }; int[] numbers = values; for (int k = 0; k < values.length; k++) { values[k] = k * 2; }
outputs0 2 4 6 8
forvalues
and0 2 4 6 8
fornumbers
. - The enhanced for loop
int[] values = { 1, 3, 5, 3, 7 }; int count = 0; for (int number : values) { if (number > 4) { count++; } }
outputs2
forcount
. - The most appropriate statement for the body of an enhanced for loop is
System.out.println(number);
Array Algorithm
- The purpose of the code segment
int count = 0; for (int k = 0; k < values.length; k++) { if (values[k] % 2 == 0) { System.out.println(number); } }
is to count the number of even values in the arrayvalues
.
Test your knowledge of Java arrays declaration and initialization with this practice quiz. Questions involve selecting correct array declaration syntax and identifying array element values.
Make Your Own Quizzes and Flashcards
Convert your notes into interactive study material.
Get started for free