0% found this document useful (0 votes)
44 views

ARRAYS

The document contains 20 multiple choice questions about arrays in Java. Some key points covered are: - Arrays allow storing elements of the same type in contiguous memory locations. - Elements are accessed using their indices, from 0 to size-1. - Arrays can be initialized during or after declaration using curly braces. - Multi-dimensional arrays represent structured data in a tabular format. - ArrayLists can dynamically change in size unlike fixed-size arrays. - Out of bounds index access causes a runtime error.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views

ARRAYS

The document contains 20 multiple choice questions about arrays in Java. Some key points covered are: - Arrays allow storing elements of the same type in contiguous memory locations. - Elements are accessed using their indices, from 0 to size-1. - Arrays can be initialized during or after declaration using curly braces. - Multi-dimensional arrays represent structured data in a tabular format. - ArrayLists can dynamically change in size unlike fixed-size arrays. - Out of bounds index access causes a runtime error.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

ARRAYS

MCQ

1. What is the primary purpose of arrays in Java?


A. Store elements of different data types
B. Store elements of the same type
C. Store elements with different sizes
D. Store elements with different names
Answer: B. Store elements of the same type

2. What is the benefit of efficient storage and retrieval in Java arrays?


A. Allows storing unlimited elements
B. Saves memory space
C. Enables quick access to elements based on indices
D. Reduces the need for loops
Answer: C. Enables quick access to elements based on indices

3. How are array elements accessed in Java?


A. Using their names
B. Using their values
C. Using their indices
D. Using their data types
Answer: C. Using their indices

4. What does the index 0 represent in an array?


A. First element
B. Last element
C. Number of elements
D. Array size
Answer: A. First element

5. How can you initialize a Java array at the time of declaration?


A. By providing a comma-separated list of values enclosed in curly braces
B. By specifying only the array size
C. By providing elements one by one using 'add' method
D. By using a loop to fill the array
Answer: A. By providing a comma-separated list of values enclosed in curly braces

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

8. What is a multi-dimensional array in Java?


A. An array with multiple data types
B. An array that contains multiple elements of the same type
C. An array with multiple rows and columns
D. An array with varying sizes
Answer: C. An array with multiple rows and columns

9. What is the syntax for accessing an element in a two-dimensional array named


'matrix' at row 2, column 3?
A. matrix[2][3];
B. matrix(2, 3);
C. matrix[3][2];
D. matrix(3)(2);
Answer: A. matrix[2][3];

10. What do multi-dimensional arrays allow you to store and manipulate?


A. Structured data in a tabular format
B. Elements of different data types
C. Dynamic data of varying sizes
D. Single values
Answer: A. Structured data in a tabular format

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

13. What data types can Java arrays store?


A. Only numeric data types
B. Only characters
C. Elements of any data type, including user-defined types
D. Only boolean values
Answer: C. Elements of any data type, including user-defined types

14. Which of the following statements is true about multi-dimensional arrays in


Java?
A. All rows in a multi-dimensional array must have the same number of columns
B. Multi-dimensional arrays can have different data types in each row
C. Multi-dimensional arrays in Java are always rectangular (same number of columns
in each row)
D. Multi-dimensional arrays are not supported in Java
Answer: A. All rows in a multi-dimensional array must have the same number of
columns

15. What is the benefit of using ArrayLists over arrays in Java?


A. ArrayLists allow storing elements of different data types
B. ArrayLists can change in size dynamically
C. ArrayLists provide faster access to elements
D. ArrayLists require less memory than arrays
Answer: B. ArrayLists can change in size dynamically

16. In a two-dimensional array named 'table', how is an element accessed at row 3,


column 2?
A. table[2, 3];
B. table[3][2];
C. table(3, 2);
D. table(2)[3];
Answer: B. table[3][2];

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

18. How is memory allocated for Java arrays?


A. Randomly in the memory
B. Sequentially in contiguous memory locations
C. In reverse order
D. Memory allocation for arrays is not fixed
Answer: B. Sequentially in contiguous memory locations

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

1. What is the primary purpose of arrays in Java?

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.

2. How are elements accessed in a Java array?

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.

3. What is the syntax for declaring an array in Java?

Answer: The syntax for declaring an array in Java is:


type[] arrayName = new type[arraySize];

4. Can Java arrays store elements of different data types?

Answer: No, Java arrays can only store elements of the same data type.

5. What is the purpose of initializing an array in Java?


Answer: Initializing an array in Java assigns initial values to its elements,
providing specific data to work with during program execution.

6. What is the index of the last element in an array with size n?

Answer: The index of the last element in an array with size n is n - 1.

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.

8. What is the syntax for accessing an element in a two-dimensional array at row 2,


column 3?

Answer: The syntax for accessing an element in a two-dimensional array at row 2,


column 3 is:
arrayName[3][4];

9. What do multi-dimensional arrays allow you to store and manipulate?

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: One-dimensional arrays have a single row of elements, while multi-


dimensional arrays have multiple rows and columns.

11. How are array elements initialized in Java?

Answer: Array elements in Java can be initialized using a comma-separated list of


values enclosed in curly braces {} at the time of declaration.

Answer: If you try to access an index that is out of bounds in an array, it will
result in a runtime error.**

13. What is the benefit of using ArrayLists over arrays in Java?

Answer: ArrayLists in Java can change in size dynamically, allowing for more
flexibility compared to fixed-size arrays.

14. How can you modify an element in a Java array?

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.

15. What is the purpose of declaring an array in Java?

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?

Answer: Java multi-dimensional arrays allow you to represent complex data


structures such as matrices, tables, grids, and more.

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.

20. Can the size of a Java array be changed after initialization?

Answer: No, the size of a Java array cannot be changed after initialization.

You might also like