Array Declaration and Initialization Quiz
10 Questions
1 Views

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to Lesson

Podcast

Play an AI-generated podcast conversation about this lesson

Questions and Answers

In practice 6.09, what does the code segment count in the 'values' array?

  • The number of even elements (correct)
  • The average of all elements
  • The number of odd elements
  • The sum of all elements

What is missing in the code in practice 6.10 to correctly manipulate array elements?

  • values[k] = Math.pow(values[k], 2); (correct)
  • values[k] = sqrt(values[k]);
  • values[k] = values[k]^2;
  • values[k] = values[k] * values[k];

How would you correctly call the 'display' method with the 'values' array in practice 6.11?

  • display(int[] values);
  • display(int[] array=values);
  • display(values); (correct)
  • display(array=values);

How can you complete the 'firstPlusLast' method in practice 6.13 to return the sum of the first and last elements of an array?

<p>array[0] + array[array.length - 1]; (B)</p> Signup and view all the answers

Which of the following is the correct way to create an array with 20 double elements?

<p>double[] array = new double[20]; (A), double[] array = new double[20]; (B)</p> Signup and view all the answers

Given the declaration int[] array = {4, 3, 5, 2, 0};, what is the value of array[2]?

<p>5 (C)</p> Signup and view all the answers

Which of the following statements would cause a run-time error with the declaration int[] array = {4, 3, 5, 2, 0};?

<p>array[5] = 10; (A)</p> Signup and view all the answers

What output is generated by the code segment below? int[] values = new int[5]; for (int k = 0; k < values.length; k++) { values[k] = k * 2; } System.out.println(values);

<p>[0, 2, 4, 6, 8] (C)</p> Signup and view all the answers

Insert the missing statement in the code fragment to initialize the array elements with the values 1, 2, 3,..., 100. int[] numbers = new int[100]; for (int k = 0; k < numbers.length; k++) { }

<p>numbers[k] = k + 1; (A)</p> Signup and view all the answers

What output is generated by the code fragment below? int[] values = {1, 3, 1, 3, 1}; int[] numbers = values; for (int k = 0; k < values.length; k++) { values[k] = k * 2; } System.out.print(values); System.out.print(" " + numbers);

<p>[0, 2, 4, 6, 8] [0, 2, 4, 6, 8] (A)</p> Signup and view all the answers

More Like This

Use Quizgecko on...
Browser
Browser