Java Collections Quiz

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to Lesson

Podcast

Play an AI-generated podcast conversation about this lesson
Download our mobile app to listen on the go
Get App

Questions and Answers

What keyword is used to declare a new array in programming?

  • array
  • create
  • define
  • new (correct)

What will the first index of an array always be?

  • 2
  • 0 (correct)
  • 1
  • -1

What is the default value of elements in a new String array when using the new method?

  • null (correct)
  • zero
  • empty string
  • undefined

What is an example of a perfect size array?

<p>An array that holds fixed values like the days of the week (C)</p> Signup and view all the answers

What occurs when you attempt to store an int in a String array?

<p>It generates a runtime error. (B)</p> Signup and view all the answers

What does it mean when an array is said to be a 'perfect size' array?

<p>The array has a size fixed by the program context and element count matches allocated memory. (A)</p> Signup and view all the answers

How are perfect size arrays passed to methods in programming?

<p>Using only the array’s reference (C)</p> Signup and view all the answers

Which of the following describes the use of initializer lists in array creation?

<p>They set both the array's length and initialize its elements to specified values. (C)</p> Signup and view all the answers

What does the term 'indices' refer to in the context of an array?

<p>The positions of elements within the array (B)</p> Signup and view all the answers

What default value does an integer array initialize its elements to when using the 'new' method?

<p>0.0 (B), 0 (C)</p> Signup and view all the answers

What will be the output when printing an array without specifying an index?

<p>The memory location of the array (B)</p> Signup and view all the answers

Which of the following is a common error when working with arrays?

<p>ArrayIndexOutOfBoundsException (D)</p> Signup and view all the answers

How would you modify an element in an array?

<p>Find the element's index and assign a new value to it (A)</p> Signup and view all the answers

What happens to a String array when initialized using the 'new' method?

<p>It consists of null elements (C)</p> Signup and view all the answers

What is the final output when the cars ArrayList is processed?

<p>A Prius is available. (D)</p> Signup and view all the answers

What will happen if grades.set(0, 42) is added after grades.add(98);?

<p>The first grade will be replaced with 42. (A)</p> Signup and view all the answers

If you attempt to access an index that is out of range for an array, what is typically the result?

<p>A runtime error will be thrown (D)</p> Signup and view all the answers

What does the following code snippet print: System.out.println(double[] decimals = new double)?

<p>A reference to decimals in memory (D)</p> Signup and view all the answers

Which of the following would correctly modify the code to find the maximum grade?

<p>int max = grades.get(0); for (int i : grades) { if (i &gt; max) { max = i; } } (A)</p> Signup and view all the answers

What is the purpose of the line letters.remove(0) in the reversing code?

<p>To remove the first letter in each iteration. (C)</p> Signup and view all the answers

Why is an enhanced for loop suitable for finding the minimum grade?

<p>It does not change the elements of the ArrayList. (D)</p> Signup and view all the answers

What will the output be after reversing the letters ArrayList?

<p>[E, D, C, B, A] (C)</p> Signup and view all the answers

What is the initial value of min before searching for the minimum grade?

<p>The first element in the grades ArrayList. (A)</p> Signup and view all the answers

What is a key difference between the methods used to find the minimum and maximum grades?

<p>Minimum uses less than, maximum uses greater than. (C)</p> Signup and view all the answers

What is the primary purpose of a 2D array?

<p>To represent data in rows and columns like a table. (C)</p> Signup and view all the answers

When defining a 2D array, which syntax correctly initializes a 2D array with 3 rows and 2 columns?

<p>String[][] names = new String[3][2]; (A)</p> Signup and view all the answers

Which statement correctly accesses the element in the second row and third column of a 2D array named 'data'?

<p>data[1][2]; (A)</p> Signup and view all the answers

What is the correct way to print the number of columns in the first row of a 2D array named 'records'?

<p>System.out.println(records[0].length); (B)</p> Signup and view all the answers

What does the following code snippet do: 'System.out.println(names.length + " rows");'?

<p>Prints the number of rows in the 'names' 2D array. (C)</p> Signup and view all the answers

What technique is commonly used to populate a 2D array during its declaration?

<p>Using an initializer list. (B)</p> Signup and view all the answers

How do you iterate through a 2D array using a regular for loop?

<p>Using two nested for loops for rows and columns. (C)</p> Signup and view all the answers

Which of the following statements about 2D arrays is false?

<p>The length property on a 2D array returns total elements in the array. (A)</p> Signup and view all the answers

What are 'formal parameters' in the context of a constructor?

<p>Data types and names listed in the constructor header (B)</p> Signup and view all the answers

Which method call would move the turtle backward by 10 pixels?

<p>t1.backward() (C)</p> Signup and view all the answers

What happens when a method call is executed in a program?

<p>The statements within the method are executed before any following lines (D)</p> Signup and view all the answers

In the constructor example provided, what do (300,300) represent?

<p>Actual parameters passed to the constructor (C)</p> Signup and view all the answers

What is indicated by the term 'call by value'?

<p>Formal parameters receive copies of actual parameters (D)</p> Signup and view all the answers

Which of the following methods is used to change the color of the turtle?

<p>t1.fillColor(Color.blue) (B)</p> Signup and view all the answers

What is the effect of a method's return value on control flow?

<p>Control flow is returned to the point after the method was called (C)</p> Signup and view all the answers

Which statement correctly describes how programmers interact with methods?

<p>Programmers can use methods based on understanding their purpose (B)</p> Signup and view all the answers

Flashcards are hidden until you start studying

Study Notes

Working with ArrayLists

  • ArrayList Creation: Initialize an ArrayList with ArrayList cars = new ArrayList();
  • Element Addition: Use cars.add(element) to add elements like "Corolla", "Camry", "Prius", etc.
  • Element Availability Check: Use an enhanced for loop to determine if a specific element (e.g., "Prius") is available and update a string accordingly.
  • Printing Elements: Print the status of availability using System.out.println(Prius);.

Finding Minimum and Maximum Values

  • Finding Minimum: Set initial min to the first element of the grades ArrayList. Loop through elements and update min if a smaller value is found.
  • Finding Maximum: Similar to minimum, initialize max and check for larger values during iteration.
  • Enhanced for Loop: Utilizes a simpler syntax for iterating through collections.

Reversing ArrayList Elements

  • Element Access: Use regular for loops in reverse order to add elements back into the ArrayList.
  • Element Removal: Remove original elements from the ArrayList to keep only the reversed elements.
  • Index Management: Important to use remove(0) to continuously access the first element without skipping any.

Array Basics

  • Declaration: Declare an array with a name followed by new and defined data type (e.g., String[] names = new String[5];).
  • Default Values: New arrays automatically initialize to default values (null for strings, 0 for integers).
  • Array Indices: Start at 0; trying to access out-of-bounds results in ArrayIndexOutOfBoundsException.

Perfect Size Arrays

  • Definition: A perfect size array has a number of elements equal to the allocated memory.
  • Usage: Ideal for fixed data, such as days of the week where the size won’t change.
  • Array Method Parameters: Pass by reference, indicating the location in memory rather than the contents.

Modifying and Accessing Arrays

  • Element Modification: Access array elements using their index to assign new values.
  • 2D Arrays: Defined with two sets of brackets (e.g., String[][] names = new String[rows][columns];).
  • Accessing in 2D Arrays: Use two indices to access and manipulate element positions within the array.

Array Method Calls

  • Method Functionality: Methods define behaviors for objects, including how to manipulate variables or perform actions.
  • Control Flow: Method calls interrupt sequence, executing all statements in the method before resuming.

Turtle Class Example

  • Method Examples: Methods like forward(), backward(), right(), and left() control the movement of a Turtle object.
  • Understanding Methods: Programmers can utilize methods without understanding their implementation, focusing on input-output behavior instead.

Studying That Suits You

Use AI to generate personalized quizzes and flashcards to suit your learning preferences.

Quiz Team

More Like This

Use Quizgecko on...
Browser
Browser