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:
- 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.
- 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):
- 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
- 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
- What will the following code output?
- A) 5
- B) 10
- C) 15
- D) Error
- 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.
- What is the output of the following code?
- A) 200
- B) 100
- C) Error
- D) null
- 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
.
- 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
- 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();
- How do you access the second element of an ArrayList
list
? - A)
list[1]
- B)
list.get(1)
- C)
list(1)
- D)
list[2]
- What is the result of the following code?
- A) 2
- B) 6
- C) 8
- D) Error
- 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.
- 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
.
- 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
- 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};
- What is the default value of an integer element in an array?
- A) 0
- B) null
- C) undefined
- D) Error
- 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
- 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();
- 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.
- 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.
- 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:
- D) Both A and B
- B) 4
- B) 10
- C) They are dynamic in size and can grow as needed.
- B) 100
- B) It will throw an
ArrayIndexOutOfBoundsException
.
- D) All of the above
- A)
ArrayList<String> list = new ArrayList<>();
- B)
list.get(1)
- B) 6
- C) Arrays can hold multiple types of data at once.
- B
- D
- C
- A
- C
- C
- A
- B
- D
- 作者:现代数学启蒙
- 链接:https://www.math1234567.com/article/reviewofarray
- 声明:本文采用 CC BY-NC-SA 4.0 许可协议,转载请注明出处。
相关文章