slug
type
status
category
summary
date
tags
password
icon
Here are the key points, sample code, and 20 AP-style MCQs based on the topics you've provided.

Key Points:

  1. Array Creation and Access (6.1)
      • Arrays are used to store multiple values in a single variable.
      • Arrays are indexed starting at 0.
      • 1D Array can be created by specifying the size and data type.
      • ArrayList is a dynamic array that can grow in size as elements are added.
      • Basic operations: creating, accessing elements, updating, and traversing arrays.
  1. Traversing Arrays (6.2)
      • Traversing refers to iterating over each element of the array.
      • It’s typically done using a loop (for, while).
      • Arrays and ArrayLists can be manipulated by modifying elements in the loop.
      • Errors in code can often stem from incorrect indexing, out-of-bound access, or improper initialization.

Sample Code:

1. Array Creation and Access
2. Traversing Arrays
3. ArrayList Manipulation

20 AP-style MCQs (without answers):

  1. Which of the following statements correctly creates an array of 5 integers?
      • A) int[] arr = {1, 2, 3, 4, 5};
      • B) int[] arr = new int[5];
      • C) int arr[5] = {1, 2, 3, 4, 5};
      • D) Both A and B
  1. What is the index of the last element in the array int[] arr = {10, 20, 30, 40, 50};?
      • A) 5
      • B) 4
      • C) 3
      • D) 0
  1. What will the following code output?
      • A) 5
      • B) 10
      • C) 15
      • D) Error
  1. Which of the following statements is true about ArrayLists in Java?
      • A) They have a fixed size after initialization.
      • B) They can hold elements of any type, including primitives.
      • C) They are dynamic in size and can grow as needed.
      • D) ArrayLists can only hold Integer objects.
  1. What is the output of the following code?
      • A) 200
      • B) 100
      • C) Error
      • D) null
  1. What will happen if you try to access arr[10] in the following code?
      • A) It will print 0.
      • B) It will throw an ArrayIndexOutOfBoundsException.
      • C) It will print null.
      • D) It will print 10.
  1. Which of the following methods can be used to traverse an ArrayList?
      • A) for (int i = 0; i < list.size(); i++)
      • B) for (int i : list)
      • C) list.forEach(element -> System.out.println(element));
      • D) All of the above
  1. What is the correct way to create an ArrayList of Strings?
      • A) ArrayList<String> list = new ArrayList<>();
      • B) ArrayList<String> list = new ArrayList[10];
      • C) String[] list = new String[10];
      • D) ArrayList<String> list = new ArrayList();
  1. How do you access the second element of an ArrayList list?
      • A) list[1]
      • B) list.get(1)
      • C) list(1)
      • D) list[2]
  1. What is the result of the following code?
      • A) 2
      • B) 6
      • C) 8
      • D) Error
  1. Which of the following statements is incorrect about arrays?
      • A) Arrays in Java are fixed in size once they are initialized.
      • B) Arrays in Java can hold primitive data types.
      • C) Arrays can hold multiple types of data at once.
      • D) Arrays are indexed starting at 0.
  1. What will happen if you attempt to access an index out of bounds in an array?
      • A) The program will crash immediately.
      • B) An ArrayIndexOutOfBoundsException will be thrown.
      • C) The program will print the value of index 0.
      • D) The program will print null.
  1. How can you initialize an array with 10 elements of type int?
      • A) int[] arr = new int[10];
      • B) int arr[] = new int[10];
      • C) int[] arr = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
      • D) All of the above
  1. Which of the following will cause a compilation error?
      • A) int[] arr = new int[5];
      • B) ArrayList<String> list = new ArrayList<>();
      • C) ArrayList list = new ArrayList<String>();
      • D) int[] arr = {1, 2, 3, 4};
  1. What is the default value of an integer element in an array?
      • A) 0
      • B) null
      • C) undefined
      • D) Error
  1. Which of the following code segments correctly creates and manipulates an array of 5 integers?
      • A) int[] nums = {1, 2, 3, 4, 5};
      • B) int nums[] = new int[5]; nums[0] = 10;
      • C) Both A and B
      • D) None of the above
  1. Which of the following operations is invalid for an ArrayList?
      • A) list.add(10);
      • B) list.remove(2);
      • C) list[0] = 100;
      • D) list.size();
  1. Which of the following is true about the ArrayList list in Java?
      • A) It can be resized dynamically.
      • B) It can only hold Integer objects.
      • C) It can hold both primitive types and objects.
      • D) It does not support element removal.
  1. What will happen if you try to access an element using an invalid index in an ArrayList?
      • A) It will return null.
      • B) It will throw an IndexOutOfBoundsException.
      • C) It will print the last element.
      • D) It will print an empty string.
  1. What is the correct syntax for initializing an array of 10 integers?
      • A) int[] arr = new int[10];
      • B) int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
      • C) int arr[10];
      • D) A and B

Answers:
  1. D) Both A and B
  1. B) 4
  1. B) 10
  1. C) They are dynamic in size and can grow as needed.
  1. B) 100
  1. B) It will throw an ArrayIndexOutOfBoundsException.
  1. D) All of the above
  1. A) ArrayList<String> list = new ArrayList<>();
  1. B) list.get(1)
  1. B) 6
  1. C) Arrays can hold multiple types of data at once.
  1. B
  1. D
  1. C
  1. A
  1. C
  1. C
  1. A
  1. B
  1. D
 
 
CSA UNIT 5: Writing ClassesCSA UNIT 7: ArrayList
Loading...
现代数学启蒙
现代数学启蒙
推广现代数学🍚
最新发布
Statistics Key  Concepts and Selected Questions (*_*)
2025-2-20
CSA UNIT 7: ArrayList
2025-2-20
CSA UNIT 6:  ARRAY
2025-2-20
CSA UNIT 10: Recursion
2025-2-20
CSA UNIT 9: Inheritance
2025-2-19
CSA UNIT 8: 2D Array
2025-2-19
公告
🎉现代数学启蒙(MME:Modern Mathematics Enlightenment)欢迎您🎉
-- 感谢您的支持 ---