Java Arrays Declaration and Initialization Practice

RiskFreeAustin avatar
RiskFreeAustin
·
·
Download

Start Quiz

Study Flashcards

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 declaration int[] array = {4, 3, 5, 2, 0}; is 4 at index 0, 3 at index 1, 5 at index 2, 2 at index 3, and 0 at index 4.
  • 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 &lt; values.length; k++) { values[k] = k * 2; } initializes an array with values 0, 2, 4, ..., 18.
  • The missing statement in the code fragment int[] numbers = new int[100]; for (int k = 0; k &lt; numbers.length; k++) { ________________________ } is numbers[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 &lt; values.length; k++) { values[k] = k * 2; } outputs 0 2 4 6 8 for values and 0 2 4 6 8 for numbers.
  • The enhanced for loop int[] values = { 1, 3, 5, 3, 7 }; int count = 0; for (int number : values) { if (number &gt; 4) { count++; } } outputs 2 for count.
  • 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 &lt; values.length; k++) { if (values[k] % 2 == 0) { System.out.println(number); } } is to count the number of even values in the array values.

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

More Quizzes Like This

Use Quizgecko on...
Browser
Browser