ARRAYS
ARRAYS
MCQ
6. What is the correct way to declare an array of integers named 'numbers' in Java
with size 10?
A. int numbers[10];
B. int[10] numbers;
C. int[] numbers = new int[10];
D. numbers = new int[10];
Answer: C. int[] numbers = new int[10];
7. What can you use to handle varying amounts of data dynamically in Java, unlike
arrays?
A. Arrays
B. ArrayLists
C. Strings
D. Variables
Answer: B. ArrayLists
11. Which of the following best describes the initialization of a Java array?
A. Arrays cannot be initialized in Java
B. Initialization is not necessary for arrays in Java
C. Arrays can be initialized only during declaration
D. Arrays can be initialized both during declaration and later using curly braces
Answer: D. Arrays can be initialized both during declaration and later using curly
braces
12. What is the index of the last element in an array with size n?
A. n
B. n - 1
C. n + 1
D. 0
Answer: B. n - 1
17. What is the key difference between a one-dimensional array and a multi-
dimensional array?
A. One-dimensional arrays can store elements of different data types
B. Multi-dimensional arrays have more memory space
C. One-dimensional arrays have a single row of elements, while multi-dimensional
arrays have multiple rows and columns
D. Multi-dimensional arrays are faster in terms of data access
Answer: C. One-dimensional arrays have a single row of elements, while multi-
dimensional arrays have multiple rows and columns
19. What happens if you try to access an index that is out of bounds in an array?
A. The program will compile but give a runtime error
B. The program will compile and run, but the result will be unexpected
C. The program will not compile
D. The program will compile and run without any issues
Answer: A. The program will compile but give a runtime error
20. Which Java data structure provides an ordered collection and allows duplicate
elements?
A. Arrays
B. Sets
C. ArrayLists
D. Maps
Answer: C. ArrayLists
IDENTIFICATION
Answer: Arrays in Java are used to store a collection of elements of the same type.
They provide a way to organize and manipulate data as a contiguous block of memory.
Answer: Elements in a Java array are accessed using their indices, which start from
0 for the first element and go up to arraySize - 1 for the last element.
Answer: No, Java arrays can only store elements of the same data type.
7. What data structure can be used in Java to handle varying amounts of data
dynamically?
Answer: ArrayLists in Java can handle varying amounts of data dynamically unlike
fixed-size arrays.
Answer: Multi-dimensional arrays allow you to store and manipulate structured data
in a tabular format, such as matrices, tables, and grids.
10. What is the key difference between a one-dimensional array and a multi-
dimensional array?
Answer: If you try to access an index that is out of bounds in an array, it will
result in a runtime error.**
Answer: ArrayLists in Java can change in size dynamically, allowing for more
flexibility compared to fixed-size arrays.
Answer: You can modify an element in a Java array by assigning a new value to the
specific index of the element you want to change.
Answer: Declaring an array in Java allocates memory space for the array and
specifies the type of elements it can store.
16. What are the indices used for accessing elements in a Java array?
Answer: The indices used for accessing elements in a Java array start from 0 for
the first element and go up to arraySize - 1 for the last element.
17. What do Java multi-dimensional arrays allow you to represent?
18. What is the benefit of efficient storage and retrieval in Java arrays?
Answer: Efficient storage and retrieval in Java arrays enable quick access to
individual elements based on their indices, allowing for fast and efficient data
manipulation.**
19. What is the advantage of using arrays in loops for data processing?
Answer: Using arrays in loops enables efficient iteration over elements, allowing
for processing of large amounts of data.
Answer: No, the size of a Java array cannot be changed after initialization.