Array - Study Mode

[#46] Which of the following statements is true about arrays in Java?
Correct Answer

(D) Arrays are objects of type Array.

Explanation

Solution: In Java, arrays are objects. They are instances of classes that extend the abstract class java.lang.Object. However, arrays in Java are implemented as objects of a specific type called "Array". This means that arrays in Java are indeed objects, but they are not instances of the Array class
rather, they are instances of classes that are automatically generated by the Java Virtual Machine (JVM) when arrays are created. Option A: Arrays can change in size after initialization. In Java, once an array is initialized with a specific size, that size cannot be changed. Arrays have a fixed length determined at the time of creation. While you can create a new array with a different size and copy elements from the old array to the new one to achieve a similar effect, the original array itself cannot change its size. Option B: Arrays can store elements of different types. In Java, arrays are homogeneous data structures, meaning they can only store elements of the same type. When you declare an array, you specify the type of elements it can hold. All elements in the array must be compatible with this type. Option C: Arrays can have a negative length. In Java, array lengths must be non-negative integers. It is not possible to declare an array with a negative length. Attempting to do so will result in a compilation error. Therefore, the correct answer is Option D: Arrays are objects of type Array. Arrays in Java are indeed objects, but they are instances of classes that are automatically generated by the Java Virtual Machine (JVM) when arrays are created. They are not instances of the Array class itself, but they are objects nonetheless.

[#47] How do you declare and initialize an array of integers with 5 elements in Java?
Correct Answer

(A) int[] numbers = new int[5]

[#48] How do you declare a dynamic array (ArrayList) of integers in Java?
Correct Answer

(B) ArrayList numbers = new ArrayList ()

[#49] In Java, how do you remove an element at a specific index from an ArrayList named list?
Correct Answer

(A) list.remove(index)

[#50] How do you declare a one-dimensional array in Java?
Correct Answer

(A) int[] myArray

Explanation

Solution: In Java, a one-dimensional array is declared by specifying the type of elements followed by square brackets and then the array name. The correct syntax is: type[] arrayName
Option A follows this syntax and is the correct way to declare a one-dimensional array in Java. Option B is also a valid syntax but is less commonly used. Both Option A and Option B are equivalent. Option C is incorrect as it attempts to use generics syntax, which is not allowed for arrays in Java. Option D is incorrect because Option C is not a valid way to declare an array.