carolyn.rios
carolyn.rios 3d ago • 10 views

Array and List Quiz: Test Your Java Data Structure Knowledge

Hey there! 👋 Java arrays and lists are fundamental. Let's solidify your knowledge with this quiz! Good luck, you've got this! 🍀
💻 Computer Science & Technology
🪄

🚀 Can't Find Your Exact Topic?

Let our AI Worksheet Generator create custom study notes, online quizzes, and printable PDFs in seconds. 100% Free!

✨ Generate Custom Content

1 Answers

✅ Best Answer
User Avatar
tina491 5d ago

📚 Quick Study Guide

  • 🔢 Arrays: Fixed-size, contiguous memory allocation. Elements accessed by index.
  • ⏱️ Array Declaration: `int[] myArray = new int[10];`
  • 📍 Lists: Dynamic size, part of the `java.util` package. Can grow or shrink as needed.
  • 🔗 ArrayList: Common implementation of the List interface. `ArrayList myList = new ArrayList<>();`
  • Adding elements to List: Use `.add()` method. Example: `myList.add("Hello");`
  • Removing elements from List: Use `.remove()` method. Example: `myList.remove(0);` (removes element at index 0)
  • 🔑 Key Difference: Arrays have a fixed size, while Lists are dynamic. Lists offer more flexibility.

🧪 Practice Quiz

  1. Which of the following statements correctly declares an array of integers with a size of 5 in Java?
    1. `int arr = new int[5];`
    2. `int[] arr = new int(5);`
    3. `int[] arr = new int[5];`
    4. `int arr[] = new int[];`
  2. What is the index of the first element in an array in Java?
    1. 1
    2. 0
    3. -1
    4. The array size
  3. Which method is used to add an element to an `ArrayList` in Java?
    1. `append()`
    2. `add()`
    3. `insert()`
    4. `push()`
  4. What happens if you try to access an array element at an index that is out of bounds?
    1. The program compiles successfully but throws an exception at runtime.
    2. The program compiles successfully and returns a default value.
    3. The program doesn't compile.
    4. The program compiles successfully and the value at the nearest valid index is returned.
  5. Which data structure provides dynamic resizing capability?
    1. Array
    2. List
    3. Both Array and List
    4. Neither Array nor List
  6. Which of the following is NOT a valid way to initialize an array?
    1. `int[] arr = {1, 2, 3};`
    2. `int[] arr = new int[]{1, 2, 3};`
    3. `int[] arr = new int[3]; arr = {1, 2, 3};`
    4. `int[] arr = new int[3];`
  7. What is the purpose of the `size()` method in `ArrayList`?
    1. To return the maximum capacity of the list.
    2. To return the number of elements currently in the list.
    3. To set the maximum capacity of the list.
    4. To check if the list is empty.
Click to see Answers
  1. C
  2. B
  3. B
  4. A
  5. B
  6. C
  7. B

Join the discussion

Please log in to post your answer.

Log In

Earn 2 Points for answering. If your answer is selected as the best, you'll get +20 Points! 🚀