NEW
Font size
WorksheetsJava Array
Total questions: 45
Worksheet time: 36mins
ar, given the following declaration:int[] ar = {2, 4, 6, 8 }0, 1, 2, 31, 2, 3, 40, 2, 4, 6What is the first index of an array?
1
0
2
3
For the array:
int stats[3];
What is the range of the index?
0 to 3
0 to 2
1 to 3
0 to 4
int [] nums = {2, 3, 5, 8, 9, 11};
How would you access the fourth element in nums?
nums[4]
nums[3]
nums(4)
nums(3)
a = {0, 2, 3, 4} and v = 1?
a = {0, 2, 3, 4} and v = 3?
a = {0, 2, 3, 4} and v = 2?
// Java program to illustrate creating an array
// of integers, puts some values in the array,
// and prints each value to standard output.
class GFG
{
public static void main (String[] args)
{
// declares an Array of integers.
int[] arr;
// allocating memory for 5 integers.
arr = new int[5];
// initialize the first elements of the array
arr[0] = 10;
// initialize the second elements of the array
arr[1] = 20;
//so on...
arr[2] = 30;
arr[3] = 40;
arr[4] = 50;
// accessing the elements of the specified array
for (int i = 0; i < arr.length; i++)
System.out.println("Element at index " + i +
" : "+ arr[i]);
}
}
Element at index 0 : 10
Element at index 1 : 10
Element at index 2 : 20
Element at index 3 : 30
Element at index 4 : 40
Element at index 0 : 50
Element at index 1 : 40
Element at index 2 : 30
Element at index 3 : 20
Element at index 4 : 10
Element at index 0 : 10
Element at index 1 : 20
Element at index 2 : 30
Element at index 3 : 40
Element at index 4 : 50
String[] nama = {"Linda", "Santi", "Susan", "Mila", "Ayu"};
System.out.println(nama[2]);
Ayu
Mila
Susan
Linda
Which of the following is FALSE about arrays on Java
A java array is always an object
Length of array can be changed after creation of array
Arrays in Java are always allocated on heap
Predict the output?
public class Main {
public static void main(String args[]) {
int arr[] = {10, 20, 30, 40, 50};
for(int i=0; i < arr.length; i++)
{
System.out.print(" " + arr[i]);
}
}
}
10 20 30 40 50
Compiler Error
10 20 30 40
class Test {
public static void main(String args[]) {
int arr[2];
System.out.println(arr[0]);
System.out.println(arr[1]);
}
}
0
0
garbage value
garbage value
Compiler Error
Exception
public class Main {
public static void main(String args[]) {
int arr[][] = new int[4][];
arr[0] = new int[1];
arr[1] = new int[2];
arr[2] = new int[3];
arr[3] = new int[4];
int i, j, k = 0;
for (i = 0; i < 4; i++) {
for (j = 0; j < i + 1; j++) {
arr[i][j] = k;
k++;
}
}
for (i = 0; i < 4; i++) {
for (j = 0; j < i + 1; j++) {
System.out.print(" " + arr[i][j]);
k++;
}
System.out.println();
}
}
}
Compiler Error
0
1 2
3 4 5
6 7 8 9
0
0 0
0 0 0
0 0 0 0
9
7 8
4 5 6
0 1 2 3
Which of the following statements correctly declares a two-dimensional integer array?
int Matrix[ ] = new int[5,4];
int Matrix[ ]; Matrix = new int[5,4];
int Matrix[ ][ ] = new int[5][4];
int Matrix[ ][ ] = int[5][4];
Which of the following is advantage of java array?
Code Optimization
Random access
Size No-Limit
Both A and B
In java, array elements are stored in ________ memory locations.
Random
Sequential
Sequential & Random
Binary search
What will be output for the following code?
class array_output
{ public static void main(String args[])
{ int array_variable[][] = {{ 1, 2, 3}, { 4 , 5, 6}, { 7, 8, 9}};
int sum = 0;
for (int i = 0; i < 3; ++i)
for (int j = 0; j < 3 ; ++j)
sum = sum + array_variable[i][j];
System.out.print(sum / 5); } }
8
9
10
11
How to sort an array?
Array.sort()
Arrays.sort()
Collection.sort()
System.sort()
How to copy contents of array?
System.arrayCopy()
Array.copy()
Arrays.copy()
Collection.copy()
Java code to create a non-element array of student marks with the size of 20.
double mark;
double [20] mark = new double;
double [ ] mark = new mark [20];
double [ ] mark = new double [20];
Java code to create a non-element array of student name with size 15.
String [ ] name = new String;
String [ ] name = new String [15];
name = new String [15];
String [15] name = new String [ ];
Java code to create an array of student id with element 115331, 450055, 555641, 875560 and 987555.
int [ ] id = new {115331, 450055, 555641, 875560, 987555};
int [5] id = {115331, 450055, 555641, 875560, 987555};
int [ ] id = {115331, 450055, 555641, 875560, 987555};
int [ ] id = new int [5];
Java code to create an array of subject code with element S, M, C, P, I and K.
char [ ] code = {'S', 'M', 'C', 'P', 'I' and 'K'};
char [ ] code = {S, M, C, P, I, K};
char [ ] code = {"S", "M", "C", "P", "I", "K"};
char [ ] code = {'S', 'M', 'C', 'P', 'I', 'K'};
Java code to display the size of an array name studentname[ ].
System.out.println("The size is " + studentname[ index ]);
System.out.println("The size is " + studentname.length);
System.out.println("The size is " + length.length);
System.out.println("The size is " + studentname);
Java code to display all elements of array studentname[ ].
Java code to read elements into array studentname[ ].
Java code to read elements into array customerid[ ].
None of the above is the right answer.
A code segment to identify the largest element in an array.
Based on the code segment below:- identify the name of the array.
for(int j=0; j<balance.length; j++)
{
System.out.print("Enter amount: ");
.......
}
No answer.
temp[ ]
balance
balance [ i ]
Based on the code segment below:- identify the name of the array.
for(int j=0; j<balance.length; j++)
{
System.out.print("Enter amount: ");
.......
}
No answer.
temp[ ]
balance
balance [ i ]
Java array is a collection of ________.
similar type of elements
different type of element
heterogeneous data
Both A and C
Array data access using _____.
Operator
Variable
index
Pointer
At time of array initialization which is necessary to specify?
Row
Column
Row and Column
None of the above
Java Array can allocate __________.
Dynamic Memory
Static Memory
Both A and B
None of the above
Which of the following is an incorrect array declaration?
int [] arr = new int[5]
int [] arr = new int[5]
int arr[] = new int[5]
int arr[] = int [5] new
Index in array start with ______.
-1
0
1
Infinite
We can calculate the length of an array using ________.
sizeof(array)
array.len
array.length
array.sizeof()
