
Data Structure Assigment03
Authored by My Dream
Computers
University
Used 6+ times

AI Actions
Add similar questions
Adjust reading levels
Convert to real-world scenario
Translate activity
More...
Content View
Student View
10 questions
Show all answers
1.
MULTIPLE CHOICE QUESTION
30 sec • 1 pt
Which of the following best describes an array in Java?
A collection that stores elements of different data types and resizes dynamically.
A fixed-size data structure that stores elements of the same data type in contiguous memory locations.
A predefined class in Java that allows dynamic insertion and deletion of elements.
A data structure that automatically sorts its elements during insertion.
Answer explanation
An array in Java is a fixed-size, contiguous block of memory that holds elements of the same data type. It is a fundamental data structure, and unlike some other collections, it does not dynamically resize and can only store a single type of data.
[Its size is determined at the time of creation and cannot change afterwards.]
2.
MULTIPLE CHOICE QUESTION
30 sec • 1 pt
Consider a 2D array in Java declared as int[][] matrix = new int[3][3];. Which of the following statements is true regarding the memory layout and access order of this array?
Java stores 2D arrays in column-major order, where elements in the same column are stored contiguously.
Java stores 2D arrays in row-major order, where elements in the same row are stored contiguously.
Java guarantees contiguous memory for the entire 2D array in either row-major or column-major order.
Java 2D arrays are not stored in any order since they are implemented as a list of linked lists.
Answer explanation
In Java, a 2D array is an array of arrays. Each inner array represents a row. This means that elements within a single row are stored contiguously in memory. This is known as row-major order.
[ Java implements 2D arrays as arrays of arrays, and memory layout follows row-major order — elements in single row are stored contiguously in memory as each row is itself separate array.
However, Java does not store the entire 2D array as a single contiguous block like
C/C++. ]
3.
MULTIPLE CHOICE QUESTION
30 sec • 1 pt
Which of the following is a valid way to declare and initialize an integer array in Java?
int array = new int[5];
int[] array = new int(5);
int[] array = new int[5];
array int[] = new int[];
Answer explanation
The correct syntax for declaring and initializing an array in Java is dataType[] arrayName = new dataType[size];. Option c follows this syntax correctly. Option a is incorrect because it is missing the brackets []. Option b uses parentheses () instead of brackets []. Option d has the type and variable name reversed.
4.
MULTIPLE CHOICE QUESTION
30 sec • 1 pt
Which of the following statements about the declaration and initialization of a 2D integer array in Java is true?
int[][] arr = new int[7][]; creates a 2D array with 7 rows and undefined column sizes.
int[][] arr = new int[][3]; is valid and creates a 2D array with 3 columns and undefined row size.
int[][] arr = new int[2][3] {{1, 2, 3}, {4, 5, 6}} correctly initializes a 2D array with values.
In Java, all multidimensional arrays must be rectangular (i.e., all rows must have the same number of columns).
Answer explanation
In Java, you can declare a 2D array with only the number of rows specified, leaving the number of columns to be defined later for each row individually. This allows for the creation of "jagged" arrays. Options b and c have incorrect syntax, and option d is false because Java does support jagged arrays.
5.
MULTIPLE CHOICE QUESTION
30 sec • 1 pt
Which of the following statements about Java's Spliterator interface is true?
A Spliterator can only be used for sequential iterations and does not support parallel processing.
A Spliterator allows splitting of data sources to enable parallel processing using trySplit().
The Spliterator interface is a subclass of Iterator and inherits all of its methods.
A Spliterator must always return the same size estimate regardless of the split.
Answer explanation
The Spliterator interface was introduced in Java 8 to support parallel iteration of data sources. Its key feature is the trySplit() method, which attempts to divide the data source into two, allowing for parallel processing,
It splits until reaching the smallest value to be divided into 2 halves
for example, in the Stream API.
[ Ex: start with 1000 => s0=500; s1=500 => s2=250; s1=250 ]
6.
MULTIPLE CHOICE QUESTION
30 sec • 1 pt
Consider the following Java code snippet: Java public class ArrayTest {
public static void main(String[] args) {
int [] numbers = new int [ 5 ];
for ( int i = 0 ; i < numbers.length; i++) {
numbers[i] = i * 2 ; }
System.out.println(numbers[ 5 ]); }}
What will be the output of this program?
10
ArrayIndexOutOfBoundsException
0
Compilation Error
Answer explanation
The array numbers is declared with a size of 5, which means its valid indices are from 0 to 4. The line System.out.println(numbers[5]); attempts to access the element at index 5, which is outside the array's bounds. This will cause a runtime error, specifically an ArrayIndexOutOfBoundsException.
7.
MULTIPLE CHOICE QUESTION
30 sec • 1 pt
Which of the following statements about the Vector class in Java is true?
Vector is not synchronized and is suitable only for single-threaded environments.
Vector automatically shrinks in size when elements are removed.
Vector is synchronized and allows dynamic resizing with thread-safe operations.
Vector is a subclass of Set and does not allow duplicate elements.
Answer explanation
Vector is a legacy class in Java. It is a synchronized list, meaning its methods are thread-safe. This makes it suitable for use in multi-threaded environments. It also supports dynamic resizing, similar to ArrayList, but with the added synchronization overhead.
[Vector class in Java is part of the java.util package. It is synchronized, meaning it is thread-safe for concurrent access.
It supports dynamic resizing (like ArrayList) and allows
duplicate elements.
It is not a subclass of Set, and it does not automatically shrink its capacity unless explicitly trimmed.]
Access all questions and much more by creating a free account
Create resources
Host any resource
Get auto-graded reports

Continue with Google

Continue with Email

Continue with Classlink

Continue with Clever
or continue with

Microsoft
%20(1).png)
Apple
Others
Already have an account?